Skip to content

Commit

Permalink
Replace JsPhpizeDotCarrier with anonymous class to allow to nest in c…
Browse files Browse the repository at this point in the history
…lass declaration
  • Loading branch information
kylekatarnls committed Jul 12, 2020
1 parent 60a9bb3 commit 9600d41
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 522 deletions.
174 changes: 87 additions & 87 deletions src/JsPhpize/Compiler/Helpers/Dot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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)
Expand All @@ -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]});
}
}
}
Loading

0 comments on commit 9600d41

Please sign in to comment.