├── .gitignore ├── .travis.yml ├── phpunit.xml.dist ├── src ├── Date │ ├── Helpers.php │ └── FormatDateHelper.php ├── Collection │ ├── Helpers.php │ ├── CountHelper.php │ ├── FirstHelper.php │ └── LastHelper.php ├── Layout │ ├── AbstractBlockHelper.php │ ├── Helpers.php │ ├── BlockHelper.php │ ├── BlockStorage.php │ ├── IfOverriddenHelper.php │ ├── OverrideHelper.php │ ├── UnlessOverriddenHelper.php │ └── ExtendsHelper.php ├── Text │ ├── Helpers.php │ ├── LowercaseHelper.php │ ├── UppercaseHelper.php │ ├── ReplaceHelper.php │ ├── RepeatHelper.php │ ├── TruncateHelper.php │ └── EllipsisHelper.php ├── Comparison │ ├── IfOddHelper.php │ ├── IfEvenHelper.php │ ├── IfEqualHelper.php │ ├── UnlessEqualHelper.php │ ├── Helpers.php │ ├── IfLessHelper.php │ ├── IfMoreHelper.php │ ├── IfBetweenHelper.php │ ├── IfBetweenClosedHelper.php │ ├── AbstractComparisonHelper.php │ ├── IfBetweenLeftClosedHelper.php │ ├── IfBetweenRightClosedHelper.php │ └── IfAnyHelper.php └── Helpers.php ├── LICENSE ├── composer.json ├── tests ├── Date │ ├── HelpersTest.php │ └── FormatDateHelperTest.php ├── Layout │ ├── BlockStorageTest.php │ ├── BlockHelperTest.php │ ├── ExtendsHelperTest.php │ ├── OverrideHelperTest.php │ ├── HelpersTest.php │ ├── IfOverriddenHelperTest.php │ ├── UnlessOverriddenHelperTest.php │ └── IntegrationTest.php ├── Collection │ ├── HelpersTest.php │ ├── CountHelperTest.php │ ├── LastHelperTest.php │ └── FirstHelperTest.php ├── Text │ ├── HelpersTest.php │ ├── LowercaseHelperTest.php │ ├── UppercaseHelperTest.php │ ├── ReplaceHelperTest.php │ ├── RepeatHelperTest.php │ ├── EllipsisHelperTest.php │ └── TruncateHelperTest.php ├── Comparison │ ├── HelpersTest.php │ ├── IfOddHelperTest.php │ ├── IfEvenHelperTest.php │ ├── UnlessEqualHelperTest.php │ ├── IfEqualHelperTest.php │ ├── IfLessHelperTest.php │ ├── IfMoreHelperTest.php │ ├── IfBetweenHelperTest.php │ ├── IfAnyHelperTest.php │ ├── IfBetweenClosedHelperTest.php │ ├── IfBetweenLeftClosedHelperTest.php │ └── IfBetweenRightClosedHelperTest.php └── HelpersTest.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Exclude dependencies 2 | vendor 3 | 4 | # Exclude composer's files 5 | composer.phar 6 | composer.lock 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.3 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - 7.0 8 | - hhvm 9 | install: composer install 10 | script: "./vendor/bin/phpunit && ./vendor/bin/phpcs --standard=PSR2 -n src/" 11 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | tests 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/Date/Helpers.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Date; 12 | 13 | use Handlebars\Helpers as BaseHelpers; 14 | 15 | /** 16 | * Contains all date related helpers. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class Helpers extends BaseHelpers 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | protected function addDefaultHelpers() 26 | { 27 | parent::addDefaultHelpers(); 28 | 29 | $this->add('formatDate', new FormatDateHelper()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Collection/Helpers.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Collection; 12 | 13 | use Handlebars\Helpers as BaseHelpers; 14 | 15 | /** 16 | * Contains all collections related helpers. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class Helpers extends BaseHelpers 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | protected function addDefaultHelpers() 26 | { 27 | parent::addDefaultHelpers(); 28 | 29 | $this->add('count', new CountHelper()); 30 | $this->add('first', new FirstHelper()); 31 | $this->add('last', new LastHelper()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Layout/AbstractBlockHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | /** 14 | * Contains base functionality for all helpers related with blocks overriding. 15 | * 16 | * @author Dmitriy Simushev 17 | */ 18 | abstract class AbstractBlockHelper 19 | { 20 | /** 21 | * @var BlockStorage 22 | */ 23 | protected $blocksStorage; 24 | 25 | /** 26 | * Helper's constructor. 27 | * 28 | * @param BlockStorage $storage A Blocks context instance 29 | */ 30 | public function __construct(BlockStorage $storage) 31 | { 32 | $this->blocksStorage = $storage; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Text/Helpers.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Helpers as BaseHelpers; 14 | 15 | /** 16 | * Contains all strings related helpers. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class Helpers extends BaseHelpers 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | protected function addDefaultHelpers() 26 | { 27 | parent::addDefaultHelpers(); 28 | 29 | $this->add('lowercase', new LowercaseHelper()); 30 | $this->add('uppercase', new UppercaseHelper()); 31 | $this->add('repeat', new RepeatHelper()); 32 | $this->add('replace', new ReplaceHelper()); 33 | $this->add('truncate', new TruncateHelper()); 34 | $this->add('ellipsis', new EllipsisHelper()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Layout/Helpers.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | use Handlebars\Helpers as BaseHelpers; 14 | 15 | /** 16 | * Contains all layout helpers. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class Helpers extends BaseHelpers 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | protected function addDefaultHelpers() 26 | { 27 | parent::addDefaultHelpers(); 28 | 29 | $storage = new BlockStorage(); 30 | $this->add('block', new BlockHelper($storage)); 31 | $this->add('extends', new ExtendsHelper($storage)); 32 | $this->add('override', new OverrideHelper($storage)); 33 | $this->add('ifOverridden', new IfOverriddenHelper($storage)); 34 | $this->add('unlessOverridden', new UnlessOverriddenHelper($storage)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 - 2016 Dmitriy Simushev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mibew/handlebars.php-helpers", 3 | "version": "1.2.3", 4 | "description": "A set of helpers for Handlebars.php template engine.", 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Dmitriy Simushev", 10 | "email": "simushevds@gmail.com" 11 | }, 12 | { 13 | "name": "Mibew Messenger project", 14 | "homepage": "https://mibew.org/" 15 | } 16 | ], 17 | "support": { 18 | "issues": "https://github.com/JustBlackBird/handlebars.php-helpers/issues", 19 | "source": "https://github.com/JustBlackBird/handlebars.php-helpers" 20 | }, 21 | "repositories": [ 22 | { 23 | "type": "vcs", 24 | "url": "https://github.com/mibew/handlebars.php" 25 | } 26 | ], 27 | "require": { 28 | "mibew/handlebars.php": "~0.10.5" 29 | }, 30 | "require-dev": { 31 | "phpunit/phpunit": "~4.4", 32 | "squizlabs/php_codesniffer": "~2.0" 33 | }, 34 | "autoload": { 35 | "psr-4": { 36 | "JustBlackBird\\HandlebarsHelpers\\": "src/" 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/Comparison/IfOddHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if specified argument is odd or not. 17 | * 18 | * Example of usage: 19 | * ```handlebars 20 | * {{#ifOdd value}} 21 | * The value is odd. 22 | * {{else}} 23 | * The value is even. 24 | * {{/ifOdd}} 25 | * ``` 26 | * 27 | * @author Dmitriy Simushev 28 | */ 29 | class IfOddHelper extends AbstractComparisonHelper implements HelperInterface 30 | { 31 | /** 32 | * {@inheritdoc} 33 | */ 34 | protected function evaluateCondition($args) 35 | { 36 | if (count($args) != 1) { 37 | throw new \InvalidArgumentException( 38 | '"ifOdd" helper expects exactly one argument.' 39 | ); 40 | } 41 | 42 | return ($args[0] % 2 == 1); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Comparison/IfEvenHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if specified argument is even or not. 17 | * 18 | * Example of usage: 19 | * ```handlebars 20 | * {{#ifEven value}} 21 | * The value is even. 22 | * {{else}} 23 | * The value is odd. 24 | * {{/ifEven}} 25 | * ``` 26 | * 27 | * @author Dmitriy Simushev 28 | */ 29 | class IfEvenHelper extends AbstractComparisonHelper implements HelperInterface 30 | { 31 | /** 32 | * {@inheritdoc} 33 | */ 34 | protected function evaluateCondition($args) 35 | { 36 | if (count($args) != 1) { 37 | throw new \InvalidArgumentException( 38 | '"ifEven" helper expects exactly one argument.' 39 | ); 40 | } 41 | 42 | return ($args[0] % 2 == 0); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Comparison/IfEqualHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if two values are equal or not. 17 | * 18 | * Example of usage: 19 | * ```handlebars 20 | * {{#ifEqual first second}} 21 | * The first argument is equal to the second one. 22 | * {{else}} 23 | * The arguments are not equal. 24 | * {{/ifEqual}} 25 | * ``` 26 | * 27 | * @author Dmitriy Simushev 28 | */ 29 | class IfEqualHelper extends AbstractComparisonHelper implements HelperInterface 30 | { 31 | /** 32 | * {@inheritdoc} 33 | */ 34 | protected function evaluateCondition($args) 35 | { 36 | if (count($args) != 2) { 37 | throw new \InvalidArgumentException( 38 | '"ifEqual" helper expects exactly two arguments.' 39 | ); 40 | } 41 | 42 | return ($args[0] == $args[1]); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Comparison/UnlessEqualHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if two values are equal or not. 17 | * 18 | * Example of usage: 19 | * ```handlebars 20 | * {{#unlessEqual first second}} 21 | * The first argument is equal to the second one. 22 | * {{else}} 23 | * The arguments are not equal. 24 | * {{/unlessEqual}} 25 | * ``` 26 | * 27 | * @author Dmitriy Simushev 28 | */ 29 | class UnlessEqualHelper extends AbstractComparisonHelper implements HelperInterface 30 | { 31 | /** 32 | * {@inheritdoc} 33 | */ 34 | protected function evaluateCondition($args) 35 | { 36 | if (count($args) != 2) { 37 | throw new \InvalidArgumentException( 38 | '"unlessEqual" helper expects exactly two arguments.' 39 | ); 40 | } 41 | 42 | return ($args[0] != $args[1]); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Text/LowercaseHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Converts a string to lowercase. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{lowercase string}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "string": A string that should be converted to lowercase. 27 | * 28 | * @author Dmitriy Simushev 29 | */ 30 | class LowercaseHelper implements HelperInterface 31 | { 32 | /** 33 | * {@inheritdoc} 34 | */ 35 | public function execute(Template $template, Context $context, $args, $source) 36 | { 37 | $parsed_args = $template->parseArguments($args); 38 | if (count($parsed_args) != 1) { 39 | throw new \InvalidArgumentException( 40 | '"lowercase" helper expects exactly one argument.' 41 | ); 42 | } 43 | 44 | return strtolower($context->get($parsed_args[0])); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Text/UppercaseHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Converts a string to uppercase. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{uppercase string}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "string": A string that should be converted to uppercase. 27 | * 28 | * @author Dmitriy Simushev 29 | */ 30 | class UppercaseHelper implements HelperInterface 31 | { 32 | /** 33 | * {@inheritdoc} 34 | */ 35 | public function execute(Template $template, Context $context, $args, $source) 36 | { 37 | $parsed_args = $template->parseArguments($args); 38 | if (count($parsed_args) != 1) { 39 | throw new \InvalidArgumentException( 40 | '"uppercase" helper expects exactly one argument.' 41 | ); 42 | } 43 | 44 | return strtoupper($context->get($parsed_args[0])); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Date/HelpersTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Date; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Date\Helpers; 14 | 15 | /** 16 | * Test class for Date Helpers Set. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class HelpersTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that all helpers in the set exist and have valid classes. 24 | * 25 | * @dataProvider helpersProvider 26 | */ 27 | public function testHelper($name, $class) 28 | { 29 | $helpers = new Helpers(); 30 | 31 | $this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name)); 32 | $this->assertInstanceOf($class, $helpers->{$name}); 33 | } 34 | 35 | /** 36 | * A data provider for testHelper method. 37 | */ 38 | public function helpersProvider() 39 | { 40 | return array( 41 | array('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'), 42 | ); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Comparison/Helpers.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helpers as BaseHelpers; 14 | 15 | /** 16 | * Contains all comparison related helpers. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class Helpers extends BaseHelpers 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | protected function addDefaultHelpers() 26 | { 27 | parent::addDefaultHelpers(); 28 | 29 | $this->add('ifAny', new IfAnyHelper()); 30 | $this->add('ifEqual', new IfEqualHelper()); 31 | $this->add('ifEven', new IfEvenHelper()); 32 | $this->add('ifOdd', new IfOddHelper()); 33 | $this->add('ifLess', new IfLessHelper()); 34 | $this->add('ifMore', new IfMoreHelper()); 35 | $this->add('ifBetween', new IfBetweenHelper()); 36 | $this->add('ifBetweenClosed', new IfBetweenClosedHelper()); 37 | $this->add('ifBetweenLeftClosed', new IfBetweenLeftClosedHelper()); 38 | $this->add('ifBetweenRightClosed', new IfBetweenRightClosedHelper()); 39 | $this->add('unlessEqual', new UnlessEqualHelper()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Comparison/IfLessHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if one value is less than another one. 17 | * 18 | * By "less" strict inequality is meant. That's why in cases where two equal 19 | * values are compared the result of the "less" operation is false. 20 | * 21 | * Example of usage: 22 | * ```handlebars 23 | * {{#ifLess first second}} 24 | * The first argument is less than the second one. 25 | * {{else}} 26 | * The first argument is more or equal to the second one. 27 | * {{/ifLess}} 28 | * ``` 29 | * 30 | * @author Dmitriy Simushev 31 | */ 32 | class IfLessHelper extends AbstractComparisonHelper implements HelperInterface 33 | { 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | protected function evaluateCondition($args) 38 | { 39 | if (count($args) != 2) { 40 | throw new \InvalidArgumentException( 41 | '"ifLess" helper expects exactly two arguments.' 42 | ); 43 | } 44 | 45 | return ($args[0] < $args[1]); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Comparison/IfMoreHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if one value is more than another one. 17 | * 18 | * By "more" strict inequality is meant. That's why in cases where two equal 19 | * values are compared the result of the "more" operation is false. 20 | * 21 | * Example of usage: 22 | * ```handlebars 23 | * {{#ifMore first second}} 24 | * The first argument is more than the second one. 25 | * {{else}} 26 | * The first argument is less or equal to the second one. 27 | * {{/ifMore}} 28 | * ``` 29 | * 30 | * @author Dmitriy Simushev 31 | */ 32 | class IfMoreHelper extends AbstractComparisonHelper implements HelperInterface 33 | { 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | protected function evaluateCondition($args) 38 | { 39 | if (count($args) != 2) { 40 | throw new \InvalidArgumentException( 41 | '"ifMore" helper expects exactly two arguments.' 42 | ); 43 | } 44 | 45 | return ($args[0] > $args[1]); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Layout/BlockStorageTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 14 | 15 | /** 16 | * Test class for BlockStorage. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class BlockStorageTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests basic operations with storage. 24 | */ 25 | public function testBasicOperations() 26 | { 27 | $storage = new BlockStorage(); 28 | $this->assertFalse($storage->has('test')); 29 | $storage->set('test', 'content'); 30 | $this->assertTrue($storage->has('test')); 31 | $this->assertEquals($storage->get('test'), 'content'); 32 | $storage->set('test', 'another content'); 33 | $this->assertEquals($storage->get('test'), 'another content'); 34 | $storage->remove('test'); 35 | $this->assertFalse($storage->has('test')); 36 | $storage->set('test', 'content'); 37 | $storage->set('another_test', 'content'); 38 | $storage->clear(); 39 | $this->assertFalse($storage->has('test')); 40 | $this->assertFalse($storage->has('another_test')); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/Collection/HelpersTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Collection; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Collection\Helpers; 14 | 15 | /** 16 | * Test class for Collection Helpers Set. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class HelpersTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that all helpers in the set exist and have valid classes. 24 | * 25 | * @dataProvider helpersProvider 26 | */ 27 | public function testHelper($name, $class) 28 | { 29 | $helpers = new Helpers(); 30 | 31 | $this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name)); 32 | $this->assertInstanceOf($class, $helpers->{$name}); 33 | } 34 | 35 | /** 36 | * A data provider for testHelper method. 37 | */ 38 | public function helpersProvider() 39 | { 40 | return array( 41 | array('count', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\CountHelper'), 42 | array('first', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\FirstHelper'), 43 | array('last', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\LastHelper'), 44 | ); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Comparison/IfBetweenHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if one value is between two another values. 17 | * 18 | * Borders of the interval are excluded from comparison. That's why in cases 19 | * where the value under comparision is equal to one of the borders the result 20 | * of "between" operator is false. 21 | * 22 | * Example of usage: 23 | * ```handlebars 24 | * {{#ifBetween value leftBorder rightBorder}} 25 | * The first argument is between borders. 26 | * {{else}} 27 | * The first argument is not between the borders. 28 | * {{/ifBetween}} 29 | * ``` 30 | * 31 | * @author Dmitriy Simushev 32 | */ 33 | class IfBetweenHelper extends AbstractComparisonHelper implements HelperInterface 34 | { 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | protected function evaluateCondition($args) 39 | { 40 | if (count($args) != 3) { 41 | throw new \InvalidArgumentException( 42 | '"ifBetween" helper expects exactly three arguments.' 43 | ); 44 | } 45 | 46 | return ($args[0] > $args[1]) && ($args[0] < $args[2]); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Comparison/IfBetweenClosedHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if one value is between two another values. 17 | * 18 | * Borders of the interval are included into comparison. That's why in cases 19 | * where the value under comparision is equal to one of the borders the result 20 | * of "between closed" operator is true. 21 | * 22 | * Example of usage: 23 | * ```handlebars 24 | * {{#ifBetweenClosed value leftBorder rightBorder}} 25 | * The first argument is between borders. 26 | * {{else}} 27 | * The first argument is not between the borders. 28 | * {{/ifBetweenClosed}} 29 | * ``` 30 | * 31 | * @author Dmitriy Simushev 32 | */ 33 | class IfBetweenClosedHelper extends AbstractComparisonHelper implements HelperInterface 34 | { 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | protected function evaluateCondition($args) 39 | { 40 | if (count($args) != 3) { 41 | throw new \InvalidArgumentException( 42 | '"ifBetweenClosed" helper expects exactly three arguments.' 43 | ); 44 | } 45 | 46 | return ($args[0] >= $args[1]) && ($args[0] <= $args[2]); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Handlebars.php Helpers 2 | 3 | [![Build Status](https://travis-ci.org/JustBlackBird/handlebars.php-helpers.svg)](https://travis-ci.org/JustBlackBird/handlebars.php-helpers) 4 | 5 | > Provides a set of helpers for [Handlebars.php](https://github.com/XaminProject/handlebars.php) template engine. 6 | 7 | 8 | ## Installation 9 | 10 | Simply add a dependency on `justblackbird/handlebars.php-helpers` to your 11 | project's `composer.json` file if you use [Composer](http://getcomposer.org/) to 12 | manage the dependencies of your project. 13 | 14 | 15 | ## Usage 16 | 17 | To use all helpers in your templates just create an instance of helpers set and 18 | attach it to Handlebars engine. 19 | 20 | ```php 21 | $helpers = new \JustBlackBird\HandlebarsHelpers\Helpers(); 22 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 23 | ``` 24 | 25 | Want to use only subset of helpers? Fine. Just create an instance of appropriate 26 | helpers set and attach it to Handlebars engine. Here is an example for Date 27 | helpers: 28 | 29 | ```php 30 | $helpers = new \JustBlackBird\HandlebarsHelpers\Date\Helpers(); 31 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 32 | ``` 33 | 34 | Want to use only chosen helpers? No problem. Just add them manually to your 35 | helpers set: 36 | 37 | ```php 38 | $engine = new \Handlebars\Handlebars(); 39 | $engine->getHelpers()->add( 40 | 'ifEqual', 41 | new \JustBlackBird\HandlebarsHelpers\Comparison\IfEqualHelper() 42 | ); 43 | ``` 44 | 45 | 46 | ## License 47 | 48 | [MIT](http://opensource.org/licenses/MIT) (c) Dmitriy Simushev 49 | -------------------------------------------------------------------------------- /src/Text/ReplaceHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Helper for replacing substrings. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#replace search replacement}}Target string to search in.{{/replace}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "search": The value that should be replaced. 27 | * - "replacement": The value that should be use as a replacement. 28 | * 29 | * @author Dmitriy Simushev 30 | */ 31 | class ReplaceHelper implements HelperInterface 32 | { 33 | /** 34 | * {@inheritdoc} 35 | */ 36 | public function execute(Template $template, Context $context, $args, $source) 37 | { 38 | $parsed_args = $template->parseArguments($args); 39 | if (count($parsed_args) != 2) { 40 | throw new \InvalidArgumentException( 41 | '"replace" helper expects exactly two arguments.' 42 | ); 43 | } 44 | 45 | $search = $context->get($parsed_args[0]); 46 | $replacement = $context->get($parsed_args[1]); 47 | $subject = (string)$template->render($context); 48 | 49 | return str_replace($search, $replacement, $subject); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Collection/CountHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Collection; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Returns count of items of the collection. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{count collection}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "collection": an array or an instance of \Countable which elements should 27 | * be counted. 28 | * 29 | * @author Dmitriy Simushev 30 | */ 31 | class CountHelper implements HelperInterface 32 | { 33 | /** 34 | * {@inheritdoc} 35 | */ 36 | public function execute(Template $template, Context $context, $args, $source) 37 | { 38 | $parsed_args = $template->parseArguments($args); 39 | if (count($parsed_args) != 1) { 40 | throw new \InvalidArgumentException( 41 | '"last" helper expects exactly one argument.' 42 | ); 43 | } 44 | 45 | $collection = $context->get($parsed_args[0]); 46 | if (!is_array($collection) && !($collection instanceof \Countable)) { 47 | throw new \InvalidArgumentException('Wrong type of the argument in the "count" helper.'); 48 | } 49 | 50 | return count($collection); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/Layout/BlockHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\BlockHelper; 14 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 15 | 16 | /** 17 | * Test class for "block" helper. 18 | * 19 | * @author Dmitriy Simushev 20 | */ 21 | class BlockHelperTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Tests that exception is thrown if wrong number of arguments is used. 25 | * 26 | * @expectedException InvalidArgumentException 27 | * @dataProvider wrongArgumentsProvider 28 | */ 29 | public function testArgumentsCount($template) 30 | { 31 | $storage = new BlockStorage(); 32 | $helpers = new \Handlebars\Helpers(array('block' => new BlockHelper($storage))); 33 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 34 | 35 | $engine->render($template, array()); 36 | } 37 | 38 | /** 39 | * A data provider for testArgumentsCount method. 40 | */ 41 | public function wrongArgumentsProvider() 42 | { 43 | return array( 44 | // Not enough arguments 45 | array('{{#block}}content{{/block}}'), 46 | // Too much arguments 47 | array('{{#block "arg1" "arg2"}}content{{/block}}'), 48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Comparison/AbstractComparisonHelper.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | abstract class AbstractComparisonHelper implements HelperInterface 15 | { 16 | /** 17 | * {@inheritdoc} 18 | */ 19 | public function execute(Template $template, Context $context, $args, $source) 20 | { 21 | $resolved_args = array(); 22 | foreach ($template->parseArguments($args) as $arg) { 23 | $resolved_args[] = $context->get($arg); 24 | } 25 | 26 | if ($this->evaluateCondition($resolved_args)) { 27 | $template->setStopToken('else'); 28 | $buffer = $template->render($context); 29 | $template->setStopToken(false); 30 | } else { 31 | $template->setStopToken('else'); 32 | $template->discard(); 33 | $template->setStopToken(false); 34 | $buffer = $template->render($context); 35 | } 36 | 37 | return $buffer; 38 | } 39 | 40 | /** 41 | * Evaluates condition based on helper's arguments. 42 | * 43 | * This is a template method which must be overriden in inherited helpers. 44 | * 45 | * @param array $args List of resolved arguments passed to the helper. 46 | * @return bool Indicates if the condition is true or false. 47 | */ 48 | abstract protected function evaluateCondition($args); 49 | } 50 | -------------------------------------------------------------------------------- /tests/Layout/ExtendsHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 14 | use JustBlackBird\HandlebarsHelpers\Layout\ExtendsHelper; 15 | 16 | /** 17 | * Test class for "extends" helper. 18 | * 19 | * @author Dmitriy Simushev 20 | */ 21 | class ExtendsHelperTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Tests that exception is thrown if wrong number of arguments is used. 25 | * 26 | * @expectedException InvalidArgumentException 27 | * @dataProvider wrongArgumentsProvider 28 | */ 29 | public function testArgumentsCount($template) 30 | { 31 | $storage = new BlockStorage(); 32 | $helpers = new \Handlebars\Helpers(array('extends' => new ExtendsHelper($storage))); 33 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 34 | 35 | $engine->render($template, array()); 36 | } 37 | 38 | /** 39 | * A data provider for testArgumentsCount method. 40 | */ 41 | public function wrongArgumentsProvider() 42 | { 43 | return array( 44 | // Not enough arguments 45 | array('{{#extends}}content{{/extends}}'), 46 | // Too much arguments 47 | array('{{#extends "arg1" "arg2"}}content{{/extends}}'), 48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Layout/OverrideHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\OverrideHelper; 14 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 15 | 16 | /** 17 | * Test class for "override" helper. 18 | * 19 | * @author Dmitriy Simushev 20 | */ 21 | class OverrideHelperTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Tests that exception is thrown if wrong number of arguments is used. 25 | * 26 | * @expectedException InvalidArgumentException 27 | * @dataProvider wrongArgumentsProvider 28 | */ 29 | public function testArgumentsCount($template) 30 | { 31 | $storage = new BlockStorage(); 32 | $helpers = new \Handlebars\Helpers(array('override' => new OverrideHelper($storage))); 33 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 34 | 35 | $engine->render($template, array()); 36 | } 37 | 38 | /** 39 | * A data provider for testArgumentsCount method. 40 | */ 41 | public function wrongArgumentsProvider() 42 | { 43 | return array( 44 | // Not enough arguments 45 | array('{{#override}}content{{/override}}'), 46 | // Too much arguments 47 | array('{{#override "arg1" "arg2"}}content{{/override}}'), 48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Comparison/IfBetweenLeftClosedHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if one value is between two another values. 17 | * 18 | * Only left border of the interval is included into comparison. That's why in 19 | * cases where the value under comparision is equal to left border the result 20 | * of "between left closed" operator is true but when the value under compatison 21 | * is equal to right border the result of "between left closed" is false. 22 | * 23 | * Example of usage: 24 | * ```handlebars 25 | * {{#ifBetweenLeftClosed value leftBorder rightBorder}} 26 | * The first argument is between borders. 27 | * {{else}} 28 | * The first argument is not between the borders. 29 | * {{/ifBetweenLeftClosed}} 30 | * ``` 31 | * 32 | * @author Dmitriy Simushev 33 | */ 34 | class IfBetweenLeftClosedHelper extends AbstractComparisonHelper implements HelperInterface 35 | { 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | protected function evaluateCondition($args) 40 | { 41 | if (count($args) != 3) { 42 | throw new \InvalidArgumentException( 43 | '"ifBetweenLeftClosed" helper expects exactly three arguments.' 44 | ); 45 | } 46 | 47 | return ($args[0] >= $args[1]) && ($args[0] < $args[2]); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Layout/HelpersTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\Helpers; 14 | 15 | /** 16 | * Test class for Layout Helpers Set. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class HelpersTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that all helpers in the set exist and have valid classes. 24 | * 25 | * @dataProvider helpersProvider 26 | */ 27 | public function testHelper($name, $class) 28 | { 29 | $helpers = new Helpers(); 30 | 31 | $this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name)); 32 | $this->assertInstanceOf($class, $helpers->{$name}); 33 | } 34 | 35 | /** 36 | * A data provider for testHelper method. 37 | */ 38 | public function helpersProvider() 39 | { 40 | return array( 41 | array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'), 42 | array('extends', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\ExtendsHelper'), 43 | array('override', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\OverrideHelper'), 44 | array('ifOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\IfOverriddenHelper'), 45 | array('unlessOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\UnlessOverriddenHelper'), 46 | ); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Comparison/IfBetweenRightClosedHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | 15 | /** 16 | * Conditional helper that checks if one value is between two another values. 17 | * 18 | * Only right border of the interval is included into comparison. That's why in 19 | * cases where the value under comparision is equal to right border the result 20 | * of "between left closed" operator is true but when the value under compatison 21 | * is equal to left border the result of "between left closed" is false. 22 | * 23 | * Example of usage: 24 | * ```handlebars 25 | * {{#ifBetweenRightClosed value leftBorder rightBorder}} 26 | * The first argument is between borders. 27 | * {{else}} 28 | * The first argument is not between the borders. 29 | * {{/ifBetweenRightClosed}} 30 | * ``` 31 | * 32 | * @author Dmitriy Simushev 33 | */ 34 | class IfBetweenRightClosedHelper extends AbstractComparisonHelper implements HelperInterface 35 | { 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | protected function evaluateCondition($args) 40 | { 41 | if (count($args) != 3) { 42 | throw new \InvalidArgumentException( 43 | '"ifBetweenRightClosed" helper expects exactly three arguments.' 44 | ); 45 | } 46 | 47 | return ($args[0] > $args[1]) && ($args[0] <= $args[2]); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Layout/IfOverriddenHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 14 | use JustBlackBird\HandlebarsHelpers\Layout\IfOverriddenHelper; 15 | 16 | /** 17 | * Test class for "ifOverridden" helper. 18 | * 19 | * @author Dmitriy Simushev 20 | */ 21 | class IfOverriddenHelperTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Tests that exception is thrown if wrong number of arguments is used. 25 | * 26 | * @expectedException InvalidArgumentException 27 | * @dataProvider wrongArgumentsProvider 28 | */ 29 | public function testArgumentsCount($template) 30 | { 31 | $storage = new BlockStorage(); 32 | $helpers = new \Handlebars\Helpers(array('ifOverridden' => new IfOverriddenHelper($storage))); 33 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 34 | 35 | $engine->render($template, array()); 36 | } 37 | 38 | /** 39 | * A data provider for testArgumentsCount method. 40 | */ 41 | public function wrongArgumentsProvider() 42 | { 43 | return array( 44 | // Not enough arguments 45 | array('{{#ifOverridden}}true{{else}}false{{/ifOverridden}}'), 46 | // Too much arguments 47 | array('{{#ifOverridden "arg1" "arg2"}}true{{else}}false{{/ifOverridden}}'), 48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Text/RepeatHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Repeats content specified number of times. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#repeat times}}content to repeat{{/repeat}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "times": How many times content must be repeated. This value must be 27 | * greater than or equal to 0 otherwise an exception will be thrown. 28 | * 29 | * @author Dmitriy Simushev 30 | */ 31 | class RepeatHelper implements HelperInterface 32 | { 33 | /** 34 | * {@inheritdoc} 35 | */ 36 | public function execute(Template $template, Context $context, $args, $source) 37 | { 38 | $parsed_args = $template->parseArguments($args); 39 | if (count($parsed_args) != 1) { 40 | throw new \InvalidArgumentException( 41 | '"repeat" helper expects exactly one argument.' 42 | ); 43 | } 44 | 45 | $times = intval($context->get($parsed_args[0])); 46 | if ($times < 0) { 47 | throw new \InvalidArgumentException( 48 | 'The first argument of "repeat" helper has to be greater than or equal to 0.' 49 | ); 50 | } 51 | $string = $template->render($context); 52 | 53 | return str_repeat($string, $times); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Layout/BlockHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * A helper for defining default content of a block. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#block name}} 23 | * Default content for the block 24 | * {{/block}} 25 | * ``` 26 | * 27 | * Arguments: 28 | * - "name": Name of the block. 29 | * 30 | * @author Dmitriy Simushev 31 | */ 32 | class BlockHelper extends AbstractBlockHelper implements HelperInterface 33 | { 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function execute(Template $template, Context $context, $args, $source) 38 | { 39 | // Get block name 40 | $parsed_args = $template->parseArguments($args); 41 | if (count($parsed_args) != 1) { 42 | throw new \InvalidArgumentException( 43 | '"block" helper expects exactly one argument.' 44 | ); 45 | } 46 | $block_name = $context->get(array_shift($parsed_args)); 47 | 48 | // If the block is not overridden render and show the default value 49 | if (!$this->blocksStorage->has($block_name)) { 50 | return $template->render($context); 51 | } 52 | 53 | $content = $this->blocksStorage->get($block_name); 54 | 55 | // Show overridden content 56 | return $content; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/Layout/UnlessOverriddenHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 14 | use JustBlackBird\HandlebarsHelpers\Layout\UnlessOverriddenHelper; 15 | 16 | /** 17 | * Test class for "unlessOverridden" helper. 18 | * 19 | * @author Dmitriy Simushev 20 | */ 21 | class UnlessOverriddenHelperTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Tests that exception is thrown if wrong number of arguments is used. 25 | * 26 | * @expectedException InvalidArgumentException 27 | * @dataProvider wrongArgumentsProvider 28 | */ 29 | public function testArgumentsCount($template) 30 | { 31 | $storage = new BlockStorage(); 32 | $helpers = new \Handlebars\Helpers(array('unlessOverridden' => new UnlessOverriddenHelper($storage))); 33 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 34 | 35 | $engine->render($template, array()); 36 | } 37 | 38 | /** 39 | * A data provider for testArgumentsCount method. 40 | */ 41 | public function wrongArgumentsProvider() 42 | { 43 | return array( 44 | // Not enough arguments 45 | array('{{#unlessOverridden}}false{{else}}true{{/unlessOverridden}}'), 46 | // Too much arguments 47 | array('{{#unlessOverridden "arg1" "arg2"}}false{{else}}true{{/unlessOverridden}}'), 48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Text/HelpersTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\Helpers; 14 | 15 | /** 16 | * Test class for Text Helpers Set. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class HelpersTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that all helpers in the set exist and have valid classes. 24 | * 25 | * @dataProvider helpersProvider 26 | */ 27 | public function testHelper($name, $class) 28 | { 29 | $helpers = new Helpers(); 30 | 31 | $this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name)); 32 | $this->assertInstanceOf($class, $helpers->{$name}); 33 | } 34 | 35 | /** 36 | * A data provider for testHelper method. 37 | */ 38 | public function helpersProvider() 39 | { 40 | return array( 41 | array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\LowercaseHelper'), 42 | array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\UppercaseHelper'), 43 | array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'), 44 | array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'), 45 | array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'), 46 | array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'), 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Date/FormatDateHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Date; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Format date using PHP's strftime format string. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{formatDate time format}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "time": Can be either an integer timestamp or an instance of \DateTime 27 | * class. 28 | * - "format": Format string. See 29 | * {@link http://php.net/manual/en/function.strftime.php} for details about 30 | * placeholders. 31 | * 32 | * @author Dmitriy Simushev 33 | */ 34 | class FormatDateHelper implements HelperInterface 35 | { 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | public function execute(Template $template, Context $context, $args, $source) 40 | { 41 | $parsed_args = $template->parseArguments($args); 42 | if (count($parsed_args) != 2) { 43 | throw new \InvalidArgumentException( 44 | '"formatDate" helper expects exactly two arguments.' 45 | ); 46 | } 47 | 48 | $raw_time = $context->get($parsed_args[0]); 49 | if ($raw_time instanceof \DateTime) { 50 | $timestamp = $raw_time->getTimestamp(); 51 | } else { 52 | $timestamp = intval($raw_time); 53 | } 54 | $format = $context->get($parsed_args[1]); 55 | 56 | return strftime($format, $timestamp); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Comparison/IfAnyHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Comparison; 12 | 13 | use Handlebars\Helper as HelperInterface; 14 | use Handlebars\StringWrapper; 15 | 16 | /** 17 | * Conditional helper that checks if at least one argumet can be treated as 18 | * "true" value. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#ifAny first second third}} 23 | * At least one of argument can be threated as "true". 24 | * {{else}} 25 | * All values are "falsy" 26 | * {{/ifAny}} 27 | * ``` 28 | * 29 | * @author Dmitriy Simushev 30 | */ 31 | class IfAnyHelper extends AbstractComparisonHelper implements HelperInterface 32 | { 33 | /** 34 | * {@inheritdoc} 35 | */ 36 | protected function evaluateCondition($args) 37 | { 38 | if (count($args) == 0) { 39 | throw new \InvalidArgumentException( 40 | '"ifAny" helper expects at least one argument.' 41 | ); 42 | } 43 | 44 | foreach ($args as $value) { 45 | if ($value instanceof StringWrapper) { 46 | // Casting any object of \Handlebars\StringWrapper will have 47 | // false positive result even for those with empty internal 48 | // strings. Thus we need to check internal string of such 49 | // objects. 50 | $value = $value->getString(); 51 | } 52 | 53 | if ($value) { 54 | return true; 55 | } 56 | } 57 | 58 | return false; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Layout/BlockStorage.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | /** 14 | * A storage for overridable blocks' content. 15 | * 16 | * @author Dmitriy Simushev 17 | */ 18 | class BlockStorage 19 | { 20 | /** 21 | * Associative array of blocks. The keys are blocks names and the values are 22 | * blocks content. 23 | * 24 | * @type string[] 25 | */ 26 | protected $blocks = array(); 27 | 28 | /** 29 | * Gets content of a block. 30 | * 31 | * @param string $name Block's name. 32 | * @return string Block's content. 33 | */ 34 | public function get($name) 35 | { 36 | return isset($this->blocks[$name]) ? $this->blocks[$name] : false; 37 | } 38 | 39 | /** 40 | * Sets content of a block. 41 | * 42 | * @param string $name Block's name. 43 | * @param string $content Block's content. 44 | */ 45 | public function set($name, $content) 46 | { 47 | $this->blocks[$name] = $content; 48 | } 49 | 50 | /** 51 | * Checks if a block exists in the storage. 52 | * 53 | * @param string $name Block's name 54 | * @return boolean 55 | */ 56 | public function has($name) 57 | { 58 | return isset($this->blocks[$name]); 59 | } 60 | 61 | /** 62 | * Removes block from the storage. 63 | * 64 | * @param string $name Block's name. 65 | */ 66 | public function remove($name) 67 | { 68 | unset($this->blocks[$name]); 69 | } 70 | 71 | /** 72 | * Removes all blocks from the storage. 73 | */ 74 | public function clear() 75 | { 76 | $this->blocks = array(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Layout/IfOverriddenHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Conditional helper that checks if block overridden or not. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#ifOverridden name}} 23 | * The block was overridden 24 | * {{else}} 25 | * The block was not overridden 26 | * {{/ifOverridden}} 27 | * ``` 28 | * 29 | * Arguments: 30 | * - "name": Name of the block. 31 | * 32 | * @author Dmitriy Simushev 33 | */ 34 | class IfOverriddenHelper extends AbstractBlockHelper implements HelperInterface 35 | { 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | public function execute(Template $template, Context $context, $args, $source) 40 | { 41 | // Get block name 42 | $parsed_args = $template->parseArguments($args); 43 | if (count($parsed_args) != 1) { 44 | throw new \InvalidArgumentException( 45 | '"ifOverridden" helper expects exactly one argument.' 46 | ); 47 | } 48 | $block_name = $context->get(array_shift($parsed_args)); 49 | 50 | // Check condition and render blocks 51 | if ($this->blocksStorage->has($block_name)) { 52 | $template->setStopToken('else'); 53 | $buffer = $template->render($context); 54 | $template->setStopToken(false); 55 | } else { 56 | $template->setStopToken('else'); 57 | $template->discard(); 58 | $template->setStopToken(false); 59 | $buffer = $template->render($context); 60 | } 61 | 62 | return $buffer; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Layout/OverrideHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * A helper for overriding content of a block. 19 | * 20 | * One or more "override" blocks must be wrapped with 21 | * {@link \JustBlackBird\HandlebarsHelpers\Layout\ExtendsHelper} helper. 22 | * 23 | * Usage: 24 | * ```handlebars 25 | * {{#override blockName}} 26 | * Overridden content of the block. 27 | * {{/override}} 28 | * ``` 29 | * 30 | * Arguments: 31 | * - blockName: Name of the block which should be overridden. 32 | * 33 | * @author Dmitriy Simushev 34 | */ 35 | class OverrideHelper extends AbstractBlockHelper implements HelperInterface 36 | { 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function execute(Template $template, Context $context, $args, $source) 41 | { 42 | // Get block name 43 | $parsed_args = $template->parseArguments($args); 44 | if (count($parsed_args) != 1) { 45 | throw new \InvalidArgumentException( 46 | '"override" helper expects exactly one argument.' 47 | ); 48 | } 49 | $block_name = $context->get(array_shift($parsed_args)); 50 | 51 | // We need to provide unlimited inheritence level. Rendering is started 52 | // from the deepest level template. If the content is in the block 53 | // storage it is related with the deepest level template. Thus we do not 54 | // need to override it. 55 | if (!$this->blocksStorage->has($block_name)) { 56 | $this->blocksStorage->set($block_name, $template->render($context)); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Layout/UnlessOverriddenHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Conditional helper that checks if block overridden or not. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#unlessOverridden name}} 23 | * The block was not overridden 24 | * {{else}} 25 | * The block was overridden 26 | * {{/unlessOverridden}} 27 | * ``` 28 | * 29 | * Arguments: 30 | * - "name": Name of the block. 31 | * 32 | * @author Dmitriy Simushev 33 | */ 34 | class UnlessOverriddenHelper extends AbstractBlockHelper implements HelperInterface 35 | { 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | public function execute(Template $template, Context $context, $args, $source) 40 | { 41 | // Get block name 42 | $parsed_args = $template->parseArguments($args); 43 | if (count($parsed_args) != 1) { 44 | throw new \InvalidArgumentException( 45 | '"unlessOverridden" helper expects exactly one argument.' 46 | ); 47 | } 48 | $block_name = $context->get(array_shift($parsed_args)); 49 | 50 | // Check condition and render blocks 51 | if (!$this->blocksStorage->has($block_name)) { 52 | $template->setStopToken('else'); 53 | $buffer = $template->render($context); 54 | $template->setStopToken(false); 55 | } else { 56 | $template->setStopToken('else'); 57 | $template->discard(); 58 | $template->setStopToken(false); 59 | $buffer = $template->render($context); 60 | } 61 | 62 | return $buffer; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Collection/FirstHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Collection; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Returns the first item of the collection. 19 | * 20 | * If the passed in collection is empty boolean false will be returned. 21 | * 22 | * Usage: 23 | * ```handlebars 24 | * {{first collection}} 25 | * ``` 26 | * 27 | * Arguments: 28 | * - "collection": an array or an instance of \Traversable which first element 29 | * should be returned. 30 | * 31 | * @author Dmitriy Simushev 32 | */ 33 | class FirstHelper implements HelperInterface 34 | { 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | public function execute(Template $template, Context $context, $args, $source) 39 | { 40 | $parsed_args = $template->parseArguments($args); 41 | if (count($parsed_args) != 1) { 42 | throw new \InvalidArgumentException( 43 | '"first" helper expects exactly one argument.' 44 | ); 45 | } 46 | 47 | $collection = $context->get($parsed_args[0]); 48 | if (!is_array($collection) && !($collection instanceof \Traversable)) { 49 | throw new \InvalidArgumentException('Wrong type of the argument in the "first" helper.'); 50 | } 51 | 52 | if (is_array($collection)) { 53 | return reset($collection); 54 | } 55 | 56 | // "reset" function does not work with \Traversable in HHVM. Thus we 57 | // need to get the element manually. 58 | while ($collection instanceof \IteratorAggregate) { 59 | $collection = $collection->getIterator(); 60 | } 61 | $collection->rewind(); 62 | 63 | return $collection->valid() ? $collection->current() : false; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Collection/LastHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Collection; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Returns the last item of the collection. 19 | * 20 | * If the passed in collection is empty boolean false will be returned. 21 | * 22 | * Usage: 23 | * ```handlebars 24 | * {{last collection}} 25 | * ``` 26 | * 27 | * Arguments: 28 | * - "collection": an array or an instance of \Traversable which last element 29 | * should be returned. 30 | * 31 | * @author Dmitriy Simushev 32 | */ 33 | class LastHelper implements HelperInterface 34 | { 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | public function execute(Template $template, Context $context, $args, $source) 39 | { 40 | $parsed_args = $template->parseArguments($args); 41 | if (count($parsed_args) != 1) { 42 | throw new \InvalidArgumentException( 43 | '"last" helper expects exactly one argument.' 44 | ); 45 | } 46 | 47 | $collection = $context->get($parsed_args[0]); 48 | if (!is_array($collection) && !($collection instanceof \Traversable)) { 49 | throw new \InvalidArgumentException('Wrong type of the argument in the "last" helper.'); 50 | } 51 | 52 | if (is_array($collection)) { 53 | return end($collection); 54 | } 55 | 56 | // "end" function does not work with \Traversable in HHVM. Thus we 57 | // need to get the element manually. 58 | while ($collection instanceof \IteratorAggregate) { 59 | $collection = $collection->getIterator(); 60 | } 61 | 62 | $collection->rewind(); 63 | $item = false; 64 | while ($collection->valid()) { 65 | $item = $collection->current(); 66 | $collection->next(); 67 | } 68 | 69 | return $item; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Helpers.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers; 12 | 13 | use Handlebars\Helpers as BaseHelpers; 14 | 15 | /** 16 | * Contains all helpers. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class Helpers extends BaseHelpers 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | protected function addDefaultHelpers() 26 | { 27 | parent::addDefaultHelpers(); 28 | 29 | // Date helpers 30 | $this->add('formatDate', new Date\FormatDateHelper()); 31 | 32 | // Collection helpers 33 | $this->add('count', new Collection\CountHelper()); 34 | $this->add('first', new Collection\FirstHelper()); 35 | $this->add('last', new Collection\LastHelper()); 36 | 37 | // Comparison helpers 38 | $this->add('ifAny', new Comparison\IfAnyHelper()); 39 | $this->add('ifEqual', new Comparison\IfEqualHelper()); 40 | $this->add('ifEven', new Comparison\IfEvenHelper()); 41 | $this->add('ifOdd', new Comparison\IfOddHelper()); 42 | $this->add('unlessEqual', new Comparison\UnlessEqualHelper()); 43 | 44 | // Text helpers 45 | $this->add('lowercase', new Text\LowercaseHelper()); 46 | $this->add('uppercase', new Text\UppercaseHelper()); 47 | $this->add('repeat', new Text\RepeatHelper()); 48 | $this->add('replace', new Text\ReplaceHelper()); 49 | $this->add('truncate', new Text\TruncateHelper()); 50 | $this->add('ellipsis', new Text\EllipsisHelper()); 51 | 52 | // Layout helpers 53 | $storage = new Layout\BlockStorage(); 54 | $this->add('block', new Layout\BlockHelper($storage)); 55 | $this->add('extends', new Layout\ExtendsHelper($storage)); 56 | $this->add('override', new Layout\OverrideHelper($storage)); 57 | $this->add('ifOverridden', new Layout\IfOverriddenHelper($storage)); 58 | $this->add('unlessOverridden', new Layout\UnlessOverriddenHelper($storage)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Text/TruncateHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Truncates a string to specified length. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{truncate string length append}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "string": A string that must be truncated. 27 | * - "length": A number of characters to limit the string. 28 | * - "append": A string to append if charaters are omitted. Default value is an 29 | * empty string. 30 | * 31 | * @author Dmitriy Simushev 32 | */ 33 | class TruncateHelper implements HelperInterface 34 | { 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | public function execute(Template $template, Context $context, $args, $source) 39 | { 40 | $parsed_args = $template->parseArguments($args); 41 | if (count($parsed_args) < 2 || count($parsed_args) > 3) { 42 | throw new \InvalidArgumentException( 43 | '"truncate" helper expects two or three arguments.' 44 | ); 45 | } 46 | 47 | $string = (string)$context->get($parsed_args[0]); 48 | $length = intval($context->get($parsed_args[1])); 49 | $append = isset($parsed_args[2]) ? (string)$context->get($parsed_args[2]) : ''; 50 | 51 | if ($length < 0) { 52 | throw new \InvalidArgumentException( 53 | 'The second argument of "truncate" helper has to be greater than or equal to 0.' 54 | ); 55 | } 56 | 57 | if ($append && strlen($append) > $length) { 58 | throw new \InvalidArgumentException( 59 | 'Cannot truncate string. Length of append value is greater than target length.' 60 | ); 61 | } 62 | 63 | if (strlen($string) > $length) { 64 | return substr($string, 0, $length - strlen($append)) . $append; 65 | } else { 66 | return $string; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/Comparison/HelpersTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\Helpers; 14 | 15 | /** 16 | * Test class for Comparison Helpers Set. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class HelpersTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that all helpers in the set exist and have valid classes. 24 | * 25 | * @dataProvider helpersProvider 26 | */ 27 | public function testHelper($name, $class) 28 | { 29 | $helpers = new Helpers(); 30 | 31 | $this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name)); 32 | $this->assertInstanceOf($class, $helpers->{$name}); 33 | } 34 | 35 | /** 36 | * A data provider for testHelper method. 37 | */ 38 | public function helpersProvider() 39 | { 40 | return array( 41 | array('ifAny', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfAnyHelper'), 42 | array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'), 43 | array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'), 44 | array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'), 45 | array('ifLess', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfLessHelper'), 46 | array('ifMore', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfMoreHelper'), 47 | array('ifBetween', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenHelper'), 48 | array('ifBetweenClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenClosedHelper'), 49 | array('ifBetweenLeftClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenLeftClosedHelper'), 50 | array('ifBetweenRightClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenRightClosedHelper'), 51 | array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'), 52 | ); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Layout/ExtendsHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Layout; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * A helper for templates extending. 19 | * 20 | * This helper works as a wrapper for one or more "override" blocks which 21 | * defines name of the parent template. 22 | * 23 | * Example of usage: 24 | * ```handlebars 25 | * {{#extends parentTemplateName}} 26 | * {{#override "blockName"}} 27 | * Overridden block content 28 | * {{/override}} 29 | * {{/extends}} 30 | * ``` 31 | * 32 | * Arguments: 33 | * - "parentTemplateName": Name of the template that should be extended. 34 | * 35 | * @author Dmitriy Simushev 36 | */ 37 | class ExtendsHelper extends AbstractBlockHelper implements HelperInterface 38 | { 39 | /** 40 | * The current inheritance level of the templates. 41 | * @var int 42 | */ 43 | protected $level = 0; 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function execute(Template $template, Context $context, $args, $source) 49 | { 50 | // Get name of the parent template 51 | $parsed_args = $template->parseArguments($args); 52 | if (count($parsed_args) != 1) { 53 | throw new \InvalidArgumentException( 54 | '"extends" helper expects exactly one argument.' 55 | ); 56 | } 57 | $parent_template = $context->get(array_shift($parsed_args)); 58 | 59 | // Render content inside "extends" block to override blocks 60 | $template->render($context); 61 | 62 | // We need another instance of \Handlebars\Template to render parent 63 | // template. It can be got from Handlebars engine, so get the engine. 64 | $handlebars = $template->getEngine(); 65 | 66 | // Render the parent template 67 | $this->level++; 68 | $buffer = $handlebars->render($parent_template, $context); 69 | $this->level--; 70 | 71 | if ($this->level == 0) { 72 | // The template and all its parents are rendered. Clean up the 73 | // storage. 74 | $this->blocksStorage->clear(); 75 | } 76 | 77 | return $buffer; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/Text/LowercaseHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\LowercaseHelper; 14 | 15 | /** 16 | * Test class for "lowercase" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class LowercaseHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that strings are converted to lowercase properly. 24 | * 25 | * @dataProvider convertProvider 26 | */ 27 | public function testConvert($string, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('lowercase' => new LowercaseHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{lowercase str}}', 35 | array('str' => $string) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testConvert method. 43 | */ 44 | public function convertProvider() 45 | { 46 | return array( 47 | array('already in lowercase', 'already in lowercase'), 48 | array('Mixed Case String', 'mixed case string'), 49 | array('ANOther mIxed CASE string', 'another mixed case string'), 50 | array('STRING IN CAPS', 'string in caps'), 51 | ); 52 | } 53 | 54 | /** 55 | * Tests that exception is thrown if wrong number of arguments is used. 56 | * 57 | * @expectedException InvalidArgumentException 58 | * @dataProvider wrongArgumentsProvider 59 | */ 60 | public function testArgumentsCount($template) 61 | { 62 | $helpers = new \Handlebars\Helpers(array('lowercase' => new LowercaseHelper())); 63 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 64 | 65 | $engine->render($template, array()); 66 | } 67 | 68 | /** 69 | * A data provider for testArgumentsCount method. 70 | */ 71 | public function wrongArgumentsProvider() 72 | { 73 | return array( 74 | // Not enough arguments 75 | array('{{lowercase}}'), 76 | // Too much arguments 77 | array('{{lowercase "Arg" "ANOTHER ARG"}}'), 78 | ); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /tests/Text/UppercaseHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\UppercaseHelper; 14 | 15 | /** 16 | * Test class for "uppercase" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class UppercaseHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that strings are converted to uppercase properly. 24 | * 25 | * @dataProvider convertProvider 26 | */ 27 | public function testConvert($string, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('uppercase' => new UppercaseHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{uppercase str}}', 35 | array('str' => $string) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testConvert method. 43 | */ 44 | public function convertProvider() 45 | { 46 | return array( 47 | array('ALREADY IN UPPERCASE', 'ALREADY IN UPPERCASE'), 48 | array('Mixed Case String', 'MIXED CASE STRING'), 49 | array('ANOther mIxed CASE string', 'ANOTHER MIXED CASE STRING'), 50 | array('string in lowercase', 'STRING IN LOWERCASE'), 51 | ); 52 | } 53 | 54 | /** 55 | * Tests that exception is thrown if wrong number of arguments is used. 56 | * 57 | * @expectedException InvalidArgumentException 58 | * @dataProvider wrongArgumentsProvider 59 | */ 60 | public function testArgumentsCount($template) 61 | { 62 | $helpers = new \Handlebars\Helpers(array('uppercase' => new UppercaseHelper())); 63 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 64 | 65 | $engine->render($template, array()); 66 | } 67 | 68 | /** 69 | * A data provider for testArgumentsCount method. 70 | */ 71 | public function wrongArgumentsProvider() 72 | { 73 | return array( 74 | // Not enough arguments 75 | array('{{uppercase}}'), 76 | // Too much arguments 77 | array('{{uppercase "Arg" "ANOTHER ARG"}}'), 78 | ); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /tests/Text/ReplaceHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\ReplaceHelper; 14 | 15 | /** 16 | * Test class for "replace" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class ReplaceHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that strings are repeated properly. 24 | * 25 | * @dataProvider replaceProvider 26 | */ 27 | public function testReplace($string, $search, $replacement, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('replace' => new ReplaceHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#replace search replacement}}{{str}}{{/replace}}', 35 | array('str' => $string, 'search' => $search, 'replacement' => $replacement) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testReplace method. 43 | */ 44 | public function replaceProvider() 45 | { 46 | return array( 47 | array('abcd', 'b', '', 'acd'), 48 | array('abcd', 'xyz', '', 'abcd'), 49 | array('abcd', '', 'asd', 'abcd'), 50 | ); 51 | } 52 | 53 | /** 54 | * Tests that exception is thrown if wrong number of arguments is used. 55 | * 56 | * @expectedException InvalidArgumentException 57 | * @dataProvider wrongArgumentsSetProvider 58 | */ 59 | public function testArgumentsCount($template) 60 | { 61 | $helpers = new \Handlebars\Helpers(array('replace' => new ReplaceHelper())); 62 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 63 | 64 | $engine->render($template, array()); 65 | } 66 | 67 | /** 68 | * A data provider for testArgumentsCount method. 69 | */ 70 | public function wrongArgumentsSetProvider() 71 | { 72 | return array( 73 | // Not enough arguments 74 | array('{{#replace}}str{{/replace}}'), 75 | array('{{#replace "serach"}}str{{/replace}}'), 76 | // Too much arguments 77 | array('{{#replace "search" "replacement" "asd"}}str{{/replace}}'), 78 | ); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Text/EllipsisHelper.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Text; 12 | 13 | use Handlebars\Context; 14 | use Handlebars\Helper as HelperInterface; 15 | use Handlebars\Template; 16 | 17 | /** 18 | * Truncates a string to specified length in words. 19 | * 20 | * Usage: 21 | * ```handlebars 22 | * {{#ellipsis string length append}} 23 | * ``` 24 | * 25 | * Arguments: 26 | * - "string": A string that must be truncated. 27 | * - "length": A number of words to limit the string. 28 | * - "append": A string to append if charaters are omitted. Default value is an 29 | * empty string. 30 | * 31 | * @author Matteo Merola 32 | */ 33 | class EllipsisHelper implements HelperInterface 34 | { 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | public function execute(Template $template, Context $context, $args, $source) 39 | { 40 | $parsed_args = $template->parseArguments($args); 41 | if (count($parsed_args) < 2 || count($parsed_args) > 3) { 42 | throw new \InvalidArgumentException( 43 | '"ellipsis" helper expects two or three arguments.' 44 | ); 45 | } 46 | $var_content = (string)$context->get($parsed_args[0]); 47 | $limit = intval($context->get($parsed_args[1])); 48 | $ellipsis = isset($parsed_args[2]) ? (string)$context->get($parsed_args[2]) : ''; 49 | if ($limit === 0) { 50 | return $ellipsis; 51 | } 52 | if ($limit < 0) { 53 | throw new \InvalidArgumentException( 54 | 'The second argument of "ellipsis" helper has to be greater than or equal to 0.' 55 | ); 56 | } 57 | $words = str_word_count($var_content, 2); 58 | $value = ""; 59 | if (count($words) > $limit) { 60 | $permitted = array_slice($words, 0, $limit, true); 61 | end($permitted); 62 | $last_word_position = key($permitted); 63 | $last_word = $permitted[$last_word_position]; 64 | $last_word_length = strlen($last_word); 65 | $real_limit = $last_word_position + $last_word_length; 66 | $value = substr($var_content, 0, $real_limit); 67 | } else { 68 | $value .= $var_content; 69 | } 70 | if ($ellipsis) { 71 | $value .= $ellipsis; 72 | } 73 | 74 | return $value; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/Comparison/IfOddHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfOddHelper; 14 | 15 | /** 16 | * Test class for "ifOdd" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfOddHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $is_even) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifOdd' => new IfOddHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifOdd value}}false{{else}}true{{/ifOdd}}', 35 | array('value' => $value) 36 | ), 37 | $is_even ? 'true' : 'false' 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testCondition method. 43 | */ 44 | public function conditionProvider() 45 | { 46 | return array( 47 | // Even values but with different types 48 | array(2, true), 49 | array("8", true), 50 | // Zero is even number 51 | array(0, true), 52 | // Null should be treated as zero so it's an even value too. 53 | array(null, true), 54 | // Odd values with different types 55 | array(1, false), 56 | array("17", false), 57 | ); 58 | } 59 | 60 | /** 61 | * Tests that exception is thrown if wrong number of arguments is used. 62 | * 63 | * @expectedException InvalidArgumentException 64 | * @dataProvider wrongArgumentsProvider 65 | */ 66 | public function testArgumentsCount($template) 67 | { 68 | $helpers = new \Handlebars\Helpers(array('ifOdd' => new IfOddHelper())); 69 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 70 | 71 | $engine->render($template, array()); 72 | } 73 | 74 | /** 75 | * A data provider for testArgumentsCount method. 76 | */ 77 | public function wrongArgumentsProvider() 78 | { 79 | return array( 80 | // Not enough arguments 81 | array('{{#ifOdd}}yes{{else}}no{{/ifOdd}}'), 82 | // Too much arguments 83 | array('{{#ifOdd 2 4}}yes{{else}}no{{/ifOdd}}'), 84 | ); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /tests/Comparison/IfEvenHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfEvenHelper; 14 | 15 | /** 16 | * Test class for "ifEven" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfEventHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $is_even) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifEven' => new IfEvenHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifEven value}}true{{else}}false{{/ifEven}}', 35 | array('value' => $value) 36 | ), 37 | $is_even ? 'true' : 'false' 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testCondition method. 43 | */ 44 | public function conditionProvider() 45 | { 46 | return array( 47 | // Even values but with different types 48 | array(2, true), 49 | array("8", true), 50 | // Zero is even number 51 | array(0, true), 52 | // Null should be treated as zero so it's an even value too. 53 | array(null, true), 54 | // Odd values with different types 55 | array(1, false), 56 | array("17", false), 57 | ); 58 | } 59 | 60 | /** 61 | * Tests that exception is thrown if wrong number of arguments is used. 62 | * 63 | * @expectedException InvalidArgumentException 64 | * @dataProvider wrongArgumentsProvider 65 | */ 66 | public function testArgumentsCount($template) 67 | { 68 | $helpers = new \Handlebars\Helpers(array('ifEven' => new IfEvenHelper())); 69 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 70 | 71 | $engine->render($template, array()); 72 | } 73 | 74 | /** 75 | * A data provider for testArgumentsCount method. 76 | */ 77 | public function wrongArgumentsProvider() 78 | { 79 | return array( 80 | // Not enough arguments 81 | array('{{#ifEven}}yes{{else}}no{{/ifEven}}'), 82 | // Too much arguments 83 | array('{{#ifEven 2 4}}yes{{else}}no{{/ifEven}}'), 84 | ); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /tests/Comparison/UnlessEqualHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\UnlessEqualHelper; 14 | 15 | /** 16 | * Test class for "unlessEqual" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class UnlessEqualHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($left, $right, $is_equal) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('unlessEqual' => new UnlessEqualHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#unlessEqual left right}}false{{else}}true{{/unlessEqual}}', 35 | array('left' => $left, 'right' => $right) 36 | ), 37 | $is_equal ? 'true' : 'false' 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testCondition method. 43 | */ 44 | public function conditionProvider() 45 | { 46 | return array( 47 | // Same values 48 | array(123, 123, true), 49 | // Equal values but with different types 50 | array(123, "123", true), 51 | // One more type convertion check 52 | array(0, false, true), 53 | // Different values 54 | array(123, false, false), 55 | ); 56 | } 57 | 58 | /** 59 | * Tests that exception is thrown if wrong number of arguments is used. 60 | * 61 | * @expectedException InvalidArgumentException 62 | * @dataProvider wrongArgumentsProvider 63 | */ 64 | public function testArgumentsCount($template) 65 | { 66 | $helpers = new \Handlebars\Helpers(array('unlessEqual' => new UnlessEqualHelper())); 67 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 68 | 69 | $engine->render($template, array()); 70 | } 71 | 72 | /** 73 | * A data provider for testArgumentsCount method. 74 | */ 75 | public function wrongArgumentsProvider() 76 | { 77 | return array( 78 | // Not enough arguments 79 | array('{{#unlessEqual}}no{{else}}yes{{/unlessEqual}}'), 80 | // Too much arguments 81 | array('{{#unlessEqual 2 4 8}}no{{else}}yes{{/unlessEqual}}'), 82 | ); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /tests/Date/FormatDateHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Date; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Date\FormatDateHelper; 14 | 15 | /** 16 | * Test class for "formatDate" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class FormatDateHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that date is formatted properly. 24 | * 25 | * @dataProvider formatProvider 26 | */ 27 | public function testFormat($time, $format, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('formatDate' => new FormatDateHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals($engine->render( 33 | '{{formatDate time format}}', 34 | array( 35 | 'time' => $time, 36 | 'format' => $format, 37 | ) 38 | ), $result); 39 | } 40 | 41 | /** 42 | * A data provider for testFormat method. 43 | */ 44 | public function formatProvider() 45 | { 46 | $now = new \DateTime(); 47 | $format = "%H:%M %d-%m-%Y"; 48 | $expected = strftime($format, $now->getTimestamp()); 49 | 50 | return array( 51 | // DateTime object 52 | array($now, $format, $expected), 53 | // Integer timestamp 54 | array($now->getTimestamp(), $format, $expected), 55 | // String timestamp 56 | array((string)$now->getTimestamp(), $format, $expected), 57 | ); 58 | } 59 | 60 | /** 61 | * Tests that exception is thrown if wrong number of arguments is used. 62 | * 63 | * @expectedException InvalidArgumentException 64 | * @dataProvider wrongArgumentsProvider 65 | */ 66 | public function testArgumentsCount($template) 67 | { 68 | $helpers = new \Handlebars\Helpers(array('formatDate' => new FormatDateHelper())); 69 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 70 | 71 | $engine->render($template, array()); 72 | } 73 | 74 | /** 75 | * A data provider for FormatDateHelperTest::testArgumentsCount() test. 76 | */ 77 | public function wrongArgumentsProvider() 78 | { 79 | return array( 80 | // Not enough arguments 81 | array('{{formatDate 658983600}}'), 82 | // Too much arguments 83 | array('{{formatDate 658983600 "%F" "test"}}'), 84 | ); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /tests/Comparison/IfEqualHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfEqualHelper; 14 | 15 | /** 16 | * Test class for "ifEqual" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfEqualHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($left, $right, $is_equal) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifEqual' => new IfEqualHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifEqual left right}}true{{else}}false{{/ifEqual}}', 35 | array('left' => $left, 'right' => $right) 36 | ), 37 | $is_equal ? 'true' : 'false' 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testCondition method. 43 | */ 44 | public function conditionProvider() 45 | { 46 | return array( 47 | // Same values 48 | array(123, 123, true), 49 | // Equal values but with different types 50 | array(123, "123", true), 51 | // One more type convertion check 52 | array(0, false, true), 53 | // Different values 54 | array(123, false, false), 55 | ); 56 | } 57 | 58 | /** 59 | * Tests that exception is thrown if wrong number of arguments is used. 60 | * 61 | * @expectedException InvalidArgumentException 62 | * @dataProvider wrongArgumentsProvider 63 | */ 64 | public function testArgumentsCount($template) 65 | { 66 | $helpers = new \Handlebars\Helpers(array('ifEqual' => new IfEqualHelper())); 67 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 68 | 69 | $engine->render($template, array()); 70 | } 71 | 72 | /** 73 | * A data provider for testArgumentsCount method. 74 | */ 75 | public function wrongArgumentsProvider() 76 | { 77 | return array( 78 | // Not enough arguments 79 | array('{{#ifEqual}}yes{{else}}no{{/ifEqual}}'), 80 | array('{{#ifEqual 5}}yes{{else}}no{{/ifEqual}}'), 81 | // Too much arguments 82 | array('{{#ifEqual 2 4 8}}yes{{else}}no{{/ifEqual}}'), 83 | ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tests/Text/RepeatHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\RepeatHelper; 14 | 15 | /** 16 | * Test class for "repeat" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class RepeatHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that strings are repeated properly. 24 | * 25 | * @dataProvider repeatProvider 26 | */ 27 | public function testRepeat($string, $times, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#repeat times}}{{str}}{{/repeat}}', 35 | array('times' => $times, 'str' => $string) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testRepeat method. 43 | */ 44 | public function repeatProvider() 45 | { 46 | return array( 47 | array('+', 0, ''), 48 | array('+', 3, '+++'), 49 | array('', 3, ''), 50 | ); 51 | } 52 | 53 | /** 54 | * Tests that exception is thrown if wrong number of arguments is used. 55 | * 56 | * @expectedException InvalidArgumentException 57 | * @dataProvider wrongArgumentsSetProvider 58 | */ 59 | public function testArgumentsCount($template) 60 | { 61 | $helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper())); 62 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 63 | 64 | $engine->render($template, array()); 65 | } 66 | 67 | /** 68 | * A data provider for testArgumentsCount method. 69 | */ 70 | public function wrongArgumentsSetProvider() 71 | { 72 | return array( 73 | // Not enough arguments 74 | array('{{#repeat}}{{/repeat}}'), 75 | // Too much arguments 76 | array('{{#repeat 10 "ANOTHER ARG"}}{{/repeat}}'), 77 | ); 78 | } 79 | 80 | /** 81 | * Tests that exception is thrown if arguments are invalid. 82 | * 83 | * @expectedException InvalidArgumentException 84 | */ 85 | public function testInvalidArguments() 86 | { 87 | $helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper())); 88 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 89 | 90 | $engine->render('{{#repeat -10}}+{{/repeat}}', array()); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tests/Comparison/IfLessHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfLessHelper; 14 | 15 | /** 16 | * Test class for "ifLess" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfLessHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $border, $is_less) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifLess' => new IfLessHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifLess value border}}true{{else}}false{{/ifLess}}', 35 | array( 36 | 'value' => $value, 37 | 'border' => $border, 38 | ) 39 | ), 40 | $is_less ? 'true' : 'false' 41 | ); 42 | } 43 | 44 | /** 45 | * A data provider for testCondition method. 46 | */ 47 | public function conditionProvider() 48 | { 49 | return array( 50 | // Less values with different types. 51 | array(2, 10, true), 52 | array("8", "12", true), 53 | // Equal values with different types. 54 | array(0, 0, false), 55 | array("42", "42", false), 56 | // Greater values with different types. 57 | array(75, 10, false), 58 | array("17", "2", false), 59 | ); 60 | } 61 | 62 | /** 63 | * Tests that exception is thrown if wrong number of arguments is used. 64 | * 65 | * @expectedException InvalidArgumentException 66 | * @dataProvider wrongArgumentsProvider 67 | */ 68 | public function testArgumentsCount($template) 69 | { 70 | $helpers = new \Handlebars\Helpers(array('ifLess' => new IfLessHelper())); 71 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 72 | 73 | $engine->render($template, array()); 74 | } 75 | 76 | /** 77 | * A data provider for testArgumentsCount method. 78 | */ 79 | public function wrongArgumentsProvider() 80 | { 81 | return array( 82 | // Not enough arguments 83 | array('{{#ifLess}}yes{{else}}no{{/ifLess}}'), 84 | // Still not enough arguments 85 | array('{{#ifLess 1}}yes{{else}}no{{/ifLess}}'), 86 | // Too much arguments 87 | array('{{#ifLess 1 2 3}}yes{{else}}no{{/ifLess}}'), 88 | ); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /tests/Comparison/IfMoreHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfMoreHelper; 14 | 15 | /** 16 | * Test class for "ifMore" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfMoreHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $border, $is_less) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifMore' => new IfMoreHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifMore value border}}true{{else}}false{{/ifMore}}', 35 | array( 36 | 'value' => $value, 37 | 'border' => $border, 38 | ) 39 | ), 40 | $is_less ? 'true' : 'false' 41 | ); 42 | } 43 | 44 | /** 45 | * A data provider for testCondition method. 46 | */ 47 | public function conditionProvider() 48 | { 49 | return array( 50 | // Less values with different types. 51 | array(3, 18, false), 52 | array("42", "314", false), 53 | // Equal values with different types. 54 | array(0, 0, false), 55 | array("42", "42", false), 56 | // More values with different types. 57 | array(89, 1, true), 58 | array("34", "33", true), 59 | ); 60 | } 61 | 62 | /** 63 | * Tests that exception is thrown if wrong number of arguments is used. 64 | * 65 | * @expectedException InvalidArgumentException 66 | * @dataProvider wrongArgumentsProvider 67 | */ 68 | public function testArgumentsCount($template) 69 | { 70 | $helpers = new \Handlebars\Helpers(array('ifMore' => new IfMoreHelper())); 71 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 72 | 73 | $engine->render($template, array()); 74 | } 75 | 76 | /** 77 | * A data provider for testArgumentsCount method. 78 | */ 79 | public function wrongArgumentsProvider() 80 | { 81 | return array( 82 | // Not enough arguments 83 | array('{{#ifMore}}yes{{else}}no{{/ifMore}}'), 84 | // Still not enough arguments 85 | array('{{#ifMore 1}}yes{{else}}no{{/ifMore}}'), 86 | // Too much arguments 87 | array('{{#ifMore 1 2 3}}yes{{else}}no{{/ifMore}}'), 88 | ); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /tests/Comparison/IfBetweenHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenHelper; 14 | 15 | /** 16 | * Test class for "ifBetween" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfBetweenHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $left, $right, $is_between) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifBetween' => new IfBetweenHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifBetween value left right}}true{{else}}false{{/ifBetween}}', 35 | array( 36 | 'value' => $value, 37 | 'left' => $left, 38 | 'right' => $right, 39 | ) 40 | ), 41 | $is_between ? 'true' : 'false' 42 | ); 43 | } 44 | 45 | /** 46 | * A data provider for testCondition method. 47 | */ 48 | public function conditionProvider() 49 | { 50 | return array( 51 | // The value is less than left border. 52 | array(2, 10, 12, false), 53 | // The value equals to the left border. 54 | array(0, 0, 42, false), 55 | // The value equals to the right border. 56 | array(9, 0, 9, false), 57 | // The value is more than the right border. 58 | array(75, 10, 12, false), 59 | // The value is between borders. 60 | array(58, 11, 134, true), 61 | ); 62 | } 63 | 64 | /** 65 | * Tests that exception is thrown if wrong number of arguments is used. 66 | * 67 | * @expectedException InvalidArgumentException 68 | * @dataProvider wrongArgumentsProvider 69 | */ 70 | public function testArgumentsCount($template) 71 | { 72 | $helpers = new \Handlebars\Helpers(array('ifBetween' => new IfBetweenHelper())); 73 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 74 | 75 | $engine->render($template, array()); 76 | } 77 | 78 | /** 79 | * A data provider for testArgumentsCount method. 80 | */ 81 | public function wrongArgumentsProvider() 82 | { 83 | return array( 84 | // Not enough arguments 85 | array('{{#ifBetween}}yes{{else}}no{{/ifBetween}}'), 86 | // Still not enough arguments 87 | array('{{#ifBetween 1}}yes{{else}}no{{/ifBetween}}'), 88 | array('{{#ifBetween 1 2}}yes{{else}}no{{/ifBetween}}'), 89 | // Too much arguments 90 | array('{{#ifBetween 1 2 3 4}}yes{{else}}no{{/ifBetween}}'), 91 | ); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/HelpersTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Helpers; 14 | 15 | /** 16 | * Test class for Global Helpers Set. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class HelpersTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that all helpers in the set exist and have valid classes. 24 | * 25 | * @dataProvider helpersProvider 26 | */ 27 | public function testHelper($name, $class) 28 | { 29 | $helpers = new Helpers(); 30 | 31 | $this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name)); 32 | $this->assertInstanceOf($class, $helpers->{$name}); 33 | } 34 | 35 | /** 36 | * A data provider for testHelper method. 37 | */ 38 | public function helpersProvider() 39 | { 40 | return array( 41 | // Date helpers 42 | array('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'), 43 | 44 | // Collection helpers 45 | array('count', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\CountHelper'), 46 | array('first', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\FirstHelper'), 47 | array('last', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\LastHelper'), 48 | 49 | // Comparison helpers 50 | array('ifAny', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfAnyHelper'), 51 | array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'), 52 | array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'), 53 | array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'), 54 | array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'), 55 | 56 | // Text helpers 57 | array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\LowercaseHelper'), 58 | array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\UppercaseHelper'), 59 | array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'), 60 | array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'), 61 | array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'), 62 | array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'), 63 | 64 | // Layout helpers 65 | array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'), 66 | array('extends', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\ExtendsHelper'), 67 | array('override', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\OverrideHelper'), 68 | array('ifOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\IfOverriddenHelper'), 69 | array('unlessOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\UnlessOverriddenHelper'), 70 | ); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /tests/Comparison/IfAnyHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfAnyHelper; 14 | 15 | /** 16 | * Test class for "ifAny" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfAnyHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($template, $data, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifAny' => new IfAnyHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals($engine->render($template, $data), $result); 33 | } 34 | 35 | /** 36 | * A data provider for testCondition method. 37 | */ 38 | public function conditionProvider() 39 | { 40 | return array( 41 | // Single argument. It's an analog of "if" helper. 42 | array('{{#ifAny a}}true{{else}}false{{/ifAny}}', array('a' => true), 'true'), 43 | array('{{#ifAny a}}true{{else}}false{{/ifAny}}', array('a' => false), 'false'), 44 | // Multiple arguments (positive) 45 | array( 46 | '{{#ifAny a b c}}true{{else}}false{{/ifAny}}', 47 | array('a' => false, 'b' => true, 'c' => false), 48 | 'true', 49 | ), 50 | // Multiple arguments (negative) 51 | array( 52 | '{{#ifAny a b c}}true{{else}}false{{/ifAny}}', 53 | array('a' => false, 'b' => false, 'c' => false), 54 | 'false', 55 | ), 56 | // Multiple arguments (negative). Check different falsy values. 57 | array( 58 | '{{#ifAny a b c d e}}true{{else}}false{{/ifAny}}', 59 | array( 60 | 'a' => 0, 61 | 'b' => null, 62 | 'c' => array(), 63 | 'd' => '', 64 | 'e' => new \Handlebars\StringWrapper(''), 65 | ), 66 | 'false', 67 | ), 68 | ); 69 | } 70 | 71 | /** 72 | * Tests that exception is thrown if wrong number of arguments is used. 73 | * 74 | * @expectedException InvalidArgumentException 75 | * @dataProvider wrongArgumentsProvider 76 | */ 77 | public function testArgumentsCount($template) 78 | { 79 | $helpers = new \Handlebars\Helpers(array('ifAny' => new IfAnyHelper())); 80 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 81 | 82 | $engine->render($template, array()); 83 | } 84 | 85 | /** 86 | * A data provider for testArgumentsCount method. 87 | */ 88 | public function wrongArgumentsProvider() 89 | { 90 | return array( 91 | // Not enough arguments 92 | array('{{#ifAny}}yes{{else}}no{{/ifAny}}'), 93 | ); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/Comparison/IfBetweenClosedHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenClosedHelper; 14 | 15 | /** 16 | * Test class for "ifBetweenClosed" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfBetweenClosedHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $left, $right, $is_between) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifBetweenClosed' => new IfBetweenClosedHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifBetweenClosed value left right}}true{{else}}false{{/ifBetweenClosed}}', 35 | array( 36 | 'value' => $value, 37 | 'left' => $left, 38 | 'right' => $right, 39 | ) 40 | ), 41 | $is_between ? 'true' : 'false' 42 | ); 43 | } 44 | 45 | /** 46 | * A data provider for testCondition method. 47 | */ 48 | public function conditionProvider() 49 | { 50 | return array( 51 | // The value is less than left border. 52 | array(2, 10, 12, false), 53 | // The value equals to the left border. 54 | array(0, 0, 42, true), 55 | // The value equals to the right border. 56 | array(9, 0, 9, true), 57 | // The value is more than the right border. 58 | array(75, 10, 12, false), 59 | // The value is between borders. 60 | array(58, 11, 134, true), 61 | ); 62 | } 63 | 64 | /** 65 | * Tests that exception is thrown if wrong number of arguments is used. 66 | * 67 | * @expectedException InvalidArgumentException 68 | * @dataProvider wrongArgumentsProvider 69 | */ 70 | public function testArgumentsCount($template) 71 | { 72 | $helpers = new \Handlebars\Helpers(array('ifBetweenClosed' => new IfBetweenClosedHelper())); 73 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 74 | 75 | $engine->render($template, array()); 76 | } 77 | 78 | /** 79 | * A data provider for testArgumentsCount method. 80 | */ 81 | public function wrongArgumentsProvider() 82 | { 83 | return array( 84 | // Not enough arguments 85 | array('{{#ifBetweenClosed}}yes{{else}}no{{/ifBetweenClosed}}'), 86 | // Still not enough arguments 87 | array('{{#ifBetweenClosed 1}}yes{{else}}no{{/ifBetweenClosed}}'), 88 | array('{{#ifBetweenClosed 1 2}}yes{{else}}no{{/ifBetweenClosed}}'), 89 | // Too much arguments 90 | array('{{#ifBetweenClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenClosed}}'), 91 | ); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/Comparison/IfBetweenLeftClosedHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenLeftClosedHelper; 14 | 15 | /** 16 | * Test class for "ifBetweenLeftClosed" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfBetweenLeftClosedHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $left, $right, $is_between) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifBetweenLeftClosed' => new IfBetweenLeftClosedHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifBetweenLeftClosed value left right}}true{{else}}false{{/ifBetweenLeftClosed}}', 35 | array( 36 | 'value' => $value, 37 | 'left' => $left, 38 | 'right' => $right, 39 | ) 40 | ), 41 | $is_between ? 'true' : 'false' 42 | ); 43 | } 44 | 45 | /** 46 | * A data provider for testCondition method. 47 | */ 48 | public function conditionProvider() 49 | { 50 | return array( 51 | // The value is less than left border. 52 | array(2, 10, 12, false), 53 | // The value equals to the left border. 54 | array(0, 0, 42, true), 55 | // The value equals to the right border. 56 | array(9, 0, 9, false), 57 | // The value is more than the right border. 58 | array(75, 10, 12, false), 59 | // The value is between borders. 60 | array(58, 11, 134, true), 61 | ); 62 | } 63 | 64 | /** 65 | * Tests that exception is thrown if wrong number of arguments is used. 66 | * 67 | * @expectedException InvalidArgumentException 68 | * @dataProvider wrongArgumentsProvider 69 | */ 70 | public function testArgumentsCount($template) 71 | { 72 | $helpers = new \Handlebars\Helpers(array('ifBetweenLeftClosed' => new IfBetweenLeftClosedHelper())); 73 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 74 | 75 | $engine->render($template, array()); 76 | } 77 | 78 | /** 79 | * A data provider for testArgumentsCount method. 80 | */ 81 | public function wrongArgumentsProvider() 82 | { 83 | return array( 84 | // Not enough arguments 85 | array('{{#ifBetweenLeftClosed}}yes{{else}}no{{/ifBetweenLeftClosed}}'), 86 | // Still not enough arguments 87 | array('{{#ifBetweenLeftClosed 1}}yes{{else}}no{{/ifBetweenLeftClosed}}'), 88 | array('{{#ifBetweenLeftClosed 1 2}}yes{{else}}no{{/ifBetweenLeftClosed}}'), 89 | // Too much arguments 90 | array('{{#ifBetweenLeftClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenLeftClosed}}'), 91 | ); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/Comparison/IfBetweenRightClosedHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenRightClosedHelper; 14 | 15 | /** 16 | * Test class for "ifBetweenRightClosed" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class IfBetweenRightClosedHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests conditions work as expected. 24 | * 25 | * @dataProvider conditionProvider 26 | */ 27 | public function testCondition($value, $left, $right, $is_between) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ifBetweenRightClosed' => new IfBetweenRightClosedHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{#ifBetweenRightClosed value left right}}true{{else}}false{{/ifBetweenRightClosed}}', 35 | array( 36 | 'value' => $value, 37 | 'left' => $left, 38 | 'right' => $right, 39 | ) 40 | ), 41 | $is_between ? 'true' : 'false' 42 | ); 43 | } 44 | 45 | /** 46 | * A data provider for testCondition method. 47 | */ 48 | public function conditionProvider() 49 | { 50 | return array( 51 | // The value is less than left border. 52 | array(2, 10, 12, false), 53 | // The value equals to the left border. 54 | array(0, 0, 42, false), 55 | // The value equals to the right border. 56 | array(9, 0, 9, true), 57 | // The value is more than the right border. 58 | array(75, 10, 12, false), 59 | // The value is between borders. 60 | array(58, 11, 134, true), 61 | ); 62 | } 63 | 64 | /** 65 | * Tests that exception is thrown if wrong number of arguments is used. 66 | * 67 | * @expectedException InvalidArgumentException 68 | * @dataProvider wrongArgumentsProvider 69 | */ 70 | public function testArgumentsCount($template) 71 | { 72 | $helpers = new \Handlebars\Helpers(array('ifBetweenRightClosed' => new IfBetweenRightClosedHelper())); 73 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 74 | 75 | $engine->render($template, array()); 76 | } 77 | 78 | /** 79 | * A data provider for testArgumentsCount method. 80 | */ 81 | public function wrongArgumentsProvider() 82 | { 83 | return array( 84 | // Not enough arguments 85 | array('{{#ifBetweenRightClosed}}yes{{else}}no{{/ifBetweenRightClosed}}'), 86 | // Still not enough arguments 87 | array('{{#ifBetweenRightClosed 1}}yes{{else}}no{{/ifBetweenRightClosed}}'), 88 | array('{{#ifBetweenRightClosed 1 2}}yes{{else}}no{{/ifBetweenRightClosed}}'), 89 | // Too much arguments 90 | array('{{#ifBetweenRightClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenRightClosed}}'), 91 | ); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/Text/EllipsisHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\EllipsisHelper; 14 | 15 | /** 16 | * Test class for "ellipsis" helper. 17 | * 18 | * @author Matteo Merola 19 | */ 20 | class EllipsisTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that strings are repeated properly. 24 | * 25 | * @dataProvider truncateProvider 26 | */ 27 | public function testEllipsis($template, $data, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals($engine->render($template, $data), $result); 33 | } 34 | 35 | /** 36 | * A data provider for testEllipsis method. 37 | */ 38 | public function truncateProvider() 39 | { 40 | return array( 41 | // No truncate 42 | array('{{ellipsis a len}}', array('a' => '123', 'len' => 5), '123'), 43 | // Simple truncates 44 | array('{{ellipsis "prova matteo ciao" 2}}', array(), 'prova matteo'), 45 | array('{{ellipsis "prova merola hello" 0}}', array(), ''), 46 | // Truncate with ellipsis 47 | array('{{ellipsis "prova matt" 1 "..."}}', array(), 'prova...'), 48 | ); 49 | } 50 | 51 | /** 52 | * Tests that exception is thrown if wrong number of arguments is used. 53 | * 54 | * @expectedException InvalidArgumentException 55 | * @dataProvider wrongArgumentsSetProvider 56 | */ 57 | public function testArgumentsCount($template) 58 | { 59 | $helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper())); 60 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 61 | 62 | $engine->render($template, array()); 63 | } 64 | 65 | /** 66 | * A data provider for testArgumentsCount method. 67 | */ 68 | public function wrongArgumentsSetProvider() 69 | { 70 | return array( 71 | // Not enough arguments 72 | array('{{ellipsis}}'), 73 | array('{{ellipsis "abc"}}'), 74 | // Too much arguments 75 | array('{{ellipsis "abc" 30 "..." "xyz"}}'), 76 | ); 77 | } 78 | 79 | /** 80 | * Tests that exception is thrown if arguments are invalid. 81 | * 82 | * @expectedException InvalidArgumentException 83 | * @dataProvider invalidArgumentsProvider 84 | */ 85 | public function testInvalidArguments($template) 86 | { 87 | $helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper())); 88 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 89 | 90 | $engine->render($template, array()); 91 | } 92 | 93 | /** 94 | * A data provider for testInvalidArguments method. 95 | */ 96 | public function invalidArgumentsProvider() 97 | { 98 | return array( 99 | // Negative target length. 100 | array('{{ellipsis "abc" -10}}'), 101 | ); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/Text/TruncateHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Text; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Text\TruncateHelper; 14 | 15 | /** 16 | * Test class for "truncate" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class TruncateTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that strings are repeated properly. 24 | * 25 | * @dataProvider truncateProvider 26 | */ 27 | public function testTruncate($template, $data, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('truncate' => new TruncateHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals($engine->render($template, $data), $result); 33 | } 34 | 35 | /** 36 | * A data provider for testTruncate method. 37 | */ 38 | public function truncateProvider() 39 | { 40 | return array( 41 | // No truncate 42 | array('{{truncate a len}}', array('a' => '123', 'len' => 5), '123'), 43 | // Simple truncates 44 | array('{{truncate "0123456789" 5}}', array(), '01234'), 45 | array('{{truncate "0123456789" 0}}', array(), ''), 46 | // Truncate with ellipsis 47 | array('{{truncate "0123456789" 5 "..."}}', array(), '01...'), 48 | ); 49 | } 50 | 51 | /** 52 | * Tests that exception is thrown if wrong number of arguments is used. 53 | * 54 | * @expectedException InvalidArgumentException 55 | * @dataProvider wrongArgumentsSetProvider 56 | */ 57 | public function testArgumentsCount($template) 58 | { 59 | $helpers = new \Handlebars\Helpers(array('truncate' => new TruncateHelper())); 60 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 61 | 62 | $engine->render($template, array()); 63 | } 64 | 65 | /** 66 | * A data provider for testArgumentsCount method. 67 | */ 68 | public function wrongArgumentsSetProvider() 69 | { 70 | return array( 71 | // Not enough arguments 72 | array('{{truncate}}'), 73 | array('{{truncate "abc"}}'), 74 | // Too much arguments 75 | array('{{truncate "abc" 30 "..." "xyz"}}'), 76 | ); 77 | } 78 | 79 | /** 80 | * Tests that exception is thrown if arguments are invalid. 81 | * 82 | * @expectedException InvalidArgumentException 83 | * @dataProvider invalidArgumentsProvider 84 | */ 85 | public function testInvalidArguments($template) 86 | { 87 | $helpers = new \Handlebars\Helpers(array('truncate' => new TruncateHelper())); 88 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 89 | 90 | $engine->render($template, array()); 91 | } 92 | 93 | /** 94 | * A data provider for testInvalidArguments method. 95 | */ 96 | public function invalidArgumentsProvider() 97 | { 98 | return array( 99 | // Negative target length. 100 | array('{{truncate "abc" -10}}'), 101 | // Length of ellipsis is greater than target length. 102 | array('{{truncate "abc" 2 "..."}}') 103 | ); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/Collection/CountHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Collection; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Collection\CountHelper; 14 | 15 | /** 16 | * Test class for "count" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class CountHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that count is calculated properly. 24 | * 25 | * @dataProvider collectionsProvider 26 | */ 27 | public function testCount($collection, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('count' => new CountHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{count collection}}', 35 | array('collection' => $collection) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testCount method. 43 | */ 44 | public function collectionsProvider() 45 | { 46 | return array( 47 | // Test arrays with numeric keys 48 | array(array('a', 'b', 'c'), '3'), 49 | // Test associative arrays 50 | array(array('a' => '10'), '1'), 51 | // Test \Countable instance 52 | array(new \ArrayIterator(array('a', 'b')), '2'), 53 | // Test empty collections 54 | array(array(), '0'), 55 | array(new \ArrayIterator(array()), '0'), 56 | ); 57 | } 58 | 59 | /** 60 | * Tests that exception is thrown if wrong number of arguments is used. 61 | * 62 | * @expectedException InvalidArgumentException 63 | * @dataProvider wrongArgumentsCountProvider 64 | */ 65 | public function testArgumentsCount($template) 66 | { 67 | $helpers = new \Handlebars\Helpers(array('count' => new CountHelper())); 68 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 69 | 70 | $engine->render($template, array()); 71 | } 72 | 73 | /** 74 | * A data provider for testArgumentsCount method. 75 | */ 76 | public function wrongArgumentsCountProvider() 77 | { 78 | return array( 79 | // Not enough arguments 80 | array('{{count}}'), 81 | // Too much arguments 82 | array('{{count "Arg" "ANOTHER ARG"}}'), 83 | ); 84 | } 85 | 86 | /** 87 | * Tests invalid arguments type. 88 | * 89 | * @expectedException InvalidArgumentException 90 | * @dataProvider invalidArgumentsProvider 91 | */ 92 | public function testInvalidArguments($collection) 93 | { 94 | $helpers = new \Handlebars\Helpers(array('count' => new CountHelper())); 95 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 96 | 97 | $engine->render('{{count collection}}', array('collection' => $collection)); 98 | } 99 | 100 | /** 101 | * A data provider for testInvalidArguments method. 102 | */ 103 | public function invalidArgumentsProvider() 104 | { 105 | return array( 106 | array('a string'), 107 | array(42), 108 | array(new \stdClass()), 109 | ); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /tests/Collection/LastHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Collection; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Collection\LastHelper; 14 | 15 | /** 16 | * Test class for "last" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class LastHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that the last item returned. 24 | * 25 | * @dataProvider collectionsProvider 26 | */ 27 | public function testLastItem($collection, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('last' => new LastHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{last collection}}', 35 | array('collection' => $collection) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testLastItem method. 43 | */ 44 | public function collectionsProvider() 45 | { 46 | return array( 47 | // Test arrays with numeric keys 48 | array(array('a', 'b', 'c'), 'c'), 49 | array(array('z'), 'z'), 50 | // Test associative arrays 51 | array(array('a' => '10', 'b' => '11', 'c' => '12'), '12'), 52 | array(array('f' => '15'), '15'), 53 | // Test \Traversable instance 54 | array(new \ArrayIterator(array('a', 'b', 'c')), 'c'), 55 | array(new \ArrayIterator(array('z')), 'z'), 56 | // Test empty collections 57 | array(array(), false), 58 | array(new \ArrayIterator(array()), false), 59 | ); 60 | } 61 | 62 | /** 63 | * Tests that exception is thrown if wrong number of arguments is used. 64 | * 65 | * @expectedException InvalidArgumentException 66 | * @dataProvider wrongArgumentsCountProvider 67 | */ 68 | public function testArgumentsCount($template) 69 | { 70 | $helpers = new \Handlebars\Helpers(array('last' => new LastHelper())); 71 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 72 | 73 | $engine->render($template, array()); 74 | } 75 | 76 | /** 77 | * A data provider for testArgumentsCount method. 78 | */ 79 | public function wrongArgumentsCountProvider() 80 | { 81 | return array( 82 | // Not enough arguments 83 | array('{{last}}'), 84 | // Too much arguments 85 | array('{{last "Arg" "ANOTHER ARG"}}'), 86 | ); 87 | } 88 | 89 | /** 90 | * Tests invalid arguments type. 91 | * 92 | * @expectedException InvalidArgumentException 93 | * @dataProvider invalidArgumentsProvider 94 | */ 95 | public function testInvalidArguments($collection) 96 | { 97 | $helpers = new \Handlebars\Helpers(array('last' => new LastHelper())); 98 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 99 | 100 | $engine->render('{{last collection}}', array('collection' => $collection)); 101 | } 102 | 103 | /** 104 | * A data provider for testInvalidArguments method. 105 | */ 106 | public function invalidArgumentsProvider() 107 | { 108 | return array( 109 | array('a string'), 110 | array(42), 111 | array(new \stdClass()), 112 | ); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /tests/Collection/FirstHelperTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Collection; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Collection\FirstHelper; 14 | 15 | /** 16 | * Test class for "first" helper. 17 | * 18 | * @author Dmitriy Simushev 19 | */ 20 | class FirstHelperTest extends \PHPUnit_Framework_TestCase 21 | { 22 | /** 23 | * Tests that the first item returned. 24 | * 25 | * @dataProvider collectionsProvider 26 | */ 27 | public function testFirstItem($collection, $result) 28 | { 29 | $helpers = new \Handlebars\Helpers(array('first' => new FirstHelper())); 30 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 31 | 32 | $this->assertEquals( 33 | $engine->render( 34 | '{{first collection}}', 35 | array('collection' => $collection) 36 | ), 37 | $result 38 | ); 39 | } 40 | 41 | /** 42 | * A data provider for testFirstItem method. 43 | */ 44 | public function collectionsProvider() 45 | { 46 | return array( 47 | // Test arrays with numeric keys 48 | array(array('a', 'b', 'c'), 'a'), 49 | array(array('z'), 'z'), 50 | // Test associative arrays 51 | array(array('a' => '10', 'b' => '11', 'c' => '12'), '10'), 52 | array(array('f' => '15'), '15'), 53 | // Test \Traversable instance 54 | array(new \ArrayIterator(array('a', 'b', 'c')), 'a'), 55 | array(new \ArrayIterator(array('z')), 'z'), 56 | // Test empty collections 57 | array(array(), false), 58 | array(new \ArrayIterator(array()), false), 59 | ); 60 | } 61 | 62 | /** 63 | * Tests that exception is thrown if wrong number of arguments is used. 64 | * 65 | * @expectedException InvalidArgumentException 66 | * @dataProvider wrongArgumentsCountProvider 67 | */ 68 | public function testArgumentsCount($template) 69 | { 70 | $helpers = new \Handlebars\Helpers(array('first' => new FirstHelper())); 71 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 72 | 73 | $engine->render($template, array()); 74 | } 75 | 76 | /** 77 | * A data provider for testArgumentsCount method. 78 | */ 79 | public function wrongArgumentsCountProvider() 80 | { 81 | return array( 82 | // Not enough arguments 83 | array('{{first}}'), 84 | // Too much arguments 85 | array('{{first "Arg" "ANOTHER ARG"}}'), 86 | ); 87 | } 88 | 89 | /** 90 | * Tests invalid arguments type. 91 | * 92 | * @expectedException InvalidArgumentException 93 | * @dataProvider invalidArgumentsProvider 94 | */ 95 | public function testInvalidArguments($collection) 96 | { 97 | $helpers = new \Handlebars\Helpers(array('first' => new FirstHelper())); 98 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 99 | 100 | $engine->render('{{first collection}}', array('collection' => $collection)); 101 | } 102 | 103 | /** 104 | * A data provider for testInvalidArguments method. 105 | */ 106 | public function invalidArgumentsProvider() 107 | { 108 | return array( 109 | array('a string'), 110 | array(42), 111 | array(new \stdClass()), 112 | ); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /tests/Layout/IntegrationTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace JustBlackBird\HandlebarsHelpers\Tests\Layout; 12 | 13 | use JustBlackBird\HandlebarsHelpers\Layout\BlockHelper; 14 | use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage; 15 | use JustBlackBird\HandlebarsHelpers\Layout\ExtendsHelper; 16 | use JustBlackBird\HandlebarsHelpers\Layout\IfOverriddenHelper; 17 | use JustBlackBird\HandlebarsHelpers\Layout\OverrideHelper; 18 | use JustBlackBird\HandlebarsHelpers\Layout\UnlessOverriddenHelper; 19 | 20 | /** 21 | * Test class for all layout helpers. 22 | * 23 | * Layout helpers must work together thus combined tests should be used. 24 | * 25 | * @author Dmitriy Simushev 26 | */ 27 | class IntegrationTest extends \PHPUnit_Framework_TestCase 28 | { 29 | /** 30 | * Tests how inheritance helpers works together. 31 | */ 32 | public function testInheritance() 33 | { 34 | $storage = new BlockStorage(); 35 | $helpers = new \Handlebars\Helpers(array( 36 | 'block' => new BlockHelper($storage), 37 | 'extends' => new ExtendsHelper($storage), 38 | 'override' => new OverrideHelper($storage), 39 | )); 40 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 41 | 42 | // Test simple inheritance 43 | $engine->setLoader(new \Handlebars\Loader\ArrayLoader(array( 44 | 'parent' => '{{#block "name"}}parent{{/block}} template', 45 | 'child' => '{{#extends "parent"}}{{#override "name"}}child{{/override}}{{/extends}}', 46 | 'grandchild' => '{{#extends "child"}}{{#override "name"}}grandchild{{/override}}{{/extends}}', 47 | ))); 48 | $this->assertEquals($engine->render('parent', array()), 'parent template'); 49 | $this->assertEquals($engine->render('child', array()), 'child template'); 50 | $this->assertEquals($engine->render('grandchild', array()), 'grandchild template'); 51 | 52 | // Test inheritance with nested blocks 53 | $engine->setLoader(new \Handlebars\Loader\ArrayLoader(array( 54 | 'parent' => '{{#block "title"}}{{#block "name"}}parent{{/block}} template{{/block}}', 55 | 'child' => '{{#extends "parent"}}{{#override "name"}}child{{/override}}{{/extends}}', 56 | 'newbie' => '{{#extends "parent"}}{{#override "title"}}Newbie!{{/override}}{{/extends}}', 57 | ))); 58 | $this->assertEquals($engine->render('parent', array()), 'parent template'); 59 | $this->assertEquals($engine->render('child', array()), 'child template'); 60 | $this->assertEquals($engine->render('newbie', array()), 'Newbie!'); 61 | } 62 | 63 | /** 64 | * Tests that conditions related with inheritance works as expected. 65 | */ 66 | public function testConditions() 67 | { 68 | $storage = new BlockStorage(); 69 | $helpers = new \Handlebars\Helpers(array( 70 | 'block' => new BlockHelper($storage), 71 | 'extends' => new ExtendsHelper($storage), 72 | 'override' => new OverrideHelper($storage), 73 | 'ifOverridden' => new IfOverriddenHelper($storage), 74 | 'unlessOverridden' => new UnlessOverriddenHelper($storage), 75 | )); 76 | $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); 77 | 78 | // Test "ifOverridden" helper 79 | $engine->setLoader(new \Handlebars\Loader\ArrayLoader(array( 80 | 'parent' => '{{#block "name"}}{{/block}}{{#ifOverridden "name"}}true{{else}}false{{/ifOverridden}}', 81 | 'child' => '{{#extends "parent"}}{{#override "name"}}{{/override}}{{/extends}}', 82 | 'another_child' => '{{#extends "parent"}}{{/extends}}', 83 | ))); 84 | $this->assertEquals($engine->render('parent', array()), 'false'); 85 | $this->assertEquals($engine->render('child', array()), 'true'); 86 | $this->assertEquals($engine->render('another_child', array()), 'false'); 87 | 88 | // Test "unlessOverridden" helper 89 | $engine->setLoader(new \Handlebars\Loader\ArrayLoader(array( 90 | 'parent' => '{{#block "name"}}{{/block}}{{#unlessOverridden "name"}}false{{else}}true{{/unlessOverridden}}', 91 | 'child' => '{{#extends "parent"}}{{#override "name"}}{{/override}}{{/extends}}', 92 | 'another_child' => '{{#extends "parent"}}{{/extends}}', 93 | ))); 94 | $this->assertEquals($engine->render('parent', array()), 'false'); 95 | $this->assertEquals($engine->render('child', array()), 'true'); 96 | $this->assertEquals($engine->render('another_child', array()), 'false'); 97 | } 98 | } 99 | --------------------------------------------------------------------------------