mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-14 09:44:11 +03:00
Add "uppercase" helper
This commit is contained in:
parent
a4022518b6
commit
02421b3e28
@ -37,5 +37,6 @@ class Helpers extends BaseHelpers
|
||||
|
||||
// String helpers
|
||||
$this->add('lowercase', new String\LowercaseHelper());
|
||||
$this->add('uppercase', new String\UppercaseHelper());
|
||||
}
|
||||
}
|
||||
|
@ -27,5 +27,6 @@ class Helpers extends BaseHelpers
|
||||
parent::addDefaultHelpers();
|
||||
|
||||
$this->add('lowercase', new LowercaseHelper());
|
||||
$this->add('uppercase', new UppercaseHelper());
|
||||
}
|
||||
}
|
||||
|
46
src/String/UppercaseHelper.php
Normal file
46
src/String/UppercaseHelper.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 uppercase.
|
||||
*
|
||||
* Usage:
|
||||
* ```handlebars
|
||||
* {{uppercase string}}
|
||||
* ```
|
||||
*
|
||||
* Arguments:
|
||||
* - "string": A string that should be converted to uppercase.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class UppercaseHelper 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(
|
||||
'"uppercase" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
return strtoupper($context->get($parsed_args[0]));
|
||||
}
|
||||
}
|
@ -49,6 +49,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
// String helpers
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
return array(
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
80
tests/String/UppercaseHelperTest.php
Normal file
80
tests/String/UppercaseHelperTest.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\UppercaseHelper;
|
||||
|
||||
/**
|
||||
* Test class for "uppercase" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class UppercaseHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that strings are converted to uppercase properly.
|
||||
*
|
||||
* @dataProvider convertProvider
|
||||
*/
|
||||
public function testConvert($string, $result)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('uppercase' => new UppercaseHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{uppercase str}}',
|
||||
array('str' => $string)
|
||||
),
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testConvert method.
|
||||
*/
|
||||
public function convertProvider()
|
||||
{
|
||||
return array(
|
||||
array('ALREADY IN UPPERCASE', 'ALREADY IN UPPERCASE'),
|
||||
array('Mixed Case String', 'MIXED CASE STRING'),
|
||||
array('ANOther mIxed CASE string', 'ANOTHER MIXED CASE STRING'),
|
||||
array('string in lowercase', 'STRING IN LOWERCASE'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('uppercase' => new UppercaseHelper()));
|
||||
$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('{{uppercase}}'),
|
||||
// Too much arguments
|
||||
array('{{uppercase "Arg" "ANOTHER ARG"}}'),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user