Forráskód Böngészése

Merge pull request #129 from bobthecow/feature/anchor-dot-context

Add "anchored dot notation"
Justin Hileman 10 éve
szülő
commit
72b9693912
2 módosított fájl, 49 hozzáadás és 1 törlés
  1. 6 1
      src/Mustache/Context.php
  2. 43 0
      test/Mustache/Test/ContextTest.php

+ 6 - 1
src/Mustache/Context.php

@@ -128,7 +128,12 @@ class Mustache_Context
     {
         $chunks = explode('.', $id);
         $first  = array_shift($chunks);
-        $value  = $this->findVariableInStack($first, $this->stack);
+
+        if ($first === '') {
+            $value = $this->last();
+        } else {
+            $value = $this->findVariableInStack($first, $this->stack);
+        }
 
         foreach ($chunks as $chunk) {
             if ($value === '') {

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

@@ -119,6 +119,49 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('win', $context->find('baz'), 'ArrayAccess stands alone');
         $this->assertEquals('win', $context->find('qux'), 'ArrayAccess beats private property');
     }
+
+    public function testAnchoredDotNotation()
+    {
+        $context = new Mustache_Context();
+
+        $a = array(
+            'name'   => 'a',
+            'number' => 1,
+        );
+
+        $b = array(
+            'number' => 2,
+            'child'  => array(
+                'name' => 'baby bee',
+            ),
+        );
+
+        $c = array(
+            'name' => 'cee',
+        );
+
+        $context->push($a);
+        $this->assertEquals('a', $context->find('name'));
+        $this->assertEquals('a', $context->findDot('.name'));
+        $this->assertEquals(1, $context->find('number'));
+        $this->assertEquals(1, $context->findDot('.number'));
+
+        $context->push($b);
+        $this->assertEquals('a', $context->find('name'));
+        $this->assertEquals(2, $context->find('number'));
+        $this->assertEquals('', $context->findDot('.name'));
+        $this->assertEquals(2, $context->findDot('.number'));
+        $this->assertEquals('baby bee', $context->findDot('child.name'));
+        $this->assertEquals('baby bee', $context->findDot('.child.name'));
+
+        $context->push($c);
+        $this->assertEquals('cee', $context->find('name'));
+        $this->assertEquals('cee', $context->findDot('.name'));
+        $this->assertEquals(2, $context->find('number'));
+        $this->assertEquals('', $context->findDot('.number'));
+        $this->assertEquals('baby bee', $context->findDot('child.name'));
+        $this->assertEquals('', $context->findDot('.child.name'));
+    }
 }
 
 class Mustache_Test_TestDummy