From 738ae3b6f3f4be286ee70d63342cdc4092795cda Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 24 Aug 2012 15:49:46 +0200 Subject: [PATCH] fixed the in operator for objects that contains circular references (closes #813) --- lib/Twig/Extension/Core.php | 6 ++++-- .../Twig/Tests/Fixtures/tests/in_with_objects.test | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tests/in_with_objects.test diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6587f3e..c789350 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -745,8 +745,10 @@ 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); @@ -754,7 +756,7 @@ function twig_in_filter($value, $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; diff --git a/test/Twig/Tests/Fixtures/tests/in_with_objects.test b/test/Twig/Tests/Fixtures/tests/in_with_objects.test new file mode 100644 index 0000000..daeb1cb --- /dev/null +++ b/test/Twig/Tests/Fixtures/tests/in_with_objects.test @@ -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 -- 1.7.2.5