mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-05-05 17:53:06 +03:00
Add "lowercase" helper
This commit is contained in:
parent
ef77303571
commit
a4022518b6
@ -34,5 +34,8 @@ class Helpers extends BaseHelpers
|
|||||||
$this->add('ifEven', new Comparison\IfEvenHelper());
|
$this->add('ifEven', new Comparison\IfEvenHelper());
|
||||||
$this->add('ifOdd', new Comparison\IfOddHelper());
|
$this->add('ifOdd', new Comparison\IfOddHelper());
|
||||||
$this->add('unlessEqual', new Comparison\UnlessEqualHelper());
|
$this->add('unlessEqual', new Comparison\UnlessEqualHelper());
|
||||||
|
|
||||||
|
// String helpers
|
||||||
|
$this->add('lowercase', new String\LowercaseHelper());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
31
src/String/Helpers.php
Normal file
31
src/String/Helpers.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?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\Helpers as BaseHelpers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all strings related helpers.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class Helpers extends BaseHelpers
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function addDefaultHelpers()
|
||||||
|
{
|
||||||
|
parent::addDefaultHelpers();
|
||||||
|
|
||||||
|
$this->add('lowercase', new LowercaseHelper());
|
||||||
|
}
|
||||||
|
}
|
46
src/String/LowercaseHelper.php
Normal file
46
src/String/LowercaseHelper.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a string to lowercase.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```handlebars
|
||||||
|
* {{lowercase string}}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Arguments:
|
||||||
|
* - "string": A string that should be converted to lowercase.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class LowercaseHelper 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(
|
||||||
|
'"lowercase" helper expects exactly one argument.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return strtolower($context->get($parsed_args[0]));
|
||||||
|
}
|
||||||
|
}
|
@ -46,6 +46,9 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
|
array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
|
||||||
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
|
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
|
||||||
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
|
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
|
||||||
|
|
||||||
|
// String helpers
|
||||||
|
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
tests/String/HelpersTest.php
Normal file
44
tests/String/HelpersTest.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?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\Helpers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for String Helpers Set.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that all helpers in the set exist and have valid classes.
|
||||||
|
*
|
||||||
|
* @dataProvider helpersProvider
|
||||||
|
*/
|
||||||
|
public function testHelper($name, $class)
|
||||||
|
{
|
||||||
|
$helpers = new Helpers();
|
||||||
|
|
||||||
|
$this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name));
|
||||||
|
$this->assertInstanceOf($class, $helpers->{$name});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testHelper method.
|
||||||
|
*/
|
||||||
|
public function helpersProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
80
tests/String/LowercaseHelperTest.php
Normal file
80
tests/String/LowercaseHelperTest.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?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\LowercaseHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for "lowercase" helper.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class LowercaseHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that strings are converted to lowercase properly.
|
||||||
|
*
|
||||||
|
* @dataProvider convertProvider
|
||||||
|
*/
|
||||||
|
public function testConvert($string)
|
||||||
|
{
|
||||||
|
$helpers = new \Handlebars\Helpers(array('lowercase' => new LowercaseHelper()));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
$engine->render(
|
||||||
|
'{{lowercase str}}',
|
||||||
|
array('str' => $string)
|
||||||
|
),
|
||||||
|
strtolower($string)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testConvert method.
|
||||||
|
*/
|
||||||
|
public function convertProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('already in lower'),
|
||||||
|
array('Mixed Case String'),
|
||||||
|
array('ANOther mIxed CASE string'),
|
||||||
|
array('STRING IN CAPS'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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('lowercase' => new LowercaseHelper()));
|
||||||
|
$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('{{lowercase}}'),
|
||||||
|
// Too much arguments
|
||||||
|
array('{{lowercase "Arg" "ANOTHER ARG"}}'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user