Skip to content

Commit

Permalink
Objects with __toString magic method are valid string-typed values
Browse files Browse the repository at this point in the history
```
{% set foo %}Hello{% endset %}
{% types {foo: 'string'} %}
```
Actually, `$context['foo'] instanceof Twig\Markup`.
  • Loading branch information
drjayvee committed Dec 18, 2024
1 parent 303e066 commit 57debbf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Jeroen Versteeg"
}
],
"version": "1.0.7",
"version": "1.0.8",
"autoload": {
"psr-4": {
"AlisQI\\TwigQI\\": "src/"
Expand Down
2 changes: 1 addition & 1 deletion src/Assertion/AssertType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function matches($value, string $type): bool
}

return match ($type) {
'string' => is_string($value),
'string' => is_string($value) || (is_object($value) && method_exists($value, '__toString')),
'number' => is_int($value) || is_float($value),
'boolean' => is_bool($value),
'iterable' => is_iterable($value),
Expand Down
6 changes: 5 additions & 1 deletion tests/TypeAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace AlisQI\TwigQI\Tests;

use ArrayIterator;
use DateTime;
use Exception;
use Twig\Markup;
use Twig\Node\Node;

class TypeAssertionsTest extends AbstractTestCase
Expand Down Expand Up @@ -74,11 +76,13 @@ public static function getTypes(): array
['string', 'hello', true],
['string', '', true],
['string', '0', true],
['string', new Markup('hello', 'UTF-8'), true], // class Markup implements \Stringable
['string', new Exception(), true], // Exception has __toString()

['string', true, false],
['string', 1, false],
['string', [], false],
['string', new Exception(), false],
['string', new DateTime(), false],

['number', 0, true],
['number', 1, true],
Expand Down

0 comments on commit 57debbf

Please sign in to comment.