composer package updates
[openemr.git] / vendor / illuminate / support / Fluent.php
blobc34a5d760dadeb3781a7a1c212fc10ac650489d4
1 <?php
3 namespace Illuminate\Support;
5 use ArrayAccess;
6 use JsonSerializable;
7 use Illuminate\Contracts\Support\Jsonable;
8 use Illuminate\Contracts\Support\Arrayable;
10 class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
12 /**
13 * All of the attributes set on the container.
15 * @var array
17 protected $attributes = [];
19 /**
20 * Create a new fluent container instance.
22 * @param array|object $attributes
23 * @return void
25 public function __construct($attributes = [])
27 foreach ($attributes as $key => $value) {
28 $this->attributes[$key] = $value;
32 /**
33 * Get an attribute from the container.
35 * @param string $key
36 * @param mixed $default
37 * @return mixed
39 public function get($key, $default = null)
41 if (array_key_exists($key, $this->attributes)) {
42 return $this->attributes[$key];
45 return value($default);
48 /**
49 * Get the attributes from the container.
51 * @return array
53 public function getAttributes()
55 return $this->attributes;
58 /**
59 * Convert the Fluent instance to an array.
61 * @return array
63 public function toArray()
65 return $this->attributes;
68 /**
69 * Convert the object into something JSON serializable.
71 * @return array
73 public function jsonSerialize()
75 return $this->toArray();
78 /**
79 * Convert the Fluent instance to JSON.
81 * @param int $options
82 * @return string
84 public function toJson($options = 0)
86 return json_encode($this->jsonSerialize(), $options);
89 /**
90 * Determine if the given offset exists.
92 * @param string $offset
93 * @return bool
95 public function offsetExists($offset)
97 return isset($this->attributes[$offset]);
101 * Get the value for a given offset.
103 * @param string $offset
104 * @return mixed
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
116 * @return void
118 public function offsetSet($offset, $value)
120 $this->attributes[$offset] = $value;
124 * Unset the value at the given offset.
126 * @param string $offset
127 * @return void
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
139 * @return $this
141 public function __call($method, $parameters)
143 $this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true;
145 return $this;
149 * Dynamically retrieve the value of an attribute.
151 * @param string $key
152 * @return mixed
154 public function __get($key)
156 return $this->get($key);
160 * Dynamically set the value of an attribute.
162 * @param string $key
163 * @param mixed $value
164 * @return void
166 public function __set($key, $value)
168 $this->offsetSet($key, $value);
172 * Dynamically check if an attribute is set.
174 * @param string $key
175 * @return bool
177 public function __isset($key)
179 return $this->offsetExists($key);
183 * Dynamically unset an attribute.
185 * @param string $key
186 * @return void
188 public function __unset($key)
190 $this->offsetUnset($key);