Added: Countable interface support for empty test

This commit is contained in:
Max Romanovsky
2011-09-04 15:13:42 +03:00
parent cbbeb71e85
commit 4b95621c0f
2 changed files with 25 additions and 3 deletions
+5 -2
View File
@@ -426,7 +426,7 @@ function twig_reverse_filter($array)
/**
* Sorts an array.
*
*
* @param array $array An array
*/
function twig_sort_filter($array)
@@ -706,7 +706,7 @@ function twig_ensure_traversable($seq)
* <pre>
* {% if foo.attribute is sameas(false) %}
* the foo attribute really is the ``false`` PHP value
* {% endif %}
* {% endif %}
* </pre>
*
* @param mixed $value A PHP variable
@@ -839,5 +839,8 @@ function twig_test_defined($name, $context)
*/
function twig_test_empty($value)
{
if ($value instanceof Countable) {
return 0 == count($value);
}
return false === $value || (empty($value) && '0' != $value);
}
+20 -1
View File
@@ -7,12 +7,31 @@
{{ array is empty ? 'ok' : 'ko' }}
{{ zero is empty ? 'ok' : 'ko' }}
{{ string is empty ? 'ok' : 'ko' }}
{{ countable_empty is empty ? 'ok' : 'ko' }}
{{ countable_not_empty is empty ? 'ok' : 'ko' }}
--DATA--
return array('foo' => '', 'bar' => null, 'foobar' => false, 'array' => array(), 'zero' => 0, 'string' => '0');
class CountableStub implements Countable
{
private $items;
public function __construct(array $items)
{
$this->items = $items;
}
public function count()
{
return count($this->items);
}
}
return array('foo' => '', 'bar' => null, 'foobar' => false, 'array' => array(), 'zero' => 0, 'string' => '0', 'countable_empty' => new CountableStub(array()), 'countable_not_empty' => new CountableStub(array(1, 2)));
--EXPECT--
ok
ok
ok
ok
ko
ko
ok
ko