Skip to content

Commit

Permalink
Improve parseArgs (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Dec 20, 2024
1 parent 8640ac2 commit ffd55c4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
42 changes: 28 additions & 14 deletions src/Handlebars/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function helperIf(Template $template, Context $context, string $args, str
{
$tpl = $template->getEngine()->loadTemplate('{{#if ' . $args . '}}' . $source . '{{/if}}');
$tree = $tpl->getTree();
$tmp = $context->get($args);
$tmp = $this->parseArgs($context, $args)[0];
if ($tmp) {
$token = 'else';
foreach ($tree[0]['nodes'] as $node) {
Expand Down Expand Up @@ -277,7 +277,7 @@ private function renderElse(Template $template, Context $context): string
*/
public function helperUnless(Template $template, Context $context, string $args, string $source): string
{
$tmp = $context->get($args);
$tmp = $this->parseArgs($context, $args)[0];
if (!$tmp) {
$template->setStopToken('else');
$buffer = $template->render($context);
Expand Down Expand Up @@ -375,7 +375,6 @@ public function helperFormatDate(Template $template, Context $context, string $a

$date = $context->get($keyname);
if ($format) {
$dt = new DateTime;
if (is_numeric($date)) {
$dt = (new DateTime)->setTimestamp($date);
} else {
Expand Down Expand Up @@ -526,21 +525,36 @@ private function extractSlice(string $string): array
/**
* Parse a variable from current args
*/
private function parseArgs(Context $context, string $args): array
public static function parseArgs(Context $context, string $argStr): array
{
$args = preg_replace('/\s+/', ' ', trim($args));
$eles = explode(' ', $args);
foreach ($eles as $key => $ele) {
if (in_array(substr($ele, 0, 1), ['\'', '"'])) {
$val = trim($ele, '\'"');
} else if (is_numeric($ele)) {
$val = $ele;
$args = preg_split('/\s+/', trim($argStr));

// within strings, we don't want to replace multiple spaces with single space.
// if first char is single or double quote, but last character is not?
foreach ($args as $idx => $el) {
$firstChar = substr($el, 0, 1);

if ($firstChar === "'") {
$val = trim($el, "'");
} elseif ($firstChar === '"') {
$val = trim($el, '"');
} elseif ($el === 'null') {
$val = null;
} elseif ($el === 'true') {
$val = true;
} elseif ($el === 'false') {
$val = false;
} elseif (is_numeric($el)) {
$val = $el + 0;
} else {
$val = $context->get($ele);
/** @var scalar|null $val */
$val = $context->get($el);
}
$eles[$key] = $val;

$args[$idx] = $val;
}

return $eles;
return $args;
}
}
50 changes: 48 additions & 2 deletions tests/Handlebars/HandlebarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
class HandlebarsTest extends PHPUnit\Framework\TestCase
{
/**
* @dataProvider simpleTagdataProvider
* @dataProvider simpleTagDataProvider
*/
public function testBasicTags(string $src, array $data, string $result): void
{
$engine = new \Handlebars\Handlebars();
$this->assertSame($result, $engine->render($src, $data));
}

public function simpleTagdataProvider(): array
public function simpleTagDataProvider(): array
{
return array(
array(
Expand Down Expand Up @@ -64,11 +64,46 @@ public function internalHelpersDataProvider(): array
['data' => false],
''
],
[
'{{#if data}}Yes{{else if true}}Other{{/if}}',
['data' => false],
'Other'
],
[
'{{#if data}}Yes{{else if false}}False{{/if}}',
[],
''
],
[
'{{#if null}}Yes{{else if "0 items"}}String with space{{else}}No{{/if}}',
[],
'String with space'
],
[
'{{#if true}}Yes{{/if}}',
[],
'Yes'
],
[
'{{#unless data}}OK{{/unless}}',
['data' => false],
'OK'
],
[
'{{#unless data}}OK{{/unless}}',
[],
'OK'
],
[
'{{#unless false}}OK{{/unless}}',
[],
'OK'
],
[
'{{#unless true}}OK{{/unless}}',
[],
''
],
[
'{{#unless data}}OK {{else}}I believe{{/unless}}',
['data' => true],
Expand Down Expand Up @@ -373,4 +408,15 @@ public function testCustomHelper(): void
});
$this->assertEquals('Test helper is called', $engine->render('{{#test}}', []));
}

public function testParseArgs(): void
{
$context = new \Handlebars\Context(['status' => true, 'name' => 'John']);
$this->assertSame([true, false], \Handlebars\Helpers::parseArgs($context, 'status false'));
$this->assertSame([null, true], \Handlebars\Helpers::parseArgs($context, 'non_existent true'));
$this->assertSame(['John'], \Handlebars\Helpers::parseArgs($context, 'name'));
$this->assertSame([null, 'value"'], \Handlebars\Helpers::parseArgs($context, "null 'value\"'"));
$this->assertSame(["value'"], \Handlebars\Helpers::parseArgs($context, '"value\'"'));
$this->assertSame([12, 12.0], \Handlebars\Helpers::parseArgs($context, "12 12.0"));
}
}

0 comments on commit ffd55c4

Please sign in to comment.