Fluent.php 5.0 KB

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