Fluent.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.
  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. if (array_key_exists($key, $this->attributes)) {
  46. return $this->attributes[$key];
  47. }
  48. return value($default);
  49. }
  50. /**
  51. * Get the attributes from the fluent instance.
  52. *
  53. * @return array<TKey, TValue>
  54. */
  55. public function getAttributes()
  56. {
  57. return $this->attributes;
  58. }
  59. /**
  60. * Convert the fluent instance to an array.
  61. *
  62. * @return array<TKey, TValue>
  63. */
  64. public function toArray()
  65. {
  66. return $this->attributes;
  67. }
  68. /**
  69. * Convert the object into something JSON serializable.
  70. *
  71. * @return array<TKey, TValue>
  72. */
  73. public function jsonSerialize(): array
  74. {
  75. return $this->toArray();
  76. }
  77. /**
  78. * Convert the fluent instance to JSON.
  79. *
  80. * @param int $options
  81. * @return string
  82. */
  83. public function toJson($options = 0)
  84. {
  85. return json_encode($this->jsonSerialize(), $options);
  86. }
  87. /**
  88. * Determine if the given offset exists.
  89. *
  90. * @param TKey $offset
  91. * @return bool
  92. */
  93. public function offsetExists($offset): bool
  94. {
  95. return isset($this->attributes[$offset]);
  96. }
  97. /**
  98. * Get the value for a given offset.
  99. *
  100. * @param TKey $offset
  101. * @return TValue|null
  102. */
  103. public function offsetGet($offset): mixed
  104. {
  105. return $this->get($offset);
  106. }
  107. /**
  108. * Set the value at the given offset.
  109. *
  110. * @param TKey $offset
  111. * @param TValue $value
  112. * @return void
  113. */
  114. public function offsetSet($offset, $value): void
  115. {
  116. $this->attributes[$offset] = $value;
  117. }
  118. /**
  119. * Unset the value at the given offset.
  120. *
  121. * @param TKey $offset
  122. * @return void
  123. */
  124. public function offsetUnset($offset): void
  125. {
  126. unset($this->attributes[$offset]);
  127. }
  128. /**
  129. * Handle dynamic calls to the fluent instance to set attributes.
  130. *
  131. * @param TKey $method
  132. * @param array{0: ?TValue} $parameters
  133. * @return $this
  134. */
  135. public function __call($method, $parameters)
  136. {
  137. $this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
  138. return $this;
  139. }
  140. /**
  141. * Dynamically retrieve the value of an attribute.
  142. *
  143. * @param TKey $key
  144. * @return TValue|null
  145. */
  146. public function __get($key)
  147. {
  148. return $this->get($key);
  149. }
  150. /**
  151. * Dynamically set the value of an attribute.
  152. *
  153. * @param TKey $key
  154. * @param TValue $value
  155. * @return void
  156. */
  157. public function __set($key, $value)
  158. {
  159. $this->offsetSet($key, $value);
  160. }
  161. /**
  162. * Dynamically check if an attribute is set.
  163. *
  164. * @param TKey $key
  165. * @return bool
  166. */
  167. public function __isset($key)
  168. {
  169. return $this->offsetExists($key);
  170. }
  171. /**
  172. * Dynamically unset an attribute.
  173. *
  174. * @param TKey $key
  175. * @return void
  176. */
  177. public function __unset($key)
  178. {
  179. $this->offsetUnset($key);
  180. }
  181. }