mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-14 09:44:11 +03:00
Add "replace" helper
This commit is contained in:
parent
56c2ddae82
commit
bcd8e4d586
@ -40,6 +40,7 @@ class Helpers extends BaseHelpers
|
||||
$this->add('lowercase', new String\LowercaseHelper());
|
||||
$this->add('uppercase', new String\UppercaseHelper());
|
||||
$this->add('repeat', new String\RepeatHelper());
|
||||
$this->add('replace', new String\ReplaceHelper());
|
||||
$this->add('truncate', new String\TruncateHelper());
|
||||
|
||||
// Layout helpers
|
||||
|
@ -29,6 +29,7 @@ class Helpers extends BaseHelpers
|
||||
$this->add('lowercase', new LowercaseHelper());
|
||||
$this->add('uppercase', new UppercaseHelper());
|
||||
$this->add('repeat', new RepeatHelper());
|
||||
$this->add('replace', new ReplaceHelper());
|
||||
$this->add('truncate', new TruncateHelper());
|
||||
}
|
||||
}
|
||||
|
51
src/String/ReplaceHelper.php
Normal file
51
src/String/ReplaceHelper.php
Normal 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);
|
||||
}
|
||||
}
|
@ -52,6 +52,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
|
||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\String\\ReplaceHelper'),
|
||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
|
||||
|
||||
// Layout helpers
|
||||
|
@ -41,6 +41,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
|
||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\String\\ReplaceHelper'),
|
||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
|
||||
);
|
||||
}
|
||||
|
80
tests/String/ReplaceHelperTest.php
Normal file
80
tests/String/ReplaceHelperTest.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\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}}'),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user