bug #3104 Fix the "empty" test on Traversable instances (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

Fix the "empty" test on Traversable instances

closes #3101

Commits
-------

b202fe38 fixed the "empty" test on Traversable instances
This commit is contained in:
Fabien Potencier
2019-08-07 17:07:43 +02:00
3 changed files with 17 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.42.3 (2019-XX-XX)
* fixed the "empty" test on Traversable instances
* fixed cache when opcache is installed but disabled
* 1.42.2 (2019-06-18)
+4
View File
@@ -1507,6 +1507,10 @@ function twig_test_empty($value)
return 0 == \count($value);
}
if ($value instanceof \Traversable) {
return !iterator_count($value);
}
if (\is_object($value) && method_exists($value, '__toString')) {
return '' === (string) $value;
}
+12
View File
@@ -15,6 +15,10 @@
{{ tostring_not_empty is empty ? 'ko' : 'ok' }}
{{ markup_empty is empty ? 'ok' : 'ko' }}
{{ markup_not_empty is empty ? 'ko' : 'ok' }}
{{ iterator is empty ? 'ko' : 'ok' }}
{{ empty_iterator is empty ? 'ok' : 'ko' }}
{{ callback_iterator is empty ? 'ko' : 'ok' }}
{{ empty_callback_iterator is empty ? 'ok' : 'ko' }}
--DATA--
return [
'string_empty' => '', 'string_zero' => '0',
@@ -24,6 +28,10 @@ return [
'countable_empty' => new \Twig\Tests\CountableStub([]), 'countable_not_empty' => new \Twig\Tests\CountableStub([1, 2]),
'tostring_empty' => new \Twig\Tests\ToStringStub(''), 'tostring_not_empty' => new \Twig\Tests\ToStringStub('0' /* edge case of using "0" as the string */),
'markup_empty' => new \Twig\Markup('', 'UTF-8'), 'markup_not_empty' => new \Twig\Markup('test', 'UTF-8'),
'iterator' => $iter = new \ArrayIterator(['bar', 'foo']),
'empty_iterator' => new \ArrayIterator(),
'callback_iterator' => new \CallbackFilterIterator($iter, function ($el) { return true; }),
'empty_callback_iterator' => new \CallbackFilterIterator($iter, function ($el) { return false; }),
]
--EXPECT--
ok
@@ -40,3 +48,7 @@ ok
ok
ok
ok
ok
ok
ok
ok