mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-22 13:41:27 +03:00
rename excerpt to ellipsis, adjusted license
This commit is contained in:
parent
ba31e7a08a
commit
7ed5d5473f
@ -47,7 +47,7 @@ class Helpers extends BaseHelpers
|
|||||||
$this->add('repeat', new Text\RepeatHelper());
|
$this->add('repeat', new Text\RepeatHelper());
|
||||||
$this->add('replace', new Text\ReplaceHelper());
|
$this->add('replace', new Text\ReplaceHelper());
|
||||||
$this->add('truncate', new Text\TruncateHelper());
|
$this->add('truncate', new Text\TruncateHelper());
|
||||||
$this->add('excerpt', new Text\ExcerptHelper());
|
$this->add('ellipsis', new Text\EllipsisHelper());
|
||||||
|
|
||||||
// Layout helpers
|
// Layout helpers
|
||||||
$storage = new Layout\BlockStorage();
|
$storage = new Layout\BlockStorage();
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/*
|
||||||
* Excerpt
|
* This file is part of Handlebars.php Helpers Set
|
||||||
* (c) Matteo Merola
|
*
|
||||||
*
|
* (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;
|
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||||
|
|
||||||
@ -16,7 +19,7 @@ use Handlebars\Template;
|
|||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
* ```handlebars
|
* ```handlebars
|
||||||
* {{#excerpt string length append}}
|
* {{#ellipsis string length append}}
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* Arguments:
|
* Arguments:
|
||||||
@ -27,7 +30,7 @@ use Handlebars\Template;
|
|||||||
*
|
*
|
||||||
* @author Matteo Merola <mattmezza@gmail.com>
|
* @author Matteo Merola <mattmezza@gmail.com>
|
||||||
*/
|
*/
|
||||||
class ExcerptHelper implements HelperInterface
|
class EllipsisHelper implements HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
@ -37,7 +40,7 @@ class ExcerptHelper implements HelperInterface
|
|||||||
$parsed_args = $template->parseArguments($args);
|
$parsed_args = $template->parseArguments($args);
|
||||||
if (count($parsed_args) < 2 || count($parsed_args) > 3) {
|
if (count($parsed_args) < 2 || count($parsed_args) > 3) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'"excerpt" helper expects two or three arguments.'
|
'"ellipsis" helper expects two or three arguments.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$varContent = (string)$context->get($parsed_args[0]);
|
$varContent = (string)$context->get($parsed_args[0]);
|
||||||
@ -48,7 +51,7 @@ class ExcerptHelper implements HelperInterface
|
|||||||
}
|
}
|
||||||
if ($limit < 0) {
|
if ($limit < 0) {
|
||||||
throw new \InvalidArgumentException(
|
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);
|
$words = str_word_count($varContent, 2);
|
@ -31,6 +31,6 @@ class Helpers extends BaseHelpers
|
|||||||
$this->add('repeat', new RepeatHelper());
|
$this->add('repeat', new RepeatHelper());
|
||||||
$this->add('replace', new ReplaceHelper());
|
$this->add('replace', new ReplaceHelper());
|
||||||
$this->add('truncate', new TruncateHelper());
|
$this->add('truncate', new TruncateHelper());
|
||||||
$this->add('excerpt', new ExcerptHelper());
|
$this->add('ellipsis', new EllipsisHelper());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
|
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
|
||||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
|
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
|
||||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
|
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
|
||||||
array('excerpt', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ExcerptHelper'),
|
array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'),
|
||||||
|
|
||||||
// Layout helpers
|
// Layout helpers
|
||||||
array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'),
|
array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'),
|
||||||
|
@ -7,41 +7,41 @@
|
|||||||
|
|
||||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
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>
|
* @author Matteo Merola <mattmezza@gmail.com>
|
||||||
*/
|
*/
|
||||||
class ExcerptTest extends \PHPUnit_Framework_TestCase
|
class EllipsisTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Tests that strings are repeated properly.
|
* Tests that strings are repeated properly.
|
||||||
*
|
*
|
||||||
* @dataProvider truncateProvider
|
* @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));
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
$this->assertEquals($engine->render($template, $data), $result);
|
$this->assertEquals($engine->render($template, $data), $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A data provider for testExcerpt method.
|
* A data provider for testEllipsis method.
|
||||||
*/
|
*/
|
||||||
public function truncateProvider()
|
public function truncateProvider()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
// No truncate
|
// No truncate
|
||||||
array('{{excerpt a len}}', array('a' => '123', 'len' => 5), '123'),
|
array('{{ellipsis a len}}', array('a' => '123', 'len' => 5), '123'),
|
||||||
// Simple truncates
|
// Simple truncates
|
||||||
array('{{excerpt "prova matteo ciao" 2}}', array(), 'prova matteo'),
|
array('{{ellipsis "prova matteo ciao" 2}}', array(), 'prova matteo'),
|
||||||
array('{{excerpt "prova merola hello" 0}}', array(), ''),
|
array('{{ellipsis "prova merola hello" 0}}', array(), ''),
|
||||||
// Truncate with ellipsis
|
// 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)
|
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 = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
$engine->render($template, array());
|
$engine->render($template, array());
|
||||||
@ -66,10 +66,10 @@ class ExcerptTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
// Not enough arguments
|
// Not enough arguments
|
||||||
array('{{excerpt}}'),
|
array('{{ellipsis}}'),
|
||||||
array('{{excerpt "abc"}}'),
|
array('{{ellipsis "abc"}}'),
|
||||||
// Too much arguments
|
// 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)
|
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 = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
$engine->render($template, array());
|
$engine->render($template, array());
|
||||||
@ -94,7 +94,7 @@ class ExcerptTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
// Negative target length.
|
// Negative target length.
|
||||||
array('{{excerpt "abc" -10}}'),
|
array('{{ellipsis "abc" -10}}'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -43,7 +43,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
|
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
|
||||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
|
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
|
||||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
|
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
|
||||||
array('excerpt', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ExcerptHelper'),
|
array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user