Quellcode durchsuchen

Made dot-notation pragma toggleable, added test cases.

Justin Hileman vor 15 Jahren
Ursprung
Commit
2ed23003e7
2 geänderte Dateien mit 67 neuen und 12 gelöschten Zeilen
  1. 33 12
      Mustache.php
  2. 34 0
      test/MustachePragmaDotNotationTest.php

+ 33 - 12
Mustache.php

@@ -25,6 +25,8 @@ class Mustache {
 	// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
 	protected $charset = 'UTF-8';
 
+	const PRAGMA_DOT_NOTATION = 'DOT-NOTATION';
+
 	protected $tagRegEx;
 
 	protected $template = '';
@@ -32,7 +34,9 @@ class Mustache {
 	protected $partials = array();
 	protected $pragmas  = array();
 
-	protected $pragmasImplemented = array('DOT-NOTATION');
+	protected $pragmasImplemented = array(
+		self::PRAGMA_DOT_NOTATION
+	);
 
 	/**
 	 * Mustache class constructor.
@@ -168,7 +172,7 @@ class Mustache {
 
 	/**
 	 * Initialize pragmas and remove all pragma tags.
-	 * 
+	 *
 	 * @access protected
 	 * @param string $template
 	 * @param array &$context
@@ -220,6 +224,20 @@ class Mustache {
 		return '';
 	}
 
+	protected function hasPragma($pragma_name) {
+		if (array_key_exists($pragma_name, $this->pragmas) && $this->pragmas[$pragma_name]) {
+			return true;
+		}
+	}
+
+	protected function getPragmaOptions($pragma_name) {
+		if (!$this->hasPragma()) {
+			throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
+		}
+
+		return $this->pragmas[$pragma_name];
+	}
+
 	/**
 	 * Loop through and render individual Mustache tags.
 	 *
@@ -408,17 +426,20 @@ class Mustache {
 	 * @return string
 	 */
 	protected function getVariable($tag_name, &$context) {
-		$chunks = explode('.', $tag_name);
-		$first = array_shift($chunks);
-
-		$ret = $this->_getVariable($first, $context);
-		while ($next = array_shift($chunks)) {
-			// Slice off a chunk of context for dot notation traversal.
-			$c = array($ret);
-			$ret = $this->_getVariable($next, $c);
+		if ($this->hasPragma(self::PRAGMA_DOT_NOTATION)) {
+			$chunks = explode('.', $tag_name);
+			$first = array_shift($chunks);
+
+			$ret = $this->_getVariable($first, $context);
+			while ($next = array_shift($chunks)) {
+				// Slice off a chunk of context for dot notation traversal.
+				$c = array($ret);
+				$ret = $this->_getVariable($next, $c);
+			}
+			return $ret;
+		} else {
+			return $this->_getVariable($tag_name, $context);
 		}
-
-		return $ret;
 	}
 
 	/**

+ 34 - 0
test/MustachePragmaDotNotationTest.php

@@ -0,0 +1,34 @@
+<?php
+
+require_once '../Mustache.php';
+require_once 'PHPUnit/Framework.php';
+
+class MustachePragmaDotNotationTest extends PHPUnit_Framework_TestCase {
+
+	public function testDotTraversal() {
+		$m = new Mustache('', array('foo' => array('bar' => 'this worked')));
+
+		$this->assertEquals($m->render('{{foo.bar}}'), '',
+			'Dot notation not enabled, variable should have been replaced with nothing');
+		$this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar}}'), 'this worked',
+			'Dot notation enabled, variable should have been replaced by "this worked"');
+	}
+
+	public function testDeepTraversal() {
+		$data = array(
+			'foo' => array('bar' => array('baz' => array('qux' => array('quux' => 'WIN!')))),
+			'a' => array('b' => array('c' => array('d' => array('e' => 'abcs')))),
+			'one' => array(
+				'one'   => 'one-one',
+				'two'   => 'one-two',
+				'three' => 'one-three',
+			),
+		);
+
+		$m = new Mustache('', $data);
+		$this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar.baz.qux.quux}}'), 'WIN!');
+		$this->assertEquals($m->render('{{%DOT-NOTATION}}{{a.b.c.d.e}}'), 'abcs');
+		$this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three');
+	}
+
+}