mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-14 09:44:11 +03:00
Add "count" helper
This commit is contained in:
parent
76aa2509e8
commit
fe2d21710a
52
src/Collection/CountHelper.php
Normal file
52
src/Collection/CountHelper.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?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\Collection;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Returns count of items of the collection.
|
||||
*
|
||||
* Usage:
|
||||
* ```handlebars
|
||||
* {{count collection}}
|
||||
* ```
|
||||
*
|
||||
* Arguments:
|
||||
* - "collection": an array or an instance of \Countable which elements should
|
||||
* be counted.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class CountHelper 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(
|
||||
'"last" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$collection = $context->get($parsed_args[0]);
|
||||
if (!is_array($collection) && !($collection instanceof \Countable)) {
|
||||
throw new \InvalidArgumentException('Wrong type of the argument in the "count" helper.');
|
||||
}
|
||||
|
||||
return count($collection);
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ class Helpers extends BaseHelpers
|
||||
{
|
||||
parent::addDefaultHelpers();
|
||||
|
||||
$this->add('count', new CountHelper());
|
||||
$this->add('first', new FirstHelper());
|
||||
$this->add('last', new LastHelper());
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ class Helpers extends BaseHelpers
|
||||
$this->add('formatDate', new Date\FormatDateHelper());
|
||||
|
||||
// Collection helpers
|
||||
$this->add('count', new Collection\CountHelper());
|
||||
$this->add('first', new Collection\FirstHelper());
|
||||
$this->add('last', new Collection\LastHelper());
|
||||
|
||||
|
111
tests/Collection/CountHelperTest.php
Normal file
111
tests/Collection/CountHelperTest.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?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\Collection;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Collection\CountHelper;
|
||||
|
||||
/**
|
||||
* Test class for "count" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class CountHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that count is calculated properly.
|
||||
*
|
||||
* @dataProvider collectionsProvider
|
||||
*/
|
||||
public function testCount($collection, $result)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('count' => new CountHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{count collection}}',
|
||||
array('collection' => $collection)
|
||||
),
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCount method.
|
||||
*/
|
||||
public function collectionsProvider()
|
||||
{
|
||||
return array(
|
||||
// Test arrays with numeric keys
|
||||
array(array('a', 'b', 'c'), '3'),
|
||||
// Test associative arrays
|
||||
array(array('a' => '10'), '1'),
|
||||
// Test \Countable instance
|
||||
array(new \ArrayIterator(array('a', 'b')), '2'),
|
||||
// Test empty collections
|
||||
array(array(), '0'),
|
||||
array(new \ArrayIterator(array()), '0'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsCountProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('count' => new CountHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render($template, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testArgumentsCount method.
|
||||
*/
|
||||
public function wrongArgumentsCountProvider()
|
||||
{
|
||||
return array(
|
||||
// Not enough arguments
|
||||
array('{{count}}'),
|
||||
// Too much arguments
|
||||
array('{{count "Arg" "ANOTHER ARG"}}'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests invalid arguments type.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider invalidArgumentsProvider
|
||||
*/
|
||||
public function testInvalidArguments($collection)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('count' => new CountHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render('{{count collection}}', array('collection' => $collection));
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testInvalidArguments method.
|
||||
*/
|
||||
public function invalidArgumentsProvider()
|
||||
{
|
||||
return array(
|
||||
array('a string'),
|
||||
array(42),
|
||||
array(new \stdClass()),
|
||||
);
|
||||
}
|
||||
}
|
@ -38,6 +38,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
public function helpersProvider()
|
||||
{
|
||||
return array(
|
||||
array('count', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\CountHelper'),
|
||||
array('first', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\FirstHelper'),
|
||||
array('last', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\LastHelper'),
|
||||
);
|
||||
|
@ -42,6 +42,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
array('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'),
|
||||
|
||||
// Collection helpers
|
||||
array('count', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\CountHelper'),
|
||||
array('first', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\FirstHelper'),
|
||||
array('last', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\LastHelper'),
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user