diff --git a/src/Helpers.php b/src/Helpers.php index 182c60d..19eacd5 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -34,5 +34,8 @@ class Helpers extends BaseHelpers $this->add('ifEven', new Comparison\IfEvenHelper()); $this->add('ifOdd', new Comparison\IfOddHelper()); $this->add('unlessEqual', new Comparison\UnlessEqualHelper()); + + // String helpers + $this->add('lowercase', new String\LowercaseHelper()); } } diff --git a/src/String/Helpers.php b/src/String/Helpers.php new file mode 100644 index 0000000..1f75baa --- /dev/null +++ b/src/String/Helpers.php @@ -0,0 +1,31 @@ + + * + * 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 + */ +class Helpers extends BaseHelpers +{ + /** + * {@inheritdoc} + */ + protected function addDefaultHelpers() + { + parent::addDefaultHelpers(); + + $this->add('lowercase', new LowercaseHelper()); + } +} diff --git a/src/String/LowercaseHelper.php b/src/String/LowercaseHelper.php new file mode 100644 index 0000000..56e8dcc --- /dev/null +++ b/src/String/LowercaseHelper.php @@ -0,0 +1,46 @@ + + * + * 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 + */ +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])); + } +} diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index bedcfaa..f47e8ee 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -46,6 +46,9 @@ class HelpersTest extends \PHPUnit_Framework_TestCase array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'), array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'), array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'), + + // String helpers + array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'), ); } } diff --git a/tests/String/HelpersTest.php b/tests/String/HelpersTest.php new file mode 100644 index 0000000..b649c3e --- /dev/null +++ b/tests/String/HelpersTest.php @@ -0,0 +1,44 @@ + + * + * 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 + */ +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'), + ); + } +} diff --git a/tests/String/LowercaseHelperTest.php b/tests/String/LowercaseHelperTest.php new file mode 100644 index 0000000..e9734d8 --- /dev/null +++ b/tests/String/LowercaseHelperTest.php @@ -0,0 +1,80 @@ + + * + * 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 + */ +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"}}'), + ); + } +}