Traits
1 year ago
Arr.php
4 years ago
Carbon.php
4 years ago
Collection.php
1 year ago
Enumerable.php
4 years ago
HigherOrderCollectionProxy.php
4 years ago
HigherOrderTapProxy.php
4 years ago
HtmlString.php
4 years ago
LazyCollection.php
4 years ago
Optional.php
4 years ago
Pluralizer.php
4 years ago
Str.php
1 year ago
alias.php
4 years ago
helpers.php
4 years ago
Optional.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IlluminateAgnostic\Str\Support; |
| 4 | |
| 5 | use ArrayAccess; |
| 6 | use ArrayObject; |
| 7 | |
| 8 | class Optional implements ArrayAccess |
| 9 | { |
| 10 | use Traits\Macroable { |
| 11 | __call as macroCall; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * The underlying object. |
| 16 | * |
| 17 | * @var mixed |
| 18 | */ |
| 19 | protected $value; |
| 20 | |
| 21 | /** |
| 22 | * Create a new optional instance. |
| 23 | * |
| 24 | * @param mixed $value |
| 25 | * @return void |
| 26 | */ |
| 27 | public function __construct($value) |
| 28 | { |
| 29 | $this->value = $value; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Dynamically access a property on the underlying object. |
| 34 | * |
| 35 | * @param string $key |
| 36 | * @return mixed |
| 37 | */ |
| 38 | public function __get($key) |
| 39 | { |
| 40 | if (is_object($this->value)) { |
| 41 | return $this->value->{$key} ?? null; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Dynamically check a property exists on the underlying object. |
| 47 | * |
| 48 | * @param mixed $name |
| 49 | * @return bool |
| 50 | */ |
| 51 | public function __isset($name) |
| 52 | { |
| 53 | if (is_object($this->value)) { |
| 54 | return isset($this->value->{$name}); |
| 55 | } |
| 56 | |
| 57 | if (is_array($this->value) || $this->value instanceof ArrayObject) { |
| 58 | return isset($this->value[$name]); |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Determine if an item exists at an offset. |
| 66 | * |
| 67 | * @param mixed $key |
| 68 | * @return bool |
| 69 | */ |
| 70 | public function offsetExists($key) |
| 71 | { |
| 72 | return Arr::accessible($this->value) && Arr::exists($this->value, $key); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get an item at a given offset. |
| 77 | * |
| 78 | * @param mixed $key |
| 79 | * @return mixed |
| 80 | */ |
| 81 | public function offsetGet($key) |
| 82 | { |
| 83 | return Arr::get($this->value, $key); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Set the item at a given offset. |
| 88 | * |
| 89 | * @param mixed $key |
| 90 | * @param mixed $value |
| 91 | * @return void |
| 92 | */ |
| 93 | public function offsetSet($key, $value) |
| 94 | { |
| 95 | if (Arr::accessible($this->value)) { |
| 96 | $this->value[$key] = $value; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Unset the item at a given offset. |
| 102 | * |
| 103 | * @param string $key |
| 104 | * @return void |
| 105 | */ |
| 106 | public function offsetUnset($key) |
| 107 | { |
| 108 | if (Arr::accessible($this->value)) { |
| 109 | unset($this->value[$key]); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Dynamically pass a method to the underlying object. |
| 115 | * |
| 116 | * @param string $method |
| 117 | * @param array $parameters |
| 118 | * @return mixed |
| 119 | */ |
| 120 | public function __call($method, $parameters) |
| 121 | { |
| 122 | if (static::hasMacro($method)) { |
| 123 | return $this->macroCall($method, $parameters); |
| 124 | } |
| 125 | |
| 126 | if (is_object($this->value)) { |
| 127 | return $this->value->{$method}(...$parameters); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 |