PluginProbe
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder / 1.5.0
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder v1.5.0
3.1.4 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 All 93 releases
ultimate-store-kit / base / support / optional.php

optional.php in Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder 1.5.0, at base/support/optional.php

157 lines 3.4 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 /**
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 }
157