3 namespace Illuminate\Support
;
7 use Illuminate\Contracts\Support\Jsonable
;
8 use Illuminate\Contracts\Support\Arrayable
;
10 class Fluent
implements ArrayAccess
, Arrayable
, Jsonable
, JsonSerializable
13 * All of the attributes set on the container.
17 protected $attributes = [];
20 * Create a new fluent container instance.
22 * @param array|object $attributes
25 public function __construct($attributes = [])
27 foreach ($attributes as $key => $value) {
28 $this->attributes
[$key] = $value;
33 * Get an attribute from the container.
36 * @param mixed $default
39 public function get($key, $default = null)
41 if (array_key_exists($key, $this->attributes
)) {
42 return $this->attributes
[$key];
45 return value($default);
49 * Get the attributes from the container.
53 public function getAttributes()
55 return $this->attributes
;
59 * Convert the Fluent instance to an array.
63 public function toArray()
65 return $this->attributes
;
69 * Convert the object into something JSON serializable.
73 public function jsonSerialize()
75 return $this->toArray();
79 * Convert the Fluent instance to JSON.
84 public function toJson($options = 0)
86 return json_encode($this->jsonSerialize(), $options);
90 * Determine if the given offset exists.
92 * @param string $offset
95 public function offsetExists($offset)
97 return isset($this->attributes
[$offset]);
101 * Get the value for a given offset.
103 * @param string $offset
106 public function offsetGet($offset)
108 return $this->get($offset);
112 * Set the value at the given offset.
114 * @param string $offset
115 * @param mixed $value
118 public function offsetSet($offset, $value)
120 $this->attributes
[$offset] = $value;
124 * Unset the value at the given offset.
126 * @param string $offset
129 public function offsetUnset($offset)
131 unset($this->attributes
[$offset]);
135 * Handle dynamic calls to the container to set attributes.
137 * @param string $method
138 * @param array $parameters
141 public function __call($method, $parameters)
143 $this->attributes
[$method] = count($parameters) > 0 ?
$parameters[0] : true;
149 * Dynamically retrieve the value of an attribute.
154 public function __get($key)
156 return $this->get($key);
160 * Dynamically set the value of an attribute.
163 * @param mixed $value
166 public function __set($key, $value)
168 $this->offsetSet($key, $value);
172 * Dynamically check if an attribute is set.
177 public function __isset($key)
179 return $this->offsetExists($key);
183 * Dynamically unset an attribute.
188 public function __unset($key)
190 $this->offsetUnset($key);