Skip to content

Commit 842bec3

Browse files
committed
Fix #33 Support in dot operator object that implements __get and __call but not __isset
Also fix BKWLD/laravel-pug#82
1 parent 335a870 commit 842bec3

File tree

7 files changed

+495
-6
lines changed

7 files changed

+495
-6
lines changed

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function ($base) {
66
};
77
$getCallable = function ($base, $key) use ($getFromArray) {
88
if (is_callable(array($base, $key))) {
9-
return array($base, $key);
9+
return new JsPhpizeDotCarrier(array($base, $key));
1010
}
1111
if ($base instanceof \ArrayAccess) {
1212
return $getFromArray($base, $key);
@@ -99,4 +99,79 @@ function ($base) {
9999
}
100100

101101
return $base;
102+
};
103+
104+
if (!class_exists('JsPhpizeDotCarrier')) {
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 __invoke(...$arguments)
168+
{
169+
return call_user_func_array($this->getCallable(), $arguments);
170+
}
171+
172+
private function isArrayAccessible()
173+
{
174+
return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]});
175+
}
176+
}
102177
}

src/JsPhpize/Compiler/Helpers/Dot.ref.h

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function (&$base) {
66
};
77
$getCallable = function (&$base, $key) use ($getFromArray) {
88
if (is_callable(array($base, $key))) {
9-
return array($base, $key);
9+
return new JsPhpizeDotCarrier(array($base, $key));
1010
}
1111
if ($base instanceof \ArrayAccess) {
1212
return $getFromArray($base, $key);
@@ -102,4 +102,79 @@ function (&$base) {
102102
}
103103

104104
return $result;
105+
};
106+
107+
if (!class_exists('JsPhpizeDotCarrier')) {
108+
class JsPhpizeDotCarrier extends ArrayObject
109+
{
110+
public function getValue()
111+
{
112+
if ($this->isArrayAccessible()) {
113+
return $this[0][$this[1]];
114+
}
115+
116+
return $this[0]->{$this[1]} ?? null;
117+
}
118+
119+
public function setValue($value)
120+
{
121+
if ($this->isArrayAccessible()) {
122+
$this[0][$this[1]] = $value;
123+
124+
return;
125+
}
126+
127+
$this[0]->{$this[1]} = $value;
128+
}
129+
130+
public function getCallable()
131+
{
132+
return $this->getArrayCopy();
133+
}
134+
135+
public function __isset($name)
136+
{
137+
$value = $this->getValue();
138+
139+
if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) {
140+
return true;
141+
}
142+
143+
return is_object($value) && isset($value->$name);
144+
}
145+
146+
public function __get($name)
147+
{
148+
return new self(array($this->getValue(), $name));
149+
}
150+
151+
public function __set($name, $value)
152+
{
153+
$value = $this->getValue();
154+
155+
if (is_array($value)) {
156+
$value[$name] = $value;
157+
$this->setValue($value);
158+
159+
return;
160+
}
161+
162+
$value->$name = $value;
163+
}
164+
165+
public function __toString()
166+
{
167+
return (string) $this->getValue();
168+
}
169+
170+
public function __invoke(...$arguments)
171+
{
172+
return call_user_func_array($this->getCallable(), $arguments);
173+
}
174+
175+
private function isArrayAccessible()
176+
{
177+
return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]});
178+
}
179+
}
105180
}

src/JsPhpize/Compiler/Helpers/DotObject.h

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function ($base) {
66
};
77
$getCallable = function ($base, $key) use ($getFromArray) {
88
if (is_callable(array($base, $key))) {
9-
return array($base, $key);
9+
return new JsPhpizeDotCarrier(array($base, $key));
1010
}
1111
if ($base instanceof \ArrayAccess) {
1212
return $getFromArray($base, $key);
@@ -96,4 +96,79 @@ function ($base) {
9696
}
9797

9898
return $base;
99+
};
100+
101+
if (!class_exists('JsPhpizeDotCarrier')) {
102+
class JsPhpizeDotCarrier extends ArrayObject
103+
{
104+
public function getValue()
105+
{
106+
if ($this->isArrayAccessible()) {
107+
return $this[0][$this[1]];
108+
}
109+
110+
return $this[0]->{$this[1]} ?? null;
111+
}
112+
113+
public function setValue($value)
114+
{
115+
if ($this->isArrayAccessible()) {
116+
$this[0][$this[1]] = $value;
117+
118+
return;
119+
}
120+
121+
$this[0]->{$this[1]} = $value;
122+
}
123+
124+
public function getCallable()
125+
{
126+
return $this->getArrayCopy();
127+
}
128+
129+
public function __isset($name)
130+
{
131+
$value = $this->getValue();
132+
133+
if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) {
134+
return true;
135+
}
136+
137+
return is_object($value) && isset($value->$name);
138+
}
139+
140+
public function __get($name)
141+
{
142+
return new self(array($this->getValue(), $name));
143+
}
144+
145+
public function __set($name, $value)
146+
{
147+
$value = $this->getValue();
148+
149+
if (is_array($value)) {
150+
$value[$name] = $value;
151+
$this->setValue($value);
152+
153+
return;
154+
}
155+
156+
$value->$name = $value;
157+
}
158+
159+
public function __toString()
160+
{
161+
return (string) $this->getValue();
162+
}
163+
164+
public function __invoke(...$arguments)
165+
{
166+
return call_user_func_array($this->getCallable(), $arguments);
167+
}
168+
169+
private function isArrayAccessible()
170+
{
171+
return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]});
172+
}
173+
}
99174
}

src/JsPhpize/Compiler/Helpers/DotObject.ref.h

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function (&$base) {
66
};
77
$getCallable = function (&$base, $key) use ($getFromArray) {
88
if (is_callable(array($base, $key))) {
9-
return array($base, $key);
9+
return new JsPhpizeDotCarrier(array($base, $key));
1010
}
1111
if ($base instanceof \ArrayAccess) {
1212
return $getFromArray($base, $key);
@@ -99,4 +99,79 @@ function (&$base) {
9999
}
100100

101101
return $result;
102+
};
103+
104+
if (!class_exists('JsPhpizeDotCarrier')) {
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 __invoke(...$arguments)
168+
{
169+
return call_user_func_array($this->getCallable(), $arguments);
170+
}
171+
172+
private function isArrayAccessible()
173+
{
174+
return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]});
175+
}
176+
}
102177
}

0 commit comments

Comments
 (0)