Add "repeat" helper

This commit is contained in:
Dmitriy Simushev 2014-12-15 10:19:04 +00:00
parent 2b17e963c8
commit 8befb43846
6 changed files with 151 additions and 0 deletions

View File

@ -38,5 +38,6 @@ class Helpers extends BaseHelpers
// String helpers
$this->add('lowercase', new String\LowercaseHelper());
$this->add('uppercase', new String\UppercaseHelper());
$this->add('repeat', new String\RepeatHelper());
}
}

View File

@ -28,5 +28,6 @@ class Helpers extends BaseHelpers
$this->add('lowercase', new LowercaseHelper());
$this->add('uppercase', new UppercaseHelper());
$this->add('repeat', new RepeatHelper());
}
}

View File

@ -0,0 +1,55 @@
<?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\String;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
/**
* Repeats content specified number of times.
*
* Usage:
* ```handlebars
* {{#repeat times}}content to repeat{{/repeat}}
* ```
*
* Arguments:
* - "times": How many times content must be repeated. This value must be
* greater than or equal to 0 otherwise an exception will be thrown.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class RepeatHelper implements HelperInterface
{
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$parsed_args = $template->parseArguments($args);
if (count($parsed_args) != 1) {
throw new \InvalidArgumentException(
'"repeat" helper expects exactly one argument.'
);
}
$times = intval($context->get($parsed_args[0]));
if ($times < 0) {
throw new \InvalidArgumentException(
'The first argument has to be greater than or equal to 0.'
);
}
$string = $template->render($context);
return str_repeat($string, $times);
}
}

View File

@ -50,6 +50,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
// String helpers
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
);
}
}

View File

@ -40,6 +40,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
return array(
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
);
}
}

View File

@ -0,0 +1,92 @@
<?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\String;
use JustBlackBird\HandlebarsHelpers\String\RepeatHelper;
/**
* Test class for "repeat" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class RepeatHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests that strings are repeated properly.
*
* @dataProvider repeatProvider
*/
public function testRepeat($string, $times, $result)
{
$helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#repeat times}}{{str}}{{/repeat}}',
array('times' => $times, 'str' => $string)
),
$result
);
}
/**
* A data provider for testRepeat method.
*/
public function repeatProvider()
{
return array(
array('+', 0, ''),
array('+', 3, '+++'),
array('', 3, ''),
);
}
/**
* Tests that exception is thrown if wrong number of arguments is used.
*
* @expectedException InvalidArgumentException
* @dataProvider wrongArgumentsSetProvider
*/
public function testArgumentsCount($template)
{
$helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$engine->render($template, array());
}
/**
* A data provider for testArgumentsCount method.
*/
public function wrongArgumentsSetProvider()
{
return array(
// Not enough arguments
array('{{#repeat}}{{/repeat}}'),
// Too much arguments
array('{{#repeat 10 "ANOTHER ARG"}}{{/repeat}}'),
);
}
/**
* Tests that exception is thrown if arguments are invalid.
*
* @expectedException InvalidArgumentException
*/
public function testInvalidArguments()
{
$helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$engine->render('{{#repeat -10}}+{{/repeat}}', array());
}
}