Skip to content

Commit f7f76ae

Browse files
committed
Add __toBoolean methos
1 parent fb51f7b commit f7f76ae

File tree

7 files changed

+70
-0
lines changed

7 files changed

+70
-0
lines changed

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
164164
return (string) $this->getValue();
165165
}
166166

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+
167178
public function __invoke(...$arguments)
168179
{
169180
return call_user_func_array($this->getCallable(), $arguments);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
167167
return (string) $this->getValue();
168168
}
169169

170+
public function __toBoolean()
171+
{
172+
$value = $this->getValue();
173+
174+
if (method_exists($value, '__toBoolean')) {
175+
return $value->__toBoolean();
176+
}
177+
178+
return !!$value;
179+
}
180+
170181
public function __invoke(...$arguments)
171182
{
172183
return call_user_func_array($this->getCallable(), $arguments);

src/JsPhpize/Compiler/Helpers/DotObject.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
161161
return (string) $this->getValue();
162162
}
163163

164+
public function __toBoolean()
165+
{
166+
$value = $this->getValue();
167+
168+
if (method_exists($value, '__toBoolean')) {
169+
return $value->__toBoolean();
170+
}
171+
172+
return !!$value;
173+
}
174+
164175
public function __invoke(...$arguments)
165176
{
166177
return call_user_func_array($this->getCallable(), $arguments);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
164164
return (string) $this->getValue();
165165
}
166166

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+
167178
public function __invoke(...$arguments)
168179
{
169180
return call_user_func_array($this->getCallable(), $arguments);

src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
243243
return (string) $this->getValue();
244244
}
245245

246+
public function __toBoolean()
247+
{
248+
$value = $this->getValue();
249+
250+
if (method_exists($value, '__toBoolean')) {
251+
return $value->__toBoolean();
252+
}
253+
254+
return !!$value;
255+
}
256+
246257
public function __invoke(...$arguments)
247258
{
248259
return call_user_func_array($this->getCallable(), $arguments);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ if (!class_exists('JsPhpizeDotCarrier')) {
246246
return (string) $this->getValue();
247247
}
248248

249+
public function __toBoolean()
250+
{
251+
$value = $this->getValue();
252+
253+
if (method_exists($value, '__toBoolean')) {
254+
return $value->__toBoolean();
255+
}
256+
257+
return !!$value;
258+
}
259+
249260
public function __invoke(...$arguments)
250261
{
251262
return call_user_func_array($this->getCallable(), $arguments);

tests/render.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,19 @@ public function __get($name)
112112

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

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

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

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

126130
public function testDollarVariablePrefix()

0 commit comments

Comments
 (0)