mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-22 13:41:27 +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;
|
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||||
|
|
||||||
use Handlebars\Context;
|
|
||||||
use Handlebars\Helper as HelperInterface;
|
use Handlebars\Helper as HelperInterface;
|
||||||
use Handlebars\StringWrapper;
|
use Handlebars\StringWrapper;
|
||||||
use Handlebars\Template;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional helper that checks if at least one argumet can be treated as
|
* 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>
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
*/
|
*/
|
||||||
class IfAnyHelper implements HelperInterface
|
class IfAnyHelper extends AbstractComparisonHelper implements HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function execute(Template $template, Context $context, $args, $source)
|
protected function evaluateCondition($args)
|
||||||
{
|
{
|
||||||
$parsed_args = $template->parseArguments($args);
|
if (count($args) == 0) {
|
||||||
if (count($parsed_args) == 0) {
|
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'"ifAny" helper expects at least one argument.'
|
'"ifAny" helper expects at least one argument.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$condition = false;
|
foreach ($args as $value) {
|
||||||
foreach ($parsed_args as $parsed_arg) {
|
|
||||||
$value = $context->get($parsed_arg);
|
|
||||||
|
|
||||||
if ($value instanceof StringWrapper) {
|
if ($value instanceof StringWrapper) {
|
||||||
// Casting any object of \Handlebars\StringWrapper will have
|
// Casting any object of \Handlebars\StringWrapper will have
|
||||||
// false positive result even for those with empty internal
|
// false positive result even for those with empty internal
|
||||||
@ -57,22 +51,10 @@ class IfAnyHelper implements HelperInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($value) {
|
if ($value) {
|
||||||
$condition = true;
|
return true;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($condition) {
|
return false;
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
|
|
||||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||||
|
|
||||||
use Handlebars\Context;
|
|
||||||
use Handlebars\Helper as HelperInterface;
|
use Handlebars\Helper as HelperInterface;
|
||||||
use Handlebars\Template;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional helper that checks if two values are equal or not.
|
* Conditional helper that checks if two values are equal or not.
|
||||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
|||||||
*
|
*
|
||||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
*/
|
*/
|
||||||
class IfEqualHelper implements HelperInterface
|
class IfEqualHelper extends AbstractComparisonHelper implements HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function execute(Template $template, Context $context, $args, $source)
|
protected function evaluateCondition($args)
|
||||||
{
|
{
|
||||||
$parsed_args = $template->parseArguments($args);
|
if (count($args) != 2) {
|
||||||
if (count($parsed_args) != 2) {
|
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'"ifEqual" helper expects exactly two arguments.'
|
'"ifEqual" helper expects exactly two arguments.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
return ($args[0] == $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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
|
|
||||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||||
|
|
||||||
use Handlebars\Context;
|
|
||||||
use Handlebars\Helper as HelperInterface;
|
use Handlebars\Helper as HelperInterface;
|
||||||
use Handlebars\Template;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional helper that checks if specified argument is even or not.
|
* Conditional helper that checks if specified argument is even or not.
|
||||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
|||||||
*
|
*
|
||||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
*/
|
*/
|
||||||
class IfEvenHelper implements HelperInterface
|
class IfEvenHelper extends AbstractComparisonHelper implements HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function execute(Template $template, Context $context, $args, $source)
|
protected function evaluateCondition($args)
|
||||||
{
|
{
|
||||||
$parsed_args = $template->parseArguments($args);
|
if (count($args) != 1) {
|
||||||
if (count($parsed_args) != 1) {
|
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'"ifEven" helper expects exactly one argument.'
|
'"ifEven" helper expects exactly one argument.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$condition = ($context->get($parsed_args[0]) % 2 == 0);
|
return ($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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
|
|
||||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||||
|
|
||||||
use Handlebars\Context;
|
|
||||||
use Handlebars\Helper as HelperInterface;
|
use Handlebars\Helper as HelperInterface;
|
||||||
use Handlebars\Template;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional helper that checks if specified argument is odd or not.
|
* Conditional helper that checks if specified argument is odd or not.
|
||||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
|||||||
*
|
*
|
||||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
*/
|
*/
|
||||||
class IfOddHelper implements HelperInterface
|
class IfOddHelper extends AbstractComparisonHelper implements HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function execute(Template $template, Context $context, $args, $source)
|
protected function evaluateCondition($args)
|
||||||
{
|
{
|
||||||
$parsed_args = $template->parseArguments($args);
|
if (count($args) != 1) {
|
||||||
if (count($parsed_args) != 1) {
|
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'"ifOdd" helper expects exactly one argument.'
|
'"ifOdd" helper expects exactly one argument.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$condition = ($context->get($parsed_args[0]) % 2 == 1);
|
return ($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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
|
|
||||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||||
|
|
||||||
use Handlebars\Context;
|
|
||||||
use Handlebars\Helper as HelperInterface;
|
use Handlebars\Helper as HelperInterface;
|
||||||
use Handlebars\Template;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional helper that checks if two values are equal or not.
|
* Conditional helper that checks if two values are equal or not.
|
||||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
|||||||
*
|
*
|
||||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
*/
|
*/
|
||||||
class UnlessEqualHelper implements HelperInterface
|
class UnlessEqualHelper extends AbstractComparisonHelper implements HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function execute(Template $template, Context $context, $args, $source)
|
protected function evaluateCondition($args)
|
||||||
{
|
{
|
||||||
$parsed_args = $template->parseArguments($args);
|
if (count($args) != 2) {
|
||||||
if (count($parsed_args) != 2) {
|
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'"unlessEqual" helper expects exactly two arguments.'
|
'"unlessEqual" helper expects exactly two arguments.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
return ($args[0] != $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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user