Fluent.php 4.0 KB

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