fixed the in operator for objects that contains circular references (closes #813)
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 24 Aug 2012 13:49:46 +0000 (15:49 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 24 Aug 2012 13:49:46 +0000 (15:49 +0200)
lib/Twig/Extension/Core.php
test/Twig/Tests/Fixtures/tests/in_with_objects.test [new file with mode: 0644]

index 6587f3e..c789350 100644 (file)
@@ -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 (file)
index 0000000..daeb1cb
--- /dev/null
@@ -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