PluginProbe
Ultimate Store Kit / 3.0.9
Ultimate Store Kit v3.0.9
3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 92 releases
ultimate-store-kit / base / support / optional.php

optional.php in Ultimate Store Kit 3.0.9, at base/support/optional.php

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