mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-14 09:44:11 +03:00
Reduce duplication in Comparison helpers
This commit is contained in:
parent
f4d674f479
commit
3b5e609f90
49
src/Comparison/AbstractComparisonHelper.php
Normal file
49
src/Comparison/AbstractComparisonHelper.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Contains base functionality for all helpers related with comparison.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
abstract class AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$resolved_args = array();
|
||||
foreach ($template->parseArguments($args) as $arg) {
|
||||
$resolved_args[] = $context->get($arg);
|
||||
}
|
||||
|
||||
if ($this->evaluateCondition($resolved_args)) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates condition based on helper's arguments.
|
||||
*
|
||||
* This is a template method which must be overriden in inherited helpers.
|
||||
*
|
||||
* @param array $args List of resolved arguments passed to the helper.
|
||||
* @return bool Indicates if the condition is true or false.
|
||||
*/
|
||||
abstract protected function evaluateCondition($args);
|
||||
}
|
@ -10,10 +10,8 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\StringWrapper;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if at least one argumet can be treated as
|
||||
@ -30,24 +28,20 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfAnyHelper implements HelperInterface
|
||||
class IfAnyHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) == 0) {
|
||||
if (count($args) == 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifAny" helper expects at least one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = false;
|
||||
foreach ($parsed_args as $parsed_arg) {
|
||||
$value = $context->get($parsed_arg);
|
||||
|
||||
foreach ($args as $value) {
|
||||
if ($value instanceof StringWrapper) {
|
||||
// Casting any object of \Handlebars\StringWrapper will have
|
||||
// false positive result even for those with empty internal
|
||||
@ -57,22 +51,10 @@ class IfAnyHelper implements HelperInterface
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
$condition = true;
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if two values are equal or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfEqualHelper implements HelperInterface
|
||||
class IfEqualHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
if (count($args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifEqual" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
return ($args[0] == $args[1]);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if specified argument is even or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfEvenHelper implements HelperInterface
|
||||
class IfEvenHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
if (count($args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifEven" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) % 2 == 0);
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
return ($args[0] % 2 == 0);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if specified argument is odd or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfOddHelper implements HelperInterface
|
||||
class IfOddHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
if (count($args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifOdd" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) % 2 == 1);
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
return ($args[0] % 2 == 1);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if two values are equal or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class UnlessEqualHelper implements HelperInterface
|
||||
class UnlessEqualHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
if (count($args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"unlessEqual" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
||||
|
||||
if (!$condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
return ($args[0] != $args[1]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user