mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-15 02:04:10 +03:00
Add ifBetweenRightClosed helper
This commit is contained in:
parent
e2f255cfa5
commit
5d1998558e
src/Comparison
tests/Comparison
@ -35,6 +35,7 @@ class Helpers extends BaseHelpers
|
||||
$this->add('ifBetween', new IfBetweenHelper());
|
||||
$this->add('ifBetweenClosed', new IfBetweenClosedHelper());
|
||||
$this->add('ifBetweenLeftClosed', new IfBetweenLeftClosedHelper());
|
||||
$this->add('ifBetweenRightClosed', new IfBetweenRightClosedHelper());
|
||||
$this->add('unlessEqual', new UnlessEqualHelper());
|
||||
}
|
||||
}
|
||||
|
49
src/Comparison/IfBetweenRightClosedHelper.php
Normal file
49
src/Comparison/IfBetweenRightClosedHelper.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?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.
|
||||
*
|
||||
* Only right border of the interval is included into comparison. That's why in
|
||||
* cases where the value under comparision is equal to right border the result
|
||||
* of "between left closed" operator is true but when the value under compatison
|
||||
* is equal to left border the result of "between left closed" is false.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifBetweenRightClosed value leftBorder rightBorder}}
|
||||
* The first argument is between borders.
|
||||
* {{else}}
|
||||
* The first argument is not between the borders.
|
||||
* {{/ifBetweenRightClosed}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenRightClosedHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifBetweenRightClosed" helper expects exactly three arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] > $args[1]) && ($args[0] <= $args[2]);
|
||||
}
|
||||
}
|
@ -47,6 +47,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
array('ifBetween', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenHelper'),
|
||||
array('ifBetweenClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenClosedHelper'),
|
||||
array('ifBetweenLeftClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenLeftClosedHelper'),
|
||||
array('ifBetweenRightClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenRightClosedHelper'),
|
||||
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
|
||||
);
|
||||
}
|
||||
|
93
tests/Comparison/IfBetweenRightClosedHelperTest.php
Normal file
93
tests/Comparison/IfBetweenRightClosedHelperTest.php
Normal 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\IfBetweenRightClosedHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifBetweenRightClosed" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenRightClosedHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $left, $right, $is_between)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenRightClosed' => new IfBetweenRightClosedHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifBetweenRightClosed value left right}}true{{else}}false{{/ifBetweenRightClosed}}',
|
||||
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, true),
|
||||
// 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('ifBetweenRightClosed' => new IfBetweenRightClosedHelper()));
|
||||
$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('{{#ifBetweenRightClosed}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifBetweenRightClosed 1}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
array('{{#ifBetweenRightClosed 1 2}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifBetweenRightClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user