Skip to content

Commit

Permalink
Add __toBoolean methos
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Apr 6, 2020
1 parent fb51f7b commit f7f76ae
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/JsPhpize/Compiler/Helpers/Dot.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
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);
Expand Down
11 changes: 11 additions & 0 deletions src/JsPhpize/Compiler/Helpers/Dot.ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
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);
Expand Down
11 changes: 11 additions & 0 deletions src/JsPhpize/Compiler/Helpers/DotObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
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);
Expand Down
11 changes: 11 additions & 0 deletions src/JsPhpize/Compiler/Helpers/DotObject.ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
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);
Expand Down
11 changes: 11 additions & 0 deletions src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
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);
Expand Down
11 changes: 11 additions & 0 deletions src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
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);
Expand Down
4 changes: 4 additions & 0 deletions tests/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,19 @@ public function __get($name)

$result = $jsPhpize->renderCode('return obj.prop', $data);
$this->assertSame('', (string) $result);
$this->assertFalse($result->__toBoolean());

$result = $jsPhpize->renderCode('return obj.other', $data);
$this->assertSame('else', (string) $result);
$this->assertTrue($result->__toBoolean());

$result = $jsPhpize->renderCode('return obj.objectData.foo', $data);
$this->assertSame('o-bar', (string) $result);
$this->assertTrue($result->__toBoolean());

$result = $jsPhpize->renderCode('return obj.arrayData.foo', $data);
$this->assertSame('a-bar', (string) $result);
$this->assertTrue($result->__toBoolean());
}

public function testDollarVariablePrefix()
Expand Down

0 comments on commit f7f76ae

Please sign in to comment.