Эх сурвалжийг харах

Allow use of ArrayAccess objects in context.

Christian Würker 12 жил өмнө
parent
commit
0c85d19509

+ 11 - 6
src/Mustache/Context.php

@@ -133,17 +133,22 @@ class Mustache_Context
     private function findVariableInStack($id, array $stack)
     private function findVariableInStack($id, array $stack)
     {
     {
         for ($i = count($stack) - 1; $i >= 0; $i--) {
         for ($i = count($stack) - 1; $i >= 0; $i--) {
-            if (is_object($stack[$i]) && !$stack[$i] instanceof Closure) {
-                if (method_exists($stack[$i], $id)) {
-                    return $stack[$i]->$id();
-                } elseif (isset($stack[$i]->$id)) {
-                    return $stack[$i]->$id;
+            if (is_object($stack[$i])) {
+                if( $stack[$i] instanceof ArrayAccess) {
+                    if (isset($stack[$i][$id])) {
+                        return $stack[$i][$id];
+                    }
+                } elseif (!($stack[$i] instanceof Closure)) {
+                    if (method_exists($stack[$i], $id)) {
+                        return $stack[$i]->$id();
+                    } elseif (isset($stack[$i]->$id)) {
+                        return $stack[$i]->$id;
+                    }
                 }
                 }
             } elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) {
             } elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) {
                 return $stack[$i][$id];
                 return $stack[$i][$id];
             }
             }
         }
         }
-
         return '';
         return '';
     }
     }
 }
 }

+ 32 - 0
test/Mustache/Test/ContextTest.php

@@ -71,6 +71,8 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
 
 
         $string = 'some arbitrary string';
         $string = 'some arbitrary string';
 
 
+        $access = new Mustache_Test_TestArrayAccess($arr);
+
         $context->push($dummy);
         $context->push($dummy);
         $this->assertEquals('dummy', $context->find('name'));
         $this->assertEquals('dummy', $context->find('name'));
 
 
@@ -95,6 +97,11 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('see', $context->findDot('a.b.c'));
         $this->assertEquals('see', $context->findDot('a.b.c'));
         $this->assertEquals('<foo>', $context->find('foo'));
         $this->assertEquals('<foo>', $context->find('foo'));
         $this->assertEquals('<bar>', $context->findDot('bar'));
         $this->assertEquals('<bar>', $context->findDot('bar'));
+
+        $context = new Mustache_Context($arr);
+        $this->assertEquals('bee', $context->find('b'));
+        $this->assertEquals('see', $context->findDot('a.b.c'));
+        $this->assertEquals(null, $context->findDot('a.b.c.d'));
     }
     }
 }
 }
 
 
@@ -117,3 +124,28 @@ class Mustache_Test_TestDummy
         return '<bar>';
         return '<bar>';
     }
     }
 }
 }
+
+class Mustache_Test_TestArrayAccess  implements arrayaccess {
+    private $container = array();
+    public function __construct($array) {
+        foreach($array as $key => $value) {
+            $this->container[$key] = $value;
+        }
+    }
+    public function offsetSet($offset, $value) {
+        if (is_null($offset)) {
+            $this->container[] = $value;
+        } else {
+            $this->container[$offset] = $value;
+        }
+    }
+    public function offsetExists($offset) {
+        return isset($this->container[$offset]);
+    }
+    public function offsetUnset($offset) {
+        unset($this->container[$offset]);
+    }
+    public function offsetGet($offset) {
+        return isset($this->container[$offset]) ? $this->container[$offset] : null;
+    }
+}