From 49dcbb2573807b69bcb31a5ffad1ee2bb51a9118 Mon Sep 17 00:00:00 2001 From: Chris Gray Date: Mon, 20 Jan 2014 09:04:31 -0600 Subject: [PATCH] Changed Internal representation of quoted string literals to be an instance of \Handlebars\String instead of strangely quoted string. --- src/Handlebars/Context.php | 8 ++--- src/Handlebars/Template.php | 23 +++++++++++--- tests/Xamin/HandlebarsTest.php | 56 ++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 1fad2d2..66d8742 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -225,12 +225,8 @@ class Context $current = $this->lastIndex(); } elseif ($variableName == '@key') { $current = $this->lastKey(); - } elseif ($variableName[0] == "'" || $variableName[0] == '"') { - if ($variableName[0] == substr($variableName, -1) && strlen($variableName) > 2) { - $current = substr($variableName, 1, strlen($variableName) -2); - } else { - throw new \RuntimeException("Malformed string: ".$variableName); - } + } elseif ($variableName instanceof \Handlebars\String){ + $current = (string)$variableName; } else { $chunks = explode('.', $variableName); foreach ($chunks as $chunk) { diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 746253a..eb791b3 100755 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -430,10 +430,23 @@ class Template */ public function parseArguments($string) { - $parts = array(); - preg_match_all('#(? (object)array(1,2,3,4)), '' + ), + array( + '{{data.length}}', + array("data"=>array("length"=>"15 inches", "test","test","test")), + "15 inches" ) ); } @@ -472,5 +477,56 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase array('data.key.key'), ); } + + /** + * Test for proper handling of the length property + **/ + public function testArrayLengthEmulation(){ + + $data = array("numbers"=>array(1,2,3,4), + "object"=>(object)array("prop1"=>"val1", "prop2"=>"val2"), + "object_with_length_property"=>(object)array("length"=>"15cm") + ); + $context = new \Handlebars\Context($data); + // make sure we are getting the array length when given an array + $this->assertEquals($context->get("numbers.length"), 4); + // make sure we are not getting a length when given an object + $this->assertEmpty($context->get("object.length")); + // make sure we can still get the length property when given an object + $this->assertEquals($context->get("object_with_length_property.length"), "15cm"); + } + + public function argumentParserProvider(){ + return array( + array('arg1 arg2', array("arg1","arg2")), + array('"arg1 arg2"', array("arg1 arg2")), + array('arg1 arg2 "arg number 3"', array("arg1","arg2","arg number 3")), + array('arg1 "arg\"2" "\"arg3\""', array("arg1",'arg"2', '"arg3"')), + array("'arg1 arg2'", array("arg1 arg2")), + array("arg1 arg2 'arg number 3'", array("arg1","arg2","arg number 3")), + array('arg1 "arg\"2" "\\\'arg3\\\'"', array("arg1",'arg"2', "'arg3'")) + ); + } + /** + * Test Argument Parser + * + * @param string $arg_string argument text + * @param array $arg_ array data + * + * @dataProvider argumentParserProvider + * + * @return void + */ + public function testArgumentParser($arg_string, $expected_array){ + $engine = new \Handlebars\Handlebars(); + $template = new \Handlebars\Template($engine, null, null); + // get the string version of the arguments array + $args = $template->parseArguments($arg_string); + $args = array_map(function($a){ return (string)$a; }, $args); + $this->assertEquals($args, $expected_array); + + } + + } \ No newline at end of file