Fluent.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayAccess;
  4. use ArrayIterator;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use Illuminate\Contracts\Support\Jsonable;
  7. use Illuminate\Support\Traits\Conditionable;
  8. use Illuminate\Support\Traits\InteractsWithData;
  9. use Illuminate\Support\Traits\Macroable;
  10. use IteratorAggregate;
  11. use JsonSerializable;
  12. use Traversable;
  13. /**
  14. * @template TKey of array-key
  15. * @template TValue
  16. *
  17. * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
  18. * @implements \ArrayAccess<TKey, TValue>
  19. */
  20. class Fluent implements Arrayable, ArrayAccess, IteratorAggregate, Jsonable, JsonSerializable
  21. {
  22. use Conditionable, InteractsWithData, Macroable {
  23. __call as macroCall;
  24. }
  25. /**
  26. * All of the attributes set on the fluent instance.
  27. *
  28. * @var array<TKey, TValue>
  29. */
  30. protected $attributes = [];
  31. /**
  32. * Create a new fluent instance.
  33. *
  34. * @param iterable<TKey, TValue> $attributes
  35. */
  36. public function __construct($attributes = [])
  37. {
  38. $this->fill($attributes);
  39. }
  40. /**
  41. * Create a new fluent instance.
  42. *
  43. * @param iterable<TKey, TValue> $attributes
  44. * @return static
  45. */
  46. public static function make($attributes = [])
  47. {
  48. return new static($attributes);
  49. }
  50. /**
  51. * Get an attribute from the fluent instance using "dot" notation.
  52. *
  53. * @template TGetDefault
  54. *
  55. * @param TKey $key
  56. * @param TGetDefault|(\Closure(): TGetDefault) $default
  57. * @return TValue|TGetDefault
  58. */
  59. public function get($key, $default = null)
  60. {
  61. return data_get($this->attributes, $key, $default);
  62. }
  63. /**
  64. * Set an attribute on the fluent instance using "dot" notation.
  65. *
  66. * @param TKey $key
  67. * @param TValue $value
  68. * @return $this
  69. */
  70. public function set($key, $value)
  71. {
  72. data_set($this->attributes, $key, $value);
  73. return $this;
  74. }
  75. /**
  76. * Fill the fluent instance with an array of attributes.
  77. *
  78. * @param iterable<TKey, TValue> $attributes
  79. * @return $this
  80. */
  81. public function fill($attributes)
  82. {
  83. foreach ($attributes as $key => $value) {
  84. $this->attributes[$key] = $value;
  85. }
  86. return $this;
  87. }
  88. /**
  89. * Get an attribute from the fluent instance.
  90. *
  91. * @param string $key
  92. * @param mixed $default
  93. * @return mixed
  94. */
  95. public function value($key, $default = null)
  96. {
  97. if (array_key_exists($key, $this->attributes)) {
  98. return $this->attributes[$key];
  99. }
  100. return value($default);
  101. }
  102. /**
  103. * Get the value of the given key as a new Fluent instance.
  104. *
  105. * @param string $key
  106. * @param mixed $default
  107. * @return static
  108. */
  109. public function scope($key, $default = null)
  110. {
  111. return new static(
  112. (array) $this->get($key, $default)
  113. );
  114. }
  115. /**
  116. * Get all of the attributes from the fluent instance.
  117. *
  118. * @param mixed $keys
  119. * @return array
  120. */
  121. public function all($keys = null)
  122. {
  123. $data = $this->data();
  124. if (! $keys) {
  125. return $data;
  126. }
  127. $results = [];
  128. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  129. Arr::set($results, $key, Arr::get($data, $key));
  130. }
  131. return $results;
  132. }
  133. /**
  134. * Get data from the fluent instance.
  135. *
  136. * @param string|null $key
  137. * @param mixed $default
  138. * @return mixed
  139. */
  140. protected function data($key = null, $default = null)
  141. {
  142. return $this->get($key, $default);
  143. }
  144. /**
  145. * Get the attributes from the fluent instance.
  146. *
  147. * @return array<TKey, TValue>
  148. */
  149. public function getAttributes()
  150. {
  151. return $this->attributes;
  152. }
  153. /**
  154. * Convert the fluent instance to an array.
  155. *
  156. * @return array<TKey, TValue>
  157. */
  158. public function toArray()
  159. {
  160. return $this->attributes;
  161. }
  162. /**
  163. * Convert the object into something JSON serializable.
  164. *
  165. * @return array<TKey, TValue>
  166. */
  167. public function jsonSerialize(): array
  168. {
  169. return $this->toArray();
  170. }
  171. /**
  172. * Convert the fluent instance to JSON.
  173. *
  174. * @param int $options
  175. * @return string
  176. */
  177. public function toJson($options = 0)
  178. {
  179. return json_encode($this->jsonSerialize(), $options);
  180. }
  181. /**
  182. * Convert the fluent instance to pretty print formatted JSON.
  183. *
  184. * @param int $options
  185. * @return string
  186. */
  187. public function toPrettyJson(int $options = 0)
  188. {
  189. return $this->toJson(JSON_PRETTY_PRINT | $options);
  190. }
  191. /**
  192. * Determine if the fluent instance is empty.
  193. *
  194. * @return bool
  195. */
  196. public function isEmpty(): bool
  197. {
  198. return empty($this->attributes);
  199. }
  200. /**
  201. * Determine if the fluent instance is not empty.
  202. *
  203. * @return bool
  204. */
  205. public function isNotEmpty(): bool
  206. {
  207. return ! $this->isEmpty();
  208. }
  209. /**
  210. * Determine if the given offset exists.
  211. *
  212. * @param TKey $offset
  213. * @return bool
  214. */
  215. public function offsetExists($offset): bool
  216. {
  217. return isset($this->attributes[$offset]);
  218. }
  219. /**
  220. * Get the value for a given offset.
  221. *
  222. * @param TKey $offset
  223. * @return TValue|null
  224. */
  225. public function offsetGet($offset): mixed
  226. {
  227. return $this->value($offset);
  228. }
  229. /**
  230. * Set the value at the given offset.
  231. *
  232. * @param TKey $offset
  233. * @param TValue $value
  234. * @return void
  235. */
  236. public function offsetSet($offset, $value): void
  237. {
  238. $this->attributes[$offset] = $value;
  239. }
  240. /**
  241. * Unset the value at the given offset.
  242. *
  243. * @param TKey $offset
  244. * @return void
  245. */
  246. public function offsetUnset($offset): void
  247. {
  248. unset($this->attributes[$offset]);
  249. }
  250. /**
  251. * Get an iterator for the attributes.
  252. *
  253. * @return ArrayIterator<TKey, TValue>
  254. */
  255. public function getIterator(): Traversable
  256. {
  257. return new ArrayIterator($this->attributes);
  258. }
  259. /**
  260. * Handle dynamic calls to the fluent instance to set attributes.
  261. *
  262. * @param TKey $method
  263. * @param array{0: ?TValue} $parameters
  264. * @return $this
  265. */
  266. public function __call($method, $parameters)
  267. {
  268. if (static::hasMacro($method)) {
  269. return $this->macroCall($method, $parameters);
  270. }
  271. $this->attributes[$method] = count($parameters) > 0 ? array_first($parameters) : true;
  272. return $this;
  273. }
  274. /**
  275. * Dynamically retrieve the value of an attribute.
  276. *
  277. * @param TKey $key
  278. * @return TValue|null
  279. */
  280. public function __get($key)
  281. {
  282. return $this->value($key);
  283. }
  284. /**
  285. * Dynamically set the value of an attribute.
  286. *
  287. * @param TKey $key
  288. * @param TValue $value
  289. * @return void
  290. */
  291. public function __set($key, $value)
  292. {
  293. $this->offsetSet($key, $value);
  294. }
  295. /**
  296. * Dynamically check if an attribute is set.
  297. *
  298. * @param TKey $key
  299. * @return bool
  300. */
  301. public function __isset($key)
  302. {
  303. return $this->offsetExists($key);
  304. }
  305. /**
  306. * Dynamically unset an attribute.
  307. *
  308. * @param TKey $key
  309. * @return void
  310. */
  311. public function __unset($key)
  312. {
  313. $this->offsetUnset($key);
  314. }
  315. }