Throw an exception if a HBS helper receives wrong arguments set

This commit is contained in:
Dmitriy Simushev 2014-09-10 14:13:16 +00:00
parent 61ee2d0af6
commit ff72960382
17 changed files with 70 additions and 36 deletions

View File

@ -100,8 +100,10 @@ class AssetHelper implements HelperInterface, AssetUrlGeneratorAwareInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"asset" helper expects exactly one argument.'
);
} }
$relative_path = $context->get($parsed_args[0]); $relative_path = $context->get($parsed_args[0]);

View File

@ -42,8 +42,10 @@ class BlockHelper extends AbstractBlockHelper implements HelperInterface
{ {
// Get block name // Get block name
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"block" helper expects exactly one argument.'
);
} }
$block_name = $context->get(array_shift($parsed_args)); $block_name = $context->get(array_shift($parsed_args));

View File

@ -39,8 +39,10 @@ class CutStringHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args) || count($parsed_args) < 2) { if (count($parsed_args) != 2) {
return ''; throw new \InvalidArgumentException(
'"cutString" helper expects exactly two arguments.'
);
} }
$string = $context->get($parsed_args[0]); $string = $context->get($parsed_args[0]);

View File

@ -48,8 +48,10 @@ class ExtendsHelper implements HelperInterface
{ {
// Get name of the parent template // Get name of the parent template
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"extends" helper expects exactly one argument.'
);
} }
$parent_template = $context->get(array_shift($parsed_args)); $parent_template = $context->get(array_shift($parsed_args));

View File

@ -39,8 +39,10 @@ class FormatDateDiffHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"formatDateDiff" helper expects exactly one argument.'
);
} }
$seconds = intval($context->get($parsed_args[0])); $seconds = intval($context->get($parsed_args[0]));

View File

@ -39,8 +39,10 @@ class FormatDateHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"formatDate" helper expects exactly one argument.'
);
} }
$timestamp = intval($context->get($parsed_args[0])); $timestamp = intval($context->get($parsed_args[0]));

View File

@ -47,8 +47,10 @@ class GeneratePaginationHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args) || count($parsed_args) < 1) { if (count($parsed_args) < 1 || count($parsed_args) > 2) {
return ''; throw new \InvalidArgumentException(
'"generatePagination" helper expects one or two arguments.'
);
} }
$pagination_info = $context->get($parsed_args[0]); $pagination_info = $context->get($parsed_args[0]);

View File

@ -45,8 +45,10 @@ class IfAnyHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) == 0) {
return ''; throw new \InvalidArgumentException(
'"ifAny" helper expects at least one argument.'
);
} }
$condition = false; $condition = false;

View File

@ -43,8 +43,10 @@ class IfEqualHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args) || count($parsed_args) < 2) { if (count($parsed_args) != 2) {
return ''; throw new \InvalidArgumentException(
'"ifEqual" helper expects exactly two arguments.'
);
} }
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1])); $condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));

View File

@ -43,8 +43,10 @@ class IfEvenHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"ifEven" helper expects exactly one argument.'
);
} }
$condition = ($context->get($parsed_args[0]) % 2 == 0); $condition = ($context->get($parsed_args[0]) % 2 == 0);

View File

@ -44,8 +44,10 @@ class IfOddHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"ifOdd" helper expects exactly one argument.'
);
} }
$condition = ($context->get($parsed_args[0]) % 2 == 1); $condition = ($context->get($parsed_args[0]) % 2 == 1);

View File

@ -32,7 +32,7 @@ use Handlebars\Template;
* The block was overridden * The block was overridden
* {{else}} * {{else}}
* The block was not overridden * The block was not overridden
* {{/ifOverriden}} * {{/ifOverridden}}
* </code> * </code>
*/ */
class IfOverriddenHelper extends AbstractBlockHelper implements HelperInterface class IfOverriddenHelper extends AbstractBlockHelper implements HelperInterface
@ -44,8 +44,10 @@ class IfOverriddenHelper extends AbstractBlockHelper implements HelperInterface
{ {
// Get block name // Get block name
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"ifOverridden" helper expects exactly one argument.'
);
} }
$block_name = $context->get(array_shift($parsed_args)); $block_name = $context->get(array_shift($parsed_args));

View File

@ -45,8 +45,10 @@ class L10nHelper implements HelperInterface
{ {
// Check if there is at least one argument // Check if there is at least one argument
$parsed_arguments = $template->parseArguments($args); $parsed_arguments = $template->parseArguments($args);
if (empty($parsed_arguments)) { if (count($parsed_arguments) == 0) {
return ''; throw new \InvalidArgumentException(
'"l10n" helper expects at least one argument.'
);
} }
$text = $context->get(array_shift($parsed_arguments)); $text = $context->get(array_shift($parsed_arguments));

View File

@ -48,8 +48,10 @@ class OverrideHelper extends AbstractBlockHelper implements HelperInterface
{ {
// Get block name // Get block name
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"override" helper expects exactly one argument.'
);
} }
$block_name = $context->get(array_shift($parsed_args)); $block_name = $context->get(array_shift($parsed_args));

View File

@ -39,8 +39,10 @@ class RepeatHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"repeat" helper expects exactly one argument.'
);
} }
$times = intval($context->get($parsed_args[0])); $times = intval($context->get($parsed_args[0]));

View File

@ -39,8 +39,10 @@ class ReplaceHelper implements HelperInterface
public function execute(Template $template, Context $context, $args, $source) public function execute(Template $template, Context $context, $args, $source)
{ {
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args) || count($parsed_args) < 2) { if (count($parsed_args) != 2) {
return ''; throw new \InvalidArgumentException(
'"replace" helper expects exactly two arguments.'
);
} }
$search = $context->get($parsed_args[0]); $search = $context->get($parsed_args[0]);

View File

@ -32,7 +32,7 @@ use Handlebars\Template;
* The block was not overridden * The block was not overridden
* {{else}} * {{else}}
* The block was overridden * The block was overridden
* {{/unlessOverriden}} * {{/unlessOverridden}}
* </code> * </code>
*/ */
class UnlessOverriddenHelper extends AbstractBlockHelper implements HelperInterface class UnlessOverriddenHelper extends AbstractBlockHelper implements HelperInterface
@ -44,8 +44,10 @@ class UnlessOverriddenHelper extends AbstractBlockHelper implements HelperInterf
{ {
// Get block name // Get block name
$parsed_args = $template->parseArguments($args); $parsed_args = $template->parseArguments($args);
if (empty($parsed_args)) { if (count($parsed_args) != 1) {
return ''; throw new \InvalidArgumentException(
'"unlessOverridden" helper expects exactly one argument.'
);
} }
$block_name = $context->get(array_shift($parsed_args)); $block_name = $context->get(array_shift($parsed_args));