mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
Merge pull request #75 from Mibew/named_arguments
Add named arguments parser
This commit is contained in:
commit
c6f3e0fd5e
@ -581,6 +581,49 @@ class Template
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Break an argument string into an array of named arguments
|
||||
*
|
||||
* @param string $string Argument String as passed to a helper
|
||||
*
|
||||
* @return array the argument list as an array
|
||||
*/
|
||||
public function parseNamedArguments($string)
|
||||
{
|
||||
$variableName = '(?:(?:[^\'"\[\]\s]|\[.+?\])+)';
|
||||
$escapedValue = '(?:(?<!\\\\)".*?(?<!\\\\)"|(?<!\\\\)\'.*?(?<!\\\\)\')';
|
||||
|
||||
// Get list of named arguemnts
|
||||
$matches = array();
|
||||
preg_match_all(
|
||||
'#(' . $variableName . ')\s*=\s*(' . $escapedValue . '|' . $variableName . ')#',
|
||||
$string,
|
||||
$matches,
|
||||
PREG_SET_ORDER
|
||||
);
|
||||
|
||||
$args = array();
|
||||
for ($x = 0, $c = count($matches); $x < $c; $x++) {
|
||||
$name = $matches[$x][1];
|
||||
$value = $matches[$x][2];
|
||||
|
||||
// Check if argument's name is a segment
|
||||
if ($name[0] == '[') {
|
||||
$name = substr($name, 1, -1);
|
||||
}
|
||||
|
||||
// Check if argument's value is a quoted string literal
|
||||
if ($value[0] == "'" || $value[0] == '"') {
|
||||
// Remove enclosing quotes and unescape
|
||||
$value = new \Handlebars\String(stripcslashes(substr($value, 1, -1)));
|
||||
}
|
||||
|
||||
$args[$name] = $value;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Break an argument string into an array of strings
|
||||
*
|
||||
|
@ -826,6 +826,38 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($args, $expected_array);
|
||||
}
|
||||
|
||||
public function namedArgumentParserProvider()
|
||||
{
|
||||
return array(
|
||||
array('arg1="value" arg2="value 2"', array('arg1' => 'value', 'arg2' => 'value 2')),
|
||||
array('arg1=var1', array('arg1' => 'var1')),
|
||||
array('[arg "1"]="arg number 1"', array('arg "1"' => "arg number 1")),
|
||||
array('arg1 = "value"', array('arg1' => "value")),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Named Argument Parser
|
||||
*
|
||||
* @param string $arg_string argument text
|
||||
* @param $expected_array
|
||||
*
|
||||
* @dataProvider namedArgumentParserProvider
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNamedArgumentsParser($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->parseNamedArguments($arg_string);
|
||||
$args = array_map(function ($a) {
|
||||
return (string)$a;
|
||||
}, $args);
|
||||
$this->assertEquals($args, $expected_array);
|
||||
}
|
||||
|
||||
public function stringLiteralInCustomHelperProvider()
|
||||
{
|
||||
return array(
|
||||
|
Loading…
Reference in New Issue
Block a user