3 namespace Illuminate\Support
;
7 class Optional
implements ArrayAccess
14 * The underlying object.
21 * Create a new optional instance.
26 public function __construct($value)
28 $this->value
= $value;
32 * Dynamically access a property on the underlying object.
37 public function __get($key)
39 if (is_object($this->value
)) {
40 return $this->value
->{$key};
45 * Dynamically pass a method to the underlying object.
47 * @param string $method
48 * @param array $parameters
51 public function __call($method, $parameters)
53 if (static::hasMacro($method)) {
54 return $this->macroCall($method, $parameters);
57 if (is_object($this->value
)) {
58 return $this->value
->{$method}(...$parameters);
63 * Determine if an item exists at an offset.
68 public function offsetExists($key)
70 return Arr
::accessible($this->value
) && Arr
::exists($this->value
, $key);
74 * Get an item at a given offset.
79 public function offsetGet($key)
81 return Arr
::get($this->value
, $key);
85 * Set the item at a given offset.
91 public function offsetSet($key, $value)
93 if (Arr
::accessible($this->value
)) {
94 $this->value
[$key] = $value;
99 * Unset the item at a given offset.
104 public function offsetUnset($key)
106 if (Arr
::accessible($this->value
)) {
107 unset($this->value
[$key]);