From 4b95621c0f723a9fe387a41a803bbe960b925a33 Mon Sep 17 00:00:00 2001 From: Max Romanovsky Date: Sun, 4 Sep 2011 15:13:42 +0300 Subject: [PATCH] Added: Countable interface support for empty test --- lib/Twig/Extension/Core.php | 7 +++++-- test/Twig/Tests/Fixtures/tests/empty.test | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f41e860..a415324 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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) *
  * {% if foo.attribute is sameas(false) %}
  *    the foo attribute really is the ``false`` PHP value
- * {% endif %} 
+ * {% endif %}
  * 
* * @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); } diff --git a/test/Twig/Tests/Fixtures/tests/empty.test b/test/Twig/Tests/Fixtures/tests/empty.test index 06065f0..23d91f6 100644 --- a/test/Twig/Tests/Fixtures/tests/empty.test +++ b/test/Twig/Tests/Fixtures/tests/empty.test @@ -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 \ No newline at end of file -- 1.7.2.5