Skip to content

Commit

Permalink
Fixed "in" operator
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Aug 5, 2014
1 parent 0c508d2 commit 9b6497f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 30 additions & 1 deletion test/Twig/Tests/Fixtures/tests/in.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,4 +60,18 @@ TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE

0 comments on commit 9b6497f

Please sign in to comment.