Fluent.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. * @params int $options
  185. *
  186. * @return string
  187. */
  188. public function toPrettyJson(int $options = 0)
  189. {
  190. return $this->toJson(JSON_PRETTY_PRINT | $options);
  191. }
  192. /**
  193. * Determine if the fluent instance is empty.
  194. *
  195. * @return bool
  196. */
  197. public function isEmpty(): bool
  198. {
  199. return empty($this->attributes);
  200. }
  201. /**
  202. * Determine if the fluent instance is not empty.
  203. *
  204. * @return bool
  205. */
  206. public function isNotEmpty(): bool
  207. {
  208. return ! $this->isEmpty();
  209. }
  210. /**
  211. * Determine if the given offset exists.
  212. *
  213. * @param TKey $offset
  214. * @return bool
  215. */
  216. public function offsetExists($offset): bool
  217. {
  218. return isset($this->attributes[$offset]);
  219. }
  220. /**
  221. * Get the value for a given offset.
  222. *
  223. * @param TKey $offset
  224. * @return TValue|null
  225. */
  226. public function offsetGet($offset): mixed
  227. {
  228. return $this->value($offset);
  229. }
  230. /**
  231. * Set the value at the given offset.
  232. *
  233. * @param TKey $offset
  234. * @param TValue $value
  235. * @return void
  236. */
  237. public function offsetSet($offset, $value): void
  238. {
  239. $this->attributes[$offset] = $value;
  240. }
  241. /**
  242. * Unset the value at the given offset.
  243. *
  244. * @param TKey $offset
  245. * @return void
  246. */
  247. public function offsetUnset($offset): void
  248. {
  249. unset($this->attributes[$offset]);
  250. }
  251. /**
  252. * Get an iterator for the attributes.
  253. *
  254. * @return ArrayIterator<TKey, TValue>
  255. */
  256. public function getIterator(): Traversable
  257. {
  258. return new ArrayIterator($this->attributes);
  259. }
  260. /**
  261. * Handle dynamic calls to the fluent instance to set attributes.
  262. *
  263. * @param TKey $method
  264. * @param array{0: ?TValue} $parameters
  265. * @return $this
  266. */
  267. public function __call($method, $parameters)
  268. {
  269. if (static::hasMacro($method)) {
  270. return $this->macroCall($method, $parameters);
  271. }
  272. $this->attributes[$method] = count($parameters) > 0 ? array_first($parameters) : true;
  273. return $this;
  274. }
  275. /**
  276. * Dynamically retrieve the value of an attribute.
  277. *
  278. * @param TKey $key
  279. * @return TValue|null
  280. */
  281. public function __get($key)
  282. {
  283. return $this->value($key);
  284. }
  285. /**
  286. * Dynamically set the value of an attribute.
  287. *
  288. * @param TKey $key
  289. * @param TValue $value
  290. * @return void
  291. */
  292. public function __set($key, $value)
  293. {
  294. $this->offsetSet($key, $value);
  295. }
  296. /**
  297. * Dynamically check if an attribute is set.
  298. *
  299. * @param TKey $key
  300. * @return bool
  301. */
  302. public function __isset($key)
  303. {
  304. return $this->offsetExists($key);
  305. }
  306. /**
  307. * Dynamically unset an attribute.
  308. *
  309. * @param TKey $key
  310. * @return void
  311. */
  312. public function __unset($key)
  313. {
  314. $this->offsetUnset($key);
  315. }
  316. }