@@ -4,17 +4,102 @@ function ($base) {
4
4
? $base [$key ]
5
5
: null ;
6
6
};
7
+
7
8
$getCallable = function ($base , $key ) use ($getFromArray ) {
8
9
if (is_callable (array ($base , $key ))) {
9
- return new JsPhpizeDotCarrier (array ($base, $key));
10
+ return new class (array ($base , $key )) extends \ArrayObject
11
+ {
12
+ public function getValue ()
13
+ {
14
+ if ($this -> isArrayAccessible ()) {
15
+ return $this [0 ][$this [1 ]];
16
+ }
17
+
18
+ return $this [0 ]-> {$this [1 ]} ?? null ;
19
+ }
20
+
21
+ public function setValue ($value )
22
+ {
23
+ if ($this -> isArrayAccessible ()) {
24
+ $this [0 ][$this [1 ]] = $value ;
25
+
26
+ return ;
27
+ }
28
+
29
+ $this [0 ]-> {$this [1 ]} = $value ;
30
+ }
31
+
32
+ public function getCallable ()
33
+ {
34
+ return $this -> getArrayCopy ();
35
+ }
36
+
37
+ public function __isset ($name )
38
+ {
39
+ $value = $this -> getValue ();
40
+
41
+ if ((is_array ($value ) || $value instanceof ArrayAccess ) && isset ($value [$name ])) {
42
+ return true;
43
+ }
44
+
45
+ return is_object ($value ) && isset ($value -> $name );
46
+ }
47
+
48
+ public function __get ($name )
49
+ {
50
+ return new self (array ($this -> getValue (), $name ));
51
+ }
52
+
53
+ public function __set ($name , $value )
54
+ {
55
+ $value = $this -> getValue ();
56
+
57
+ if (is_array ($value )) {
58
+ $value [$name ] = $value ;
59
+ $this -> setValue ($value );
60
+
61
+ return ;
62
+ }
63
+
64
+ $value -> $name = $value ;
65
+ }
66
+
67
+ public function __toString ()
68
+ {
69
+ return (string ) $this -> getValue ();
70
+ }
71
+
72
+ public function __toBoolean ()
73
+ {
74
+ $value = $this -> getValue ();
75
+
76
+ if (method_exists ($value , '__toBoolean' )) {
77
+ return $value -> __toBoolean ();
78
+ }
79
+
80
+ return !!$value ;
81
+ }
82
+
83
+ public function __invoke (...$arguments )
84
+ {
85
+ return call_user_func_array ($this -> getCallable (), $arguments );
86
+ }
87
+
88
+ private function isArrayAccessible ()
89
+ {
90
+ return is_array ($this [0 ]) || $this [0 ] instanceof ArrayAccess && !isset ($this [0 ]-> {$this [1 ]});
91
+ }
92
+ };
10
93
}
11
94
if ($base instanceof \ArrayAccess ) {
12
95
return $getFromArray ($base , $key );
13
96
}
14
97
};
98
+
15
99
$getRegExp = function ($value ) {
16
100
return is_object ($value ) && isset ($value -> isRegularExpression ) && $value -> isRegularExpression ? $value -> regExp . $value -> flags : null ;
17
101
};
102
+
18
103
$fallbackDot = function ($base , $key ) use ($getCallable , $getRegExp ) {
19
104
if (is_string ($base )) {
20
105
if (preg_match ('/^[-+]?\d+$/' , strval ($key ))) {
@@ -80,6 +165,7 @@ function ($base) {
80
165
81
166
return $getCallable ($base , $key );
82
167
};
168
+
83
169
foreach (array_slice (func_get_args (), 1 ) as $key ) {
84
170
$base = is_array ($base )
85
171
? $getFromArray ($base , $key )
@@ -100,89 +186,3 @@ function ($base) {
100
186
101
187
return $base ;
102
188
};
103
-
104
- if (!class_exists(JsPhpizeDotCarrier::class )) {
105
- class JsPhpizeDotCarrier extends \ArrayObject
106
- {
107
- public function getValue ()
108
- {
109
- if ($this ->isArrayAccessible ()) {
110
- return $this [0 ][$this [1 ]];
111
- }
112
-
113
- return $this [0 ]->{$this [1 ]} ?? null;
114
- }
115
-
116
- public function setValue ($value)
117
- {
118
- if ($this ->isArrayAccessible ()) {
119
- $this [0 ][$this [1 ]] = $value;
120
-
121
- return ;
122
- }
123
-
124
- $this [0 ]->{$this [1 ]} = $value;
125
- }
126
-
127
- public function getCallable ()
128
- {
129
- return $this ->getArrayCopy ();
130
- }
131
-
132
- public function __isset ($name)
133
- {
134
- $value = $this ->getValue ();
135
-
136
- if ((is_array ($value) || $value instanceof ArrayAccess) && isset ($value[$name])) {
137
- return true ;
138
- }
139
-
140
- return is_object ($value) && isset ($value->$name);
141
- }
142
-
143
- public function __get ($name)
144
- {
145
- return new self (array ($this ->getValue (), $name));
146
- }
147
-
148
- public function __set ($name, $value)
149
- {
150
- $value = $this ->getValue ();
151
-
152
- if (is_array ($value)) {
153
- $value[$name] = $value;
154
- $this ->setValue ($value);
155
-
156
- return ;
157
- }
158
-
159
- $value->$name = $value;
160
- }
161
-
162
- public function __toString ()
163
- {
164
- return (string) $this ->getValue ();
165
- }
166
-
167
- public function __toBoolean ()
168
- {
169
- $value = $this ->getValue ();
170
-
171
- if (method_exists ($value, ' __toBoolean' )) {
172
- return $value->__toBoolean ();
173
- }
174
-
175
- return !!$value;
176
- }
177
-
178
- public function __invoke (...$arguments)
179
- {
180
- return call_user_func_array ($this ->getCallable (), $arguments);
181
- }
182
-
183
- private function isArrayAccessible ()
184
- {
185
- return is_array ($this [0 ]) || $this [0 ] instanceof ArrayAccess && !isset ($this [0 ]->{$this [1 ]});
186
- }
187
- }
188
- }
0 commit comments