Add "replace" helper

This commit is contained in:
Dmitriy Simushev 2014-12-16 15:08:40 +00:00
parent 56c2ddae82
commit bcd8e4d586
6 changed files with 135 additions and 0 deletions

View File

@ -40,6 +40,7 @@ class Helpers extends BaseHelpers
$this->add('lowercase', new String\LowercaseHelper()); $this->add('lowercase', new String\LowercaseHelper());
$this->add('uppercase', new String\UppercaseHelper()); $this->add('uppercase', new String\UppercaseHelper());
$this->add('repeat', new String\RepeatHelper()); $this->add('repeat', new String\RepeatHelper());
$this->add('replace', new String\ReplaceHelper());
$this->add('truncate', new String\TruncateHelper()); $this->add('truncate', new String\TruncateHelper());
// Layout helpers // Layout helpers

View File

@ -29,6 +29,7 @@ class Helpers extends BaseHelpers
$this->add('lowercase', new LowercaseHelper()); $this->add('lowercase', new LowercaseHelper());
$this->add('uppercase', new UppercaseHelper()); $this->add('uppercase', new UppercaseHelper());
$this->add('repeat', new RepeatHelper()); $this->add('repeat', new RepeatHelper());
$this->add('replace', new ReplaceHelper());
$this->add('truncate', new TruncateHelper()); $this->add('truncate', new TruncateHelper());
} }
} }

View File

@ -0,0 +1,51 @@
<?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;
/**
* Helper for replacing substrings.
*
* Usage:
* ```handlebars
* {{#replace search replacement}}Target string to search in.{{/replace}}
* ```
*
* Arguments:
* - "search": The value that should be replaced.
* - "replacement": The value that should be use as a replacement.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class ReplaceHelper implements HelperInterface
{
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$parsed_args = $template->parseArguments($args);
if (count($parsed_args) != 2) {
throw new \InvalidArgumentException(
'"replace" helper expects exactly two arguments.'
);
}
$search = $context->get($parsed_args[0]);
$replacement = $context->get($parsed_args[1]);
$subject = (string)$template->render($context);
return str_replace($search, $replacement, $subject);
}
}

View File

@ -52,6 +52,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'), array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'), array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'), array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\String\\ReplaceHelper'),
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'), array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
// Layout helpers // Layout helpers

View File

@ -41,6 +41,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'), array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'), array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'), array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\String\\ReplaceHelper'),
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'), array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
); );
} }

View 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\ReplaceHelper;
/**
* Test class for "replace" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class ReplaceHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests that strings are repeated properly.
*
* @dataProvider replaceProvider
*/
public function testReplace($string, $search, $replacement, $result)
{
$helpers = new \Handlebars\Helpers(array('replace' => new ReplaceHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#replace search replacement}}{{str}}{{/replace}}',
array('str' => $string, 'search' => $search, 'replacement' => $replacement)
),
$result
);
}
/**
* A data provider for testReplace method.
*/
public function replaceProvider()
{
return array(
array('abcd', 'b', '', 'acd'),
array('abcd', 'xyz', '', 'abcd'),
array('abcd', '', 'asd', 'abcd'),
);
}
/**
* 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('replace' => new ReplaceHelper()));
$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('{{#replace}}str{{/replace}}'),
array('{{#replace "serach"}}str{{/replace}}'),
// Too much arguments
array('{{#replace "search" "replacement" "asd"}}str{{/replace}}'),
);
}
}