diff --git a/src/JsPhpize/Compiler/Helpers/Dot.h b/src/JsPhpize/Compiler/Helpers/Dot.h index 6ce7f82..2383e96 100644 --- a/src/JsPhpize/Compiler/Helpers/Dot.h +++ b/src/JsPhpize/Compiler/Helpers/Dot.h @@ -4,17 +4,102 @@ function ($base) { ? $base[$key] : null; }; + $getCallable = function ($base, $key) use ($getFromArray) { if (is_callable(array($base, $key))) { - return new JsPhpizeDotCarrier(array($base, $key)); + return new class(array($base, $key)) extends \ArrayObject + { + public function getValue() + { + if ($this->isArrayAccessible()) { + return $this[0][$this[1]]; + } + + return $this[0]->{$this[1]} ?? null; + } + + public function setValue($value) + { + if ($this->isArrayAccessible()) { + $this[0][$this[1]] = $value; + + return; + } + + $this[0]->{$this[1]} = $value; + } + + public function getCallable() + { + return $this->getArrayCopy(); + } + + public function __isset($name) + { + $value = $this->getValue(); + + if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { + return true; + } + + return is_object($value) && isset($value->$name); + } + + public function __get($name) + { + return new self(array($this->getValue(), $name)); + } + + public function __set($name, $value) + { + $value = $this->getValue(); + + if (is_array($value)) { + $value[$name] = $value; + $this->setValue($value); + + return; + } + + $value->$name = $value; + } + + public function __toString() + { + return (string) $this->getValue(); + } + + public function __toBoolean() + { + $value = $this->getValue(); + + if (method_exists($value, '__toBoolean')) { + return $value->__toBoolean(); + } + + return !!$value; + } + + public function __invoke(...$arguments) + { + return call_user_func_array($this->getCallable(), $arguments); + } + + private function isArrayAccessible() + { + return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); + } + }; } if ($base instanceof \ArrayAccess) { return $getFromArray($base, $key); } }; + $getRegExp = function ($value) { return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null; }; + $fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) { if (is_string($base)) { if (preg_match('/^[-+]?\d+$/', strval($key))) { @@ -80,6 +165,7 @@ function ($base) { return $getCallable($base, $key); }; + foreach (array_slice(func_get_args(), 1) as $key) { $base = is_array($base) ? $getFromArray($base, $key) @@ -100,89 +186,3 @@ function ($base) { return $base; }; - -if (!class_exists(JsPhpizeDotCarrier::class)) { - class JsPhpizeDotCarrier extends \ArrayObject - { - public function getValue() - { - if ($this->isArrayAccessible()) { - return $this[0][$this[1]]; - } - - return $this[0]->{$this[1]} ?? null; - } - - public function setValue($value) - { - if ($this->isArrayAccessible()) { - $this[0][$this[1]] = $value; - - return; - } - - $this[0]->{$this[1]} = $value; - } - - public function getCallable() - { - return $this->getArrayCopy(); - } - - public function __isset($name) - { - $value = $this->getValue(); - - if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { - return true; - } - - return is_object($value) && isset($value->$name); - } - - public function __get($name) - { - return new self(array($this->getValue(), $name)); - } - - public function __set($name, $value) - { - $value = $this->getValue(); - - if (is_array($value)) { - $value[$name] = $value; - $this->setValue($value); - - return; - } - - $value->$name = $value; - } - - public function __toString() - { - return (string) $this->getValue(); - } - - public function __toBoolean() - { - $value = $this->getValue(); - - if (method_exists($value, '__toBoolean')) { - return $value->__toBoolean(); - } - - return !!$value; - } - - public function __invoke(...$arguments) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - private function isArrayAccessible() - { - return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); - } - } -} diff --git a/src/JsPhpize/Compiler/Helpers/Dot.ref.h b/src/JsPhpize/Compiler/Helpers/Dot.ref.h index 391e70c..df3f7be 100644 --- a/src/JsPhpize/Compiler/Helpers/Dot.ref.h +++ b/src/JsPhpize/Compiler/Helpers/Dot.ref.h @@ -4,17 +4,102 @@ function (&$base) { ? $base[$key] : null; }; + $getCallable = function (&$base, $key) use ($getFromArray) { if (is_callable(array($base, $key))) { - return new JsPhpizeDotCarrier(array($base, $key)); + return new class(array($base, $key)) extends \ArrayObject + { + public function getValue() + { + if ($this->isArrayAccessible()) { + return $this[0][$this[1]]; + } + + return $this[0]->{$this[1]} ?? null; + } + + public function setValue($value) + { + if ($this->isArrayAccessible()) { + $this[0][$this[1]] = $value; + + return; + } + + $this[0]->{$this[1]} = $value; + } + + public function getCallable() + { + return $this->getArrayCopy(); + } + + public function __isset($name) + { + $value = $this->getValue(); + + if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { + return true; + } + + return is_object($value) && isset($value->$name); + } + + public function __get($name) + { + return new self(array($this->getValue(), $name)); + } + + public function __set($name, $value) + { + $value = $this->getValue(); + + if (is_array($value)) { + $value[$name] = $value; + $this->setValue($value); + + return; + } + + $value->$name = $value; + } + + public function __toString() + { + return (string) $this->getValue(); + } + + public function __toBoolean() + { + $value = $this->getValue(); + + if (method_exists($value, '__toBoolean')) { + return $value->__toBoolean(); + } + + return !!$value; + } + + public function __invoke(...$arguments) + { + return call_user_func_array($this->getCallable(), $arguments); + } + + private function isArrayAccessible() + { + return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); + } + }; } if ($base instanceof \ArrayAccess) { return $getFromArray($base, $key); } }; + $getRegExp = function ($value) { return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null; }; + $fallbackDot = function (&$base, $key) use ($getCallable, $getRegExp) { if (is_string($base)) { if (preg_match('/^[-+]?\d+$/', strval($key))) { @@ -80,6 +165,7 @@ function (&$base) { return $getCallable($base, $key); }; + $crawler = &$base; $result = $base; foreach (array_slice(func_get_args(), 1) as $key) { @@ -103,89 +189,3 @@ function (&$base) { return $result; }; - -if (!class_exists(JsPhpizeDotCarrier::class)) { - class JsPhpizeDotCarrier extends \ArrayObject - { - public function getValue() - { - if ($this->isArrayAccessible()) { - return $this[0][$this[1]]; - } - - return $this[0]->{$this[1]} ?? null; - } - - public function setValue($value) - { - if ($this->isArrayAccessible()) { - $this[0][$this[1]] = $value; - - return; - } - - $this[0]->{$this[1]} = $value; - } - - public function getCallable() - { - return $this->getArrayCopy(); - } - - public function __isset($name) - { - $value = $this->getValue(); - - if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { - return true; - } - - return is_object($value) && isset($value->$name); - } - - public function __get($name) - { - return new self(array($this->getValue(), $name)); - } - - public function __set($name, $value) - { - $value = $this->getValue(); - - if (is_array($value)) { - $value[$name] = $value; - $this->setValue($value); - - return; - } - - $value->$name = $value; - } - - public function __toString() - { - return (string) $this->getValue(); - } - - public function __toBoolean() - { - $value = $this->getValue(); - - if (method_exists($value, '__toBoolean')) { - return $value->__toBoolean(); - } - - return !!$value; - } - - public function __invoke(...$arguments) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - private function isArrayAccessible() - { - return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); - } - } -} diff --git a/src/JsPhpize/Compiler/Helpers/DotObject.h b/src/JsPhpize/Compiler/Helpers/DotObject.h index dfcc612..5dc9a34 100644 --- a/src/JsPhpize/Compiler/Helpers/DotObject.h +++ b/src/JsPhpize/Compiler/Helpers/DotObject.h @@ -4,17 +4,102 @@ function ($base) { ? $base[$key] : null; }; + $getCallable = function ($base, $key) use ($getFromArray) { if (is_callable(array($base, $key))) { - return new JsPhpizeDotCarrier(array($base, $key)); + return new class(array($base, $key)) extends \ArrayObject + { + public function getValue() + { + if ($this->isArrayAccessible()) { + return $this[0][$this[1]]; + } + + return $this[0]->{$this[1]} ?? null; + } + + public function setValue($value) + { + if ($this->isArrayAccessible()) { + $this[0][$this[1]] = $value; + + return; + } + + $this[0]->{$this[1]} = $value; + } + + public function getCallable() + { + return $this->getArrayCopy(); + } + + public function __isset($name) + { + $value = $this->getValue(); + + if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { + return true; + } + + return is_object($value) && isset($value->$name); + } + + public function __get($name) + { + return new self(array($this->getValue(), $name)); + } + + public function __set($name, $value) + { + $value = $this->getValue(); + + if (is_array($value)) { + $value[$name] = $value; + $this->setValue($value); + + return; + } + + $value->$name = $value; + } + + public function __toString() + { + return (string) $this->getValue(); + } + + public function __toBoolean() + { + $value = $this->getValue(); + + if (method_exists($value, '__toBoolean')) { + return $value->__toBoolean(); + } + + return !!$value; + } + + public function __invoke(...$arguments) + { + return call_user_func_array($this->getCallable(), $arguments); + } + + private function isArrayAccessible() + { + return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); + } + }; } if ($base instanceof \ArrayAccess) { return $getFromArray($base, $key); } }; + $getRegExp = function ($value) { return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null; }; + $fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) { if (is_string($base)) { if (preg_match('/^[-+]?\d+$/', strval($key))) { @@ -80,6 +165,7 @@ function ($base) { return $getCallable($base, $key); }; + foreach (array_slice(func_get_args(), 1) as $key) { $base = is_array($base) ? $getFromArray($base, $key) @@ -97,89 +183,3 @@ function ($base) { return $base; }; - -if (!class_exists(JsPhpizeDotCarrier::class)) { - class JsPhpizeDotCarrier extends \ArrayObject - { - public function getValue() - { - if ($this->isArrayAccessible()) { - return $this[0][$this[1]]; - } - - return $this[0]->{$this[1]} ?? null; - } - - public function setValue($value) - { - if ($this->isArrayAccessible()) { - $this[0][$this[1]] = $value; - - return; - } - - $this[0]->{$this[1]} = $value; - } - - public function getCallable() - { - return $this->getArrayCopy(); - } - - public function __isset($name) - { - $value = $this->getValue(); - - if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { - return true; - } - - return is_object($value) && isset($value->$name); - } - - public function __get($name) - { - return new self(array($this->getValue(), $name)); - } - - public function __set($name, $value) - { - $value = $this->getValue(); - - if (is_array($value)) { - $value[$name] = $value; - $this->setValue($value); - - return; - } - - $value->$name = $value; - } - - public function __toString() - { - return (string) $this->getValue(); - } - - public function __toBoolean() - { - $value = $this->getValue(); - - if (method_exists($value, '__toBoolean')) { - return $value->__toBoolean(); - } - - return !!$value; - } - - public function __invoke(...$arguments) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - private function isArrayAccessible() - { - return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); - } - } -} diff --git a/src/JsPhpize/Compiler/Helpers/DotObject.ref.h b/src/JsPhpize/Compiler/Helpers/DotObject.ref.h index 65f2f3a..ad9b432 100644 --- a/src/JsPhpize/Compiler/Helpers/DotObject.ref.h +++ b/src/JsPhpize/Compiler/Helpers/DotObject.ref.h @@ -4,17 +4,102 @@ function (&$base) { ? $base[$key] : null; }; + $getCallable = function (&$base, $key) use ($getFromArray) { if (is_callable(array($base, $key))) { - return new JsPhpizeDotCarrier(array($base, $key)); + return new class(array($base, $key)) extends \ArrayObject + { + public function getValue() + { + if ($this->isArrayAccessible()) { + return $this[0][$this[1]]; + } + + return $this[0]->{$this[1]} ?? null; + } + + public function setValue($value) + { + if ($this->isArrayAccessible()) { + $this[0][$this[1]] = $value; + + return; + } + + $this[0]->{$this[1]} = $value; + } + + public function getCallable() + { + return $this->getArrayCopy(); + } + + public function __isset($name) + { + $value = $this->getValue(); + + if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { + return true; + } + + return is_object($value) && isset($value->$name); + } + + public function __get($name) + { + return new self(array($this->getValue(), $name)); + } + + public function __set($name, $value) + { + $value = $this->getValue(); + + if (is_array($value)) { + $value[$name] = $value; + $this->setValue($value); + + return; + } + + $value->$name = $value; + } + + public function __toString() + { + return (string) $this->getValue(); + } + + public function __toBoolean() + { + $value = $this->getValue(); + + if (method_exists($value, '__toBoolean')) { + return $value->__toBoolean(); + } + + return !!$value; + } + + public function __invoke(...$arguments) + { + return call_user_func_array($this->getCallable(), $arguments); + } + + private function isArrayAccessible() + { + return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); + } + }; } if ($base instanceof \ArrayAccess) { return $getFromArray($base, $key); } }; + $getRegExp = function ($value) { return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null; }; + $fallbackDot = function (&$base, $key) use ($getCallable, $getRegExp) { if (is_string($base)) { if (preg_match('/^[-+]?\d+$/', strval($key))) { @@ -80,6 +165,7 @@ function (&$base) { return $getCallable($base, $key); }; + $crawler = &$base; $result = $base; foreach (array_slice(func_get_args(), 1) as $key) { @@ -100,89 +186,3 @@ function (&$base) { return $result; }; - -if (!class_exists(JsPhpizeDotCarrier::class)) { - class JsPhpizeDotCarrier extends \ArrayObject - { - public function getValue() - { - if ($this->isArrayAccessible()) { - return $this[0][$this[1]]; - } - - return $this[0]->{$this[1]} ?? null; - } - - public function setValue($value) - { - if ($this->isArrayAccessible()) { - $this[0][$this[1]] = $value; - - return; - } - - $this[0]->{$this[1]} = $value; - } - - public function getCallable() - { - return $this->getArrayCopy(); - } - - public function __isset($name) - { - $value = $this->getValue(); - - if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { - return true; - } - - return is_object($value) && isset($value->$name); - } - - public function __get($name) - { - return new self(array($this->getValue(), $name)); - } - - public function __set($name, $value) - { - $value = $this->getValue(); - - if (is_array($value)) { - $value[$name] = $value; - $this->setValue($value); - - return; - } - - $value->$name = $value; - } - - public function __toString() - { - return (string) $this->getValue(); - } - - public function __toBoolean() - { - $value = $this->getValue(); - - if (method_exists($value, '__toBoolean')) { - return $value->__toBoolean(); - } - - return !!$value; - } - - public function __invoke(...$arguments) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - private function isArrayAccessible() - { - return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); - } - } -} diff --git a/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h b/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h index 6e36bc5..e74100a 100644 --- a/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h +++ b/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h @@ -78,22 +78,108 @@ function ($base) { return null; }; + $getFromArray = function ($base, $key) use ($arrayPrototype) { return isset($base[$key]) ? $base[$key] : $arrayPrototype($base, $key); }; + $getCallable = function ($base, $key) use ($getFromArray) { if (is_callable(array($base, $key))) { - return new JsPhpizeDotCarrier(array($base, $key)); + return new class(array($base, $key)) extends \ArrayObject + { + public function getValue() + { + if ($this->isArrayAccessible()) { + return $this[0][$this[1]]; + } + + return $this[0]->{$this[1]} ?? null; + } + + public function setValue($value) + { + if ($this->isArrayAccessible()) { + $this[0][$this[1]] = $value; + + return; + } + + $this[0]->{$this[1]} = $value; + } + + public function getCallable() + { + return $this->getArrayCopy(); + } + + public function __isset($name) + { + $value = $this->getValue(); + + if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { + return true; + } + + return is_object($value) && isset($value->$name); + } + + public function __get($name) + { + return new self(array($this->getValue(), $name)); + } + + public function __set($name, $value) + { + $value = $this->getValue(); + + if (is_array($value)) { + $value[$name] = $value; + $this->setValue($value); + + return; + } + + $value->$name = $value; + } + + public function __toString() + { + return (string) $this->getValue(); + } + + public function __toBoolean() + { + $value = $this->getValue(); + + if (method_exists($value, '__toBoolean')) { + return $value->__toBoolean(); + } + + return !!$value; + } + + public function __invoke(...$arguments) + { + return call_user_func_array($this->getCallable(), $arguments); + } + + private function isArrayAccessible() + { + return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); + } + }; } if ($base instanceof \ArrayAccess) { return $getFromArray($base, $key); } }; + $getRegExp = function ($value) { return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null; }; + $fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) { if (is_string($base)) { if (preg_match('/^[-+]?\d+$/', strval($key))) { @@ -159,6 +245,7 @@ function ($base) { return $getCallable($base, $key); }; + foreach (array_slice(func_get_args(), 1) as $key) { $base = is_array($base) ? $getFromArray($base, $key) @@ -179,89 +266,3 @@ function ($base) { return $base; }; - -if (!class_exists(JsPhpizeDotCarrier::class)) { - class JsPhpizeDotCarrier extends \ArrayObject - { - public function getValue() - { - if ($this->isArrayAccessible()) { - return $this[0][$this[1]]; - } - - return $this[0]->{$this[1]} ?? null; - } - - public function setValue($value) - { - if ($this->isArrayAccessible()) { - $this[0][$this[1]] = $value; - - return; - } - - $this[0]->{$this[1]} = $value; - } - - public function getCallable() - { - return $this->getArrayCopy(); - } - - public function __isset($name) - { - $value = $this->getValue(); - - if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { - return true; - } - - return is_object($value) && isset($value->$name); - } - - public function __get($name) - { - return new self(array($this->getValue(), $name)); - } - - public function __set($name, $value) - { - $value = $this->getValue(); - - if (is_array($value)) { - $value[$name] = $value; - $this->setValue($value); - - return; - } - - $value->$name = $value; - } - - public function __toString() - { - return (string) $this->getValue(); - } - - public function __toBoolean() - { - $value = $this->getValue(); - - if (method_exists($value, '__toBoolean')) { - return $value->__toBoolean(); - } - - return !!$value; - } - - public function __invoke(...$arguments) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - private function isArrayAccessible() - { - return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); - } - } -} diff --git a/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.ref.h b/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.ref.h index c17c5ed..ec0d24b 100644 --- a/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.ref.h +++ b/src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.ref.h @@ -78,22 +78,108 @@ function (&$base) { return null; }; + $getFromArray = function (&$base, $key) use ($arrayPrototype) { return isset($base[$key]) ? $base[$key] : $arrayPrototype($base, $key); }; + $getCallable = function (&$base, $key) use ($getFromArray) { if (is_callable(array($base, $key))) { - return new JsPhpizeDotCarrier(array($base, $key)); + return new class(array($base, $key)) extends \ArrayObject + { + public function getValue() + { + if ($this->isArrayAccessible()) { + return $this[0][$this[1]]; + } + + return $this[0]->{$this[1]} ?? null; + } + + public function setValue($value) + { + if ($this->isArrayAccessible()) { + $this[0][$this[1]] = $value; + + return; + } + + $this[0]->{$this[1]} = $value; + } + + public function getCallable() + { + return $this->getArrayCopy(); + } + + public function __isset($name) + { + $value = $this->getValue(); + + if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { + return true; + } + + return is_object($value) && isset($value->$name); + } + + public function __get($name) + { + return new self(array($this->getValue(), $name)); + } + + public function __set($name, $value) + { + $value = $this->getValue(); + + if (is_array($value)) { + $value[$name] = $value; + $this->setValue($value); + + return; + } + + $value->$name = $value; + } + + public function __toString() + { + return (string) $this->getValue(); + } + + public function __toBoolean() + { + $value = $this->getValue(); + + if (method_exists($value, '__toBoolean')) { + return $value->__toBoolean(); + } + + return !!$value; + } + + public function __invoke(...$arguments) + { + return call_user_func_array($this->getCallable(), $arguments); + } + + private function isArrayAccessible() + { + return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); + } + }; } if ($base instanceof \ArrayAccess) { return $getFromArray($base, $key); } }; + $getRegExp = function ($value) { return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null; }; + $fallbackDot = function (&$base, $key) use ($getCallable, $getRegExp) { if (is_string($base)) { if (preg_match('/^[-+]?\d+$/', strval($key))) { @@ -159,6 +245,7 @@ function (&$base) { return $getCallable($base, $key); }; + $crawler = &$base; $result = $base; foreach (array_slice(func_get_args(), 1) as $key) { @@ -182,89 +269,3 @@ function (&$base) { return $result; }; - -if (!class_exists(JsPhpizeDotCarrier::class)) { - class JsPhpizeDotCarrier extends \ArrayObject - { - public function getValue() - { - if ($this->isArrayAccessible()) { - return $this[0][$this[1]]; - } - - return $this[0]->{$this[1]} ?? null; - } - - public function setValue($value) - { - if ($this->isArrayAccessible()) { - $this[0][$this[1]] = $value; - - return; - } - - $this[0]->{$this[1]} = $value; - } - - public function getCallable() - { - return $this->getArrayCopy(); - } - - public function __isset($name) - { - $value = $this->getValue(); - - if ((is_array($value) || $value instanceof ArrayAccess) && isset($value[$name])) { - return true; - } - - return is_object($value) && isset($value->$name); - } - - public function __get($name) - { - return new self(array($this->getValue(), $name)); - } - - public function __set($name, $value) - { - $value = $this->getValue(); - - if (is_array($value)) { - $value[$name] = $value; - $this->setValue($value); - - return; - } - - $value->$name = $value; - } - - public function __toString() - { - return (string) $this->getValue(); - } - - public function __toBoolean() - { - $value = $this->getValue(); - - if (method_exists($value, '__toBoolean')) { - return $value->__toBoolean(); - } - - return !!$value; - } - - public function __invoke(...$arguments) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - private function isArrayAccessible() - { - return is_array($this[0]) || $this[0] instanceof ArrayAccess && !isset($this[0]->{$this[1]}); - } - } -} diff --git a/tests/render.php b/tests/render.php index 5a4d663..df77b4f 100644 --- a/tests/render.php +++ b/tests/render.php @@ -153,4 +153,20 @@ public function testDollarVariablePrefix() $this->assertFalse($jsPhpize->renderCode($code, ['foo' => 'a'])); $this->assertTrue($jsPhpize->renderCode($code, ['foo' => 'a', 'bar' => ['foo' => 'x']])); } + + public function testClassDeclarationNesting() + { + $jsPhpize = new JsPhpize(); + $code = 'class Main { public function run() {' . + '$foo = ["bar" => "hello"];' . + $jsPhpize->compile('return foo.bar') . + '}} $main = new Main; echo $main->run();'; + + ob_start(); + eval($code); + $output = ob_get_contents(); + ob_end_clean(); + + $this->assertSame('hello', $output); + } }