Fluent.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayAccess;
  4. use Illuminate\Contracts\Support\Arrayable;
  5. use Illuminate\Contracts\Support\Jsonable;
  6. use JsonSerializable;
  7. class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
  8. {
  9. /**
  10. * All of the attributes set on the fluent instance.
  11. *
  12. * @var array
  13. */
  14. protected $attributes = [];
  15. /**
  16. * Create a new fluent instance.
  17. *
  18. * @param array|object $attributes
  19. * @return void
  20. */
  21. public function __construct($attributes = [])
  22. {
  23. foreach ($attributes as $key => $value) {
  24. $this->attributes[$key] = $value;
  25. }
  26. }
  27. /**
  28. * Get an attribute from the fluent instance.
  29. *
  30. * @param string $key
  31. * @param mixed $default
  32. * @return mixed
  33. */
  34. public function get($key, $default = null)
  35. {
  36. if (array_key_exists($key, $this->attributes)) {
  37. return $this->attributes[$key];
  38. }
  39. return value($default);
  40. }
  41. /**
  42. * Get the attributes from the fluent instance.
  43. *
  44. * @return array
  45. */
  46. public function getAttributes()
  47. {
  48. return $this->attributes;
  49. }
  50. /**
  51. * Convert the fluent instance to an array.
  52. *
  53. * @return array
  54. */
  55. public function toArray()
  56. {
  57. return $this->attributes;
  58. }
  59. /**
  60. * Convert the object into something JSON serializable.
  61. *
  62. * @return array
  63. */
  64. #[\ReturnTypeWillChange]
  65. public function jsonSerialize()
  66. {
  67. return $this->toArray();
  68. }
  69. /**
  70. * Convert the fluent instance to JSON.
  71. *
  72. * @param int $options
  73. * @return string
  74. */
  75. public function toJson($options = 0)
  76. {
  77. return json_encode($this->jsonSerialize(), $options);
  78. }
  79. /**
  80. * Determine if the given offset exists.
  81. *
  82. * @param string $offset
  83. * @return bool
  84. */
  85. #[\ReturnTypeWillChange]
  86. public function offsetExists($offset)
  87. {
  88. return isset($this->attributes[$offset]);
  89. }
  90. /**
  91. * Get the value for a given offset.
  92. *
  93. * @param string $offset
  94. * @return mixed
  95. */
  96. #[\ReturnTypeWillChange]
  97. public function offsetGet($offset)
  98. {
  99. return $this->get($offset);
  100. }
  101. /**
  102. * Set the value at the given offset.
  103. *
  104. * @param string $offset
  105. * @param mixed $value
  106. * @return void
  107. */
  108. #[\ReturnTypeWillChange]
  109. public function offsetSet($offset, $value)
  110. {
  111. $this->attributes[$offset] = $value;
  112. }
  113. /**
  114. * Unset the value at the given offset.
  115. *
  116. * @param string $offset
  117. * @return void
  118. */
  119. #[\ReturnTypeWillChange]
  120. public function offsetUnset($offset)
  121. {
  122. unset($this->attributes[$offset]);
  123. }
  124. /**
  125. * Handle dynamic calls to the fluent instance to set attributes.
  126. *
  127. * @param string $method
  128. * @param array $parameters
  129. * @return $this
  130. */
  131. public function __call($method, $parameters)
  132. {
  133. $this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true;
  134. return $this;
  135. }
  136. /**
  137. * Dynamically retrieve the value of an attribute.
  138. *
  139. * @param string $key
  140. * @return mixed
  141. */
  142. public function __get($key)
  143. {
  144. return $this->get($key);
  145. }
  146. /**
  147. * Dynamically set the value of an attribute.
  148. *
  149. * @param string $key
  150. * @param mixed $value
  151. * @return void
  152. */
  153. public function __set($key, $value)
  154. {
  155. $this->offsetSet($key, $value);
  156. }
  157. /**
  158. * Dynamically check if an attribute is set.
  159. *
  160. * @param string $key
  161. * @return bool
  162. */
  163. public function __isset($key)
  164. {
  165. return $this->offsetExists($key);
  166. }
  167. /**
  168. * Dynamically unset an attribute.
  169. *
  170. * @param string $key
  171. * @return void
  172. */
  173. public function __unset($key)
  174. {
  175. $this->offsetUnset($key);
  176. }
  177. }