rename excerpt to ellipsis, adjusted license

This commit is contained in:
Matteo Merola 2017-03-28 16:37:01 +02:00
parent ba31e7a08a
commit 7ed5d5473f
6 changed files with 32 additions and 29 deletions

View File

@ -47,7 +47,7 @@ class Helpers extends BaseHelpers
$this->add('repeat', new Text\RepeatHelper());
$this->add('replace', new Text\ReplaceHelper());
$this->add('truncate', new Text\TruncateHelper());
$this->add('excerpt', new Text\ExcerptHelper());
$this->add('ellipsis', new Text\EllipsisHelper());
// Layout helpers
$storage = new Layout\BlockStorage();

View File

@ -1,9 +1,12 @@
<?php
/**
* Excerpt
* (c) Matteo Merola
*
*/
/*
* 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\Text;
@ -16,7 +19,7 @@ use Handlebars\Template;
*
* Usage:
* ```handlebars
* {{#excerpt string length append}}
* {{#ellipsis string length append}}
* ```
*
* Arguments:
@ -27,7 +30,7 @@ use Handlebars\Template;
*
* @author Matteo Merola <mattmezza@gmail.com>
*/
class ExcerptHelper implements HelperInterface
class EllipsisHelper implements HelperInterface
{
/**
* {@inheritdoc}
@ -37,7 +40,7 @@ class ExcerptHelper implements HelperInterface
$parsed_args = $template->parseArguments($args);
if (count($parsed_args) < 2 || count($parsed_args) > 3) {
throw new \InvalidArgumentException(
'"excerpt" helper expects two or three arguments.'
'"ellipsis" helper expects two or three arguments.'
);
}
$varContent = (string)$context->get($parsed_args[0]);
@ -48,7 +51,7 @@ class ExcerptHelper implements HelperInterface
}
if ($limit < 0) {
throw new \InvalidArgumentException(
'The second argument of "excerpt" helper has to be greater than or equal to 0.'
'The second argument of "ellipsis" helper has to be greater than or equal to 0.'
);
}
$words = str_word_count($varContent, 2);

View File

@ -31,6 +31,6 @@ class Helpers extends BaseHelpers
$this->add('repeat', new RepeatHelper());
$this->add('replace', new ReplaceHelper());
$this->add('truncate', new TruncateHelper());
$this->add('excerpt', new ExcerptHelper());
$this->add('ellipsis', new EllipsisHelper());
}
}

View File

@ -59,7 +59,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
array('excerpt', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ExcerptHelper'),
array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'),
// Layout helpers
array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'),

View File

@ -7,41 +7,41 @@
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
use JustBlackBird\HandlebarsHelpers\Text\ExcerptHelper;
use JustBlackBird\HandlebarsHelpers\Text\EllipsisHelper;
/**
* Test class for "excerpt" helper.
* Test class for "ellipsis" helper.
*
* @author Matteo Merola <mattmezza@gmail.com>
*/
class ExcerptTest extends \PHPUnit_Framework_TestCase
class EllipsisTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests that strings are repeated properly.
*
* @dataProvider truncateProvider
*/
public function testExcerpt($template, $data, $result)
public function testEllipsis($template, $data, $result)
{
$helpers = new \Handlebars\Helpers(array('excerpt' => new ExcerptHelper()));
$helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals($engine->render($template, $data), $result);
}
/**
* A data provider for testExcerpt method.
* A data provider for testEllipsis method.
*/
public function truncateProvider()
{
return array(
// No truncate
array('{{excerpt a len}}', array('a' => '123', 'len' => 5), '123'),
array('{{ellipsis a len}}', array('a' => '123', 'len' => 5), '123'),
// Simple truncates
array('{{excerpt "prova matteo ciao" 2}}', array(), 'prova matteo'),
array('{{excerpt "prova merola hello" 0}}', array(), ''),
array('{{ellipsis "prova matteo ciao" 2}}', array(), 'prova matteo'),
array('{{ellipsis "prova merola hello" 0}}', array(), ''),
// Truncate with ellipsis
array('{{excerpt "prova matt" 1 "..."}}', array(), 'prova...'),
array('{{ellipsis "prova matt" 1 "..."}}', array(), 'prova...'),
);
}
@ -53,7 +53,7 @@ class ExcerptTest extends \PHPUnit_Framework_TestCase
*/
public function testArgumentsCount($template)
{
$helpers = new \Handlebars\Helpers(array('excerpt' => new ExcerptHelper()));
$helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$engine->render($template, array());
@ -66,10 +66,10 @@ class ExcerptTest extends \PHPUnit_Framework_TestCase
{
return array(
// Not enough arguments
array('{{excerpt}}'),
array('{{excerpt "abc"}}'),
array('{{ellipsis}}'),
array('{{ellipsis "abc"}}'),
// Too much arguments
array('{{excerpt "abc" 30 "..." "xyz"}}'),
array('{{ellipsis "abc" 30 "..." "xyz"}}'),
);
}
@ -81,7 +81,7 @@ class ExcerptTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidArguments($template)
{
$helpers = new \Handlebars\Helpers(array('excerpt' => new ExcerptHelper()));
$helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$engine->render($template, array());
@ -94,7 +94,7 @@ class ExcerptTest extends \PHPUnit_Framework_TestCase
{
return array(
// Negative target length.
array('{{excerpt "abc" -10}}'),
array('{{ellipsis "abc" -10}}'),
);
}
}

View File

@ -43,7 +43,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
array('excerpt', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ExcerptHelper'),
array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'),
);
}
}