Add ifBetween helper

This commit is contained in:
Dmitriy Simushev 2016-08-17 05:46:29 +00:00
parent ca30d602f8
commit 2f9cf57e58
4 changed files with 143 additions and 0 deletions

View File

@ -32,6 +32,7 @@ class Helpers extends BaseHelpers
$this->add('ifOdd', new IfOddHelper());
$this->add('ifLess', new IfLessHelper());
$this->add('ifMore', new IfMoreHelper());
$this->add('ifBetween', new IfBetweenHelper());
$this->add('unlessEqual', new UnlessEqualHelper());
}
}

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of Handlebars.php Helpers Set
*
* (c) Dmitriy Simushev <simushevds@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JustBlackBird\HandlebarsHelpers\Comparison;
use Handlebars\Helper as HelperInterface;
/**
* Conditional helper that checks if one value is between two another values.
*
* Borders of the interval are excluded from comparison. That's why in cases
* where the value under comparision is equal to one of the borders the result of
* "between" operator is false.
*
* Example of usage:
* ```handlebars
* {{#ifBetween value leftBorder rightBorder}
* The first argument is between borders.
* {{else}}
* The first argument is not between the borders.
* {{/ifBetween}}
* ```
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfBetweenHelper extends AbstractComparisonHelper implements HelperInterface
{
/**
* {@inheritdoc}
*/
protected function evaluateCondition($args)
{
if (count($args) != 3) {
throw new \InvalidArgumentException(
'"ifBetween" helper expects exactly three arguments.'
);
}
return ($args[0] > $args[1]) && ($args[0] < $args[2]);
}
}

View File

@ -44,6 +44,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
array('ifLess', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfLessHelper'),
array('ifMore', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfMoreHelper'),
array('ifBetween', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenHelper'),
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
);
}

View File

@ -0,0 +1,93 @@
<?php
/*
* This file is part of Handlebars.php Helpers Set
*
* (c) Dmitriy Simushev <simushevds@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison;
use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenHelper;
/**
* Test class for "ifBetween" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfBetweenHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests conditions work as expected.
*
* @dataProvider conditionProvider
*/
public function testCondition($value, $left, $right, $is_between)
{
$helpers = new \Handlebars\Helpers(array('ifBetween' => new IfBetweenHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#ifBetween value left right}}true{{else}}false{{/ifBetween}}',
array(
'value' => $value,
'left' => $left,
'right' => $right,
)
),
$is_between ? 'true' : 'false'
);
}
/**
* A data provider for testCondition method.
*/
public function conditionProvider()
{
return array(
// The value is less than left border.
array(2, 10, 12, false),
// The value equals to the left border.
array(0, 0, 42, false),
// The value equals to the right border.
array(9, 0, 9, false),
// The value is more than the right border.
array(75, 10, 12, false),
// The value is between borders.
array(58, 11, 134, true),
);
}
/**
* Tests that exception is thrown if wrong number of arguments is used.
*
* @expectedException InvalidArgumentException
* @dataProvider wrongArgumentsProvider
*/
public function testArgumentsCount($template)
{
$helpers = new \Handlebars\Helpers(array('ifBetween' => new IfBetweenHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$engine->render($template, array());
}
/**
* A data provider for testArgumentsCount method.
*/
public function wrongArgumentsProvider()
{
return array(
// Not enough arguments
array('{{#ifBetween}}yes{{else}}no{{/ifBetween}}'),
// Still not enough arguments
array('{{#ifBetween 1}}yes{{else}}no{{/ifBetween}}'),
array('{{#ifBetween 1 2}}yes{{else}}no{{/ifBetween}}'),
// Too much arguments
array('{{#ifBetween 1 2 3 4}}yes{{else}}no{{/ifBetween}}'),
);
}
}