| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimateStoreKit\Base\Support; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
use ArrayAccess; |
| 10 |
use ArrayObject; |
| 11 |
|
| 12 |
class Optional implements ArrayAccess { |
| 13 |
/** |
| 14 |
* The underlying object. |
| 15 |
* |
| 16 |
* @var mixed |
| 17 |
*/ |
| 18 |
protected $value; |
| 19 |
|
| 20 |
/** |
| 21 |
* Create a new optional instance. |
| 22 |
* |
| 23 |
* @param mixed $value |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __construct($value) { |
| 27 |
$this->value = $value; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Dynamically access a property on the underlying object. |
| 32 |
* |
| 33 |
* @param string $key |
| 34 |
* @return mixed |
| 35 |
*/ |
| 36 |
public function __get($key) { |
| 37 |
if (is_object($this->value)) { |
| 38 |
return isset($this->value->{$key}) ? $this->value->{$key} : null; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Dynamically check a property exists on the underlying object. |
| 44 |
* |
| 45 |
* @param mixed $name |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public function __isset($name) { |
| 49 |
if (is_object($this->value)) { |
| 50 |
return isset($this->value->{$name}); |
| 51 |
} |
| 52 |
|
| 53 |
if (is_array($this->value) || $this->value instanceof ArrayObject) { |
| 54 |
return isset($this->value[$name]); |
| 55 |
} |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Determine if an item exists at an offset. |
| 62 |
* |
| 63 |
* @param mixed $key |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
#[\ReturnTypeWillChange] |
| 67 |
public function offsetExists($key): bool { |
| 68 |
return $this->accessible($this->value) && $this->exists($this->value, $key); |
| 69 |
} |
| 70 |
|
| 71 |
protected function accessible($value) { |
| 72 |
return is_array($value) || $value instanceof ArrayAccess; |
| 73 |
} |
| 74 |
|
| 75 |
protected function exists($array, $key) { |
| 76 |
if ($array instanceof ArrayAccess) { |
| 77 |
return $array->offsetExists($key); |
| 78 |
} |
| 79 |
|
| 80 |
return array_key_exists($key, $array); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get an item at a given offset. |
| 85 |
* |
| 86 |
* @param mixed $key |
| 87 |
* @return mixed |
| 88 |
*/ |
| 89 |
#[\ReturnTypeWillChange] |
| 90 |
public function offsetGet($key) { |
| 91 |
return $this->get($this->value, $key); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
protected function get($array, $key, $default = null) { |
| 96 |
if (!$this->accessible($array)) { |
| 97 |
return $this->value($default); |
| 98 |
} |
| 99 |
|
| 100 |
if (is_null($key)) { |
| 101 |
return $array; |
| 102 |
} |
| 103 |
|
| 104 |
if ($this->exists($array, $key)) { |
| 105 |
return $array[$key]; |
| 106 |
} |
| 107 |
|
| 108 |
if (strpos($key, '.') === false) { |
| 109 |
return isset($array[$key]) ? $array[$key] : $this->value($default); |
| 110 |
} |
| 111 |
|
| 112 |
foreach (explode('.', $key) as $segment) { |
| 113 |
if ($this->accessible($array) && $this->exists($array, $segment)) { |
| 114 |
$array = $array[$segment]; |
| 115 |
} else { |
| 116 |
return $this->value($default); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $array; |
| 121 |
} |
| 122 |
|
| 123 |
protected function value($value) { |
| 124 |
return $value instanceof \Closure ? $value() : $value; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Set the item at a given offset. |
| 129 |
* |
| 130 |
* @param mixed $key |
| 131 |
* @param mixed $value |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function offsetSet($key, $value): void { |
| 135 |
if ($this->accessible($this->value)) { |
| 136 |
$this->value[$key] = $value; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Unset the item at a given offset. |
| 142 |
* |
| 143 |
* @param string $key |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function offsetUnset($key): void { |
| 147 |
if ($this->accessible($this->value)) { |
| 148 |
unset($this->value[$key]); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|