Skip to content

Commit 9600d41

Browse files
committed
Replace JsPhpizeDotCarrier with anonymous class to allow to nest in class declaration
1 parent 60a9bb3 commit 9600d41

File tree

7 files changed

+540
-522
lines changed

7 files changed

+540
-522
lines changed

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,102 @@ function ($base) {
44
? $base[$key]
55
: null;
66
};
7+
78
$getCallable = function ($base, $key) use ($getFromArray) {
89
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+
};
1093
}
1194
if ($base instanceof \ArrayAccess) {
1295
return $getFromArray($base, $key);
1396
}
1497
};
98+
1599
$getRegExp = function ($value) {
16100
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
17101
};
102+
18103
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
19104
if (is_string($base)) {
20105
if (preg_match('/^[-+]?\d+$/', strval($key))) {
@@ -80,6 +165,7 @@ function ($base) {
80165

81166
return $getCallable($base, $key);
82167
};
168+
83169
foreach (array_slice(func_get_args(), 1) as $key) {
84170
$base = is_array($base)
85171
? $getFromArray($base, $key)
@@ -100,89 +186,3 @@ function ($base) {
100186

101187
return $base;
102188
};
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

Comments
 (0)