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