Fluent.php 3.8KB

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