Skip to content

Commit 69daae8

Browse files
passchntheodorejb
andauthored
Allow Stringable variables in if statements (#8)
* Allow \Stringable in Runtime::ifvar * Simplify ifvar tests Co-authored-by: Theodore Brown <[email protected]>
1 parent e81e06e commit 69daae8

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/Runtime.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ public static function lo(array $v): string
3737
/**
3838
* For {{#if}} and {{#unless}}.
3939
*
40-
* @param array<array<mixed>|string|int>|string|int|float|bool|null $v value to be tested
40+
* @param array<array<mixed>|string|int>|string|\Stringable|int|float|bool|null $v value to be tested
4141
* @param bool $zero include zero as true
4242
*
4343
* @return bool Return true when the value is not null nor false.
4444
*/
45-
public static function ifvar(mixed $v, bool $zero): bool
45+
public static function ifvar(mixed $v, bool $zero = false): bool
4646
{
47-
return $v !== null && $v !== false && ($zero || ($v !== 0 && $v !== 0.0)) && $v !== '' && (!is_array($v) || count($v) > 0);
47+
return $v !== null
48+
&& $v !== false
49+
&& ($zero || ($v !== 0 && $v !== 0.0))
50+
&& $v !== ''
51+
&& (!$v instanceof \Stringable || (string) $v !== '')
52+
&& (!is_array($v) || count($v) > 0);
4853
}
4954

5055
/**

tests/RuntimeTest.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ class RuntimeTest extends TestCase
1010
{
1111
public function testIfVar(): void
1212
{
13-
$this->assertFalse(Runtime::ifvar(null, false));
14-
$this->assertFalse(Runtime::ifvar(0, false));
13+
$this->assertFalse(Runtime::ifvar(null));
14+
$this->assertFalse(Runtime::ifvar(0));
1515
$this->assertTrue(Runtime::ifvar(0, true));
16-
$this->assertFalse(Runtime::ifvar(false, false));
17-
$this->assertTrue(Runtime::ifvar(true, false));
18-
$this->assertTrue(Runtime::ifvar(1, false));
19-
$this->assertFalse(Runtime::ifvar('', false));
20-
$this->assertTrue(Runtime::ifvar('0', false));
21-
$this->assertFalse(Runtime::ifvar([], false));
22-
$this->assertTrue(Runtime::ifvar([''], false));
23-
$this->assertTrue(Runtime::ifvar([0], false));
16+
$this->assertFalse(Runtime::ifvar(false));
17+
$this->assertTrue(Runtime::ifvar(true));
18+
$this->assertTrue(Runtime::ifvar(1));
19+
$this->assertFalse(Runtime::ifvar(''));
20+
$this->assertTrue(Runtime::ifvar('0'));
21+
$this->assertFalse(Runtime::ifvar([]));
22+
$this->assertTrue(Runtime::ifvar(['']));
23+
$this->assertTrue(Runtime::ifvar([0]));
24+
$this->assertFalse(Runtime::ifvar(self::createStringable('')));
25+
$this->assertTrue(Runtime::ifvar(self::createStringable('0')));
2426
}
2527

2628
public function testIsec(): void
@@ -41,4 +43,16 @@ public function testWi(): void
4143
$this->assertSame('{"a":"b"}', Runtime::wi($cx, ['a' => 'b'], null, ['a' => 'c'], function ($c, $i) {return json_encode($i); }));
4244
$this->assertSame('-b=', Runtime::wi($cx, 'b', null, ['a' => 'b'], function ($c, $i) {return "-$i="; }));
4345
}
46+
47+
private static function createStringable(string $value): \Stringable
48+
{
49+
return new class ($value) implements \Stringable {
50+
public function __construct(private string $value) {}
51+
52+
public function __toString(): string
53+
{
54+
return $this->value;
55+
}
56+
};
57+
}
4458
}

0 commit comments

Comments
 (0)