mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 18:36:53 +00:00
Added: Countable interface support for empty test
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user