From ebfe6495bbf65709d8865409d2013308365ebec8 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 1 Mar 2011 13:36:05 -0500 Subject: [PATCH] Clean up $options code. * Add a nice descriptive comment to the constructor. * Add test coverage for `_setOptions`. * Fix a couple of bugs exposed by said tests. --- Mustache.php | 28 ++++++++++++++++---- test/MustacheTest.php | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/Mustache.php b/Mustache.php index b9e3862..c6d5df9 100644 --- a/Mustache.php +++ b/Mustache.php @@ -112,6 +112,23 @@ class Mustache { * This method accepts a $template string and a $view object. Optionally, pass an associative * array of partials as well. * + * Passing an $options array allows overriding certain Mustache options during instantiation: + * + * $options = array( + * // `charset` -- must be supported by `htmlspecialentities()`. defaults to 'UTF-8' + * 'charset' => 'ISO-8859-1', + * + * // opening and closing delimiters, as an array or a space-separated string + * 'delimiters' => '<% %>', + * + * // an array of pragmas to enable + * 'pragmas' => array( + * Mustache::PRAGMA_UNESCAPED, + * Mustache::PRAGMA_DOT_NOTATION, + * Mustache::PRAGMA_IMPLICIT_ITERATOR + * ), + * ); + * * @access public * @param string $template (default: null) * @param mixed $view (default: null) @@ -127,7 +144,7 @@ class Mustache { } /** - * _setOptions function. + * Helper function for setting options from constructor args. * * @access protected * @param array $options @@ -143,19 +160,20 @@ class Mustache { if (!is_array($delims)) { $delims = array_map('trim', explode(' ', $delims, 2)); } - $this->_otag = preg_quote($delims[0]); - $this->_ctag = preg_quote($delims[1]); + $this->_otag = $delims[0]; + $this->_ctag = $delims[1]; } if (isset($options['pragmas'])) { - foreach ($pragmas as $pragma_name) { + foreach ($options['pragmas'] as $pragma_name) { if (!in_array($pragma_name, $this->_pragmasImplemented)) { throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); } } - $this->_pragmas = $pragmas; + $this->_pragmas = $options['pragmas']; } } + /** * Mustache class clone method. * diff --git a/test/MustacheTest.php b/test/MustacheTest.php index 21714a7..55d5204 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -72,6 +72,54 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals($output, $m4->render($template)); } + /** + * @dataProvider constructorOptions + */ + public function testConstructorOptions($options, $charset, $delimiters, $pragmas) { + $mustache = new MustacheExposedOptionsStub(null, null, null, $options); + $this->assertEquals($charset, $mustache->getCharset()); + $this->assertEquals($delimiters, $mustache->getDelimiters()); + $this->assertEquals($pragmas, $mustache->getPragmas()); + } + + public function constructorOptions() { + return array( + array( + array(), + 'UTF-8', + array('{{', '}}'), + array(), + ), + array( + array( + 'charset' => 'UTF-8', + 'delimiters' => '<< >>', + 'pragmas' => array(Mustache::PRAGMA_UNESCAPED) + ), + 'UTF-8', + array('<<', '>>'), + array(Mustache::PRAGMA_UNESCAPED), + ), + array( + array( + 'charset' => 'cp866', + 'delimiters' => array('[[[[', ']]]]'), + 'pragmas' => array(Mustache::PRAGMA_DOT_NOTATION, Mustache::PRAGMA_IMPLICIT_ITERATOR) + ), + 'cp866', + array('[[[[', ']]]]'), + array(Mustache::PRAGMA_DOT_NOTATION, Mustache::PRAGMA_IMPLICIT_ITERATOR), + ), + ); + } + + /** + * @expectedException MustacheException + */ + public function testConstructorInvalidPragmaOptionsThrowExceptions() { + $mustache = new Mustache(null, null, null, array('pragmas' => array('banana phone'))); + } + /** * Test __toString() function. * @@ -366,4 +414,16 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $m = new Mustache($template, $view); $this->assertEquals('{{win}}', $m->render()); } +} + +class MustacheExposedOptionsStub extends Mustache { + public function getPragmas() { + return $this->_pragmas; + } + public function getCharset() { + return $this->_charset; + } + public function getDelimiters() { + return array($this->_otag, $this->_ctag); + } } \ No newline at end of file