From: Martin HasoĊˆ Date: Mon, 4 Aug 2014 05:09:00 +0000 (+0200) Subject: Fixed "in" operator X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9b6497f383514d21230e25e9491a07e8095bd24c;p=web%2Fkonrad%2Ftwig.git Fixed "in" operator --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 3092eb8..3a52ca3 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -893,15 +893,15 @@ function twig_sort_filter($array) function twig_in_filter($value, $compare) { if (is_array($compare)) { - return in_array($value, $compare, is_object($value)); + return in_array($value, $compare, true); } elseif (is_string($compare)) { - if (!strlen($value)) { - return empty($compare); + if (!is_string($value)) { + return false; } - return false !== strpos($compare, (string) $value); + return '' === $value || false !== strpos($compare, $value); } elseif ($compare instanceof Traversable) { - return in_array($value, iterator_to_array($compare, false), is_object($value)); + return in_array($value, iterator_to_array($compare, false), true); } return false; diff --git a/test/Twig/Tests/Fixtures/tests/in.test b/test/Twig/Tests/Fixtures/tests/in.test index 45c72fd..73445f8 100644 --- a/test/Twig/Tests/Fixtures/tests/in.test +++ b/test/Twig/Tests/Fixtures/tests/in.test @@ -33,8 +33,23 @@ TRUE {% if '0' in '0' %} TRUE {% endif %} +{{ false in [0, 1] ? 'TRUE' : 'FALSE' }} +{{ true in [0, 1] ? 'TRUE' : 'FALSE' }} +{{ '0' in [0, 1] ? 'TRUE' : 'FALSE' }} +{{ '' in [0, 1] ? 'TRUE' : 'FALSE' }} +{{ 0 in ['', 1] ? 'TRUE' : 'FALSE' }} +{{ '' in 'foo' ? 'TRUE' : 'FALSE' }} +{{ 0 in 'foo' ? 'TRUE' : 'FALSE' }} +{{ false in 'foo' ? 'TRUE' : 'FALSE' }} +{{ true in '100' ? 'TRUE' : 'FALSE' }} +{{ [] in 'Array' ? 'TRUE' : 'FALSE' }} +{{ [] in [true, false] ? 'TRUE' : 'FALSE' }} +{{ [] in [true, ''] ? 'TRUE' : 'FALSE' }} +{{ [] in [true, []] ? 'TRUE' : 'FALSE' }} +{{ dir_object in 'foo'~dir_name ? 'TRUE' : 'FALSE' }} +{{ 5 in 125 ? 'TRUE' : 'FALSE' }} --DATA-- -return array('bar' => 'bar', 'foo' => array('bar' => 'bar')) +return array('bar' => 'bar', 'foo' => array('bar' => 'bar'), 'dir_name' => dirname(__FILE__), 'dir_object' => new SplFileInfo(dirname(__FILE__))) --EXPECT-- TRUE TRUE @@ -45,4 +60,18 @@ TRUE TRUE TRUE TRUE +FALSE +FALSE +FALSE +FALSE +FALSE TRUE +FALSE +FALSE +FALSE +FALSE +FALSE +FALSE +TRUE +FALSE +FALSE