Skip to content

Commit

Permalink
fixed the in operator for objects that contains circular references (c…
Browse files Browse the repository at this point in the history
…loses twigphp#813)
  • Loading branch information
fabpot committed Aug 24, 2012
1 parent 37e7ba7 commit 738ae3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,16 +745,18 @@ function twig_sort_filter($array)
/* used internally */
function twig_in_filter($value, $compare)
{
$strict = is_object($value);

if (is_array($compare)) {
return in_array($value, $compare);
return in_array($value, $compare, $strict);
} elseif (is_string($compare)) {
if (!strlen((string) $value)) {
return empty($compare);
}

return false !== strpos($compare, (string) $value);
} elseif (is_object($compare) && $compare instanceof Traversable) {
return in_array($value, iterator_to_array($compare, false));
return in_array($value, iterator_to_array($compare, false), $strict);
}

return false;
Expand Down
19 changes: 19 additions & 0 deletions test/Twig/Tests/Fixtures/tests/in_with_objects.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Twig supports the in operator when using objects
--TEMPLATE--
{% if object in object_list %}
TRUE
{% endif %}
--DATA--
$foo = new Foo();
$foo1 = new Foo();

$foo->position = $foo1;
$foo1->position = $foo;

return array(
'object' => $foo,
'object_list' => array($foo1, $foo),
);
--EXPECT--
TRUE

0 comments on commit 738ae3b

Please sign in to comment.