mirror of
				https://github.com/Mibew/handlebars.php-helpers.git
				synced 2025-11-04 12:25:21 +03:00 
			
		
		
		
	Add "ifAny" helper
This commit is contained in:
		
							parent
							
								
									8befb43846
								
							
						
					
					
						commit
						2455d3d269
					
				@ -26,6 +26,7 @@ class Helpers extends BaseHelpers
 | 
			
		||||
    {
 | 
			
		||||
        parent::addDefaultHelpers();
 | 
			
		||||
 | 
			
		||||
        $this->add('ifAny', new IfAnyHelper());
 | 
			
		||||
        $this->add('ifEqual', new IfEqualHelper());
 | 
			
		||||
        $this->add('ifEven', new IfEvenHelper());
 | 
			
		||||
        $this->add('ifOdd', new IfOddHelper());
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										77
									
								
								src/Comparison/IfAnyHelper.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/Comparison/IfAnyHelper.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of Handlebars.php Helpers Set
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Dmitriy Simushev <simushevds@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
 | 
			
		||||
 | 
			
		||||
use Handlebars\Context;
 | 
			
		||||
use Handlebars\Helper as HelperInterface;
 | 
			
		||||
use Handlebars\String;
 | 
			
		||||
use Handlebars\Template;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Conditional helper that checks if at least one argumet can be treated as
 | 
			
		||||
 * "true" value.
 | 
			
		||||
 *
 | 
			
		||||
 * Usage:
 | 
			
		||||
 * ```handlebars
 | 
			
		||||
 *   {{#ifAny first second third}}
 | 
			
		||||
 *     At least one of argument can be threated as "true".
 | 
			
		||||
 *   {{else}}
 | 
			
		||||
 *     All values are "falsy"
 | 
			
		||||
 *   {{/ifAny}}
 | 
			
		||||
 * ```
 | 
			
		||||
 *
 | 
			
		||||
 * @author Dmitriy Simushev <simushevds@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
class IfAnyHelper implements HelperInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function execute(Template $template, Context $context, $args, $source)
 | 
			
		||||
    {
 | 
			
		||||
        $parsed_args = $template->parseArguments($args);
 | 
			
		||||
        if (count($parsed_args) == 0) {
 | 
			
		||||
            throw new \InvalidArgumentException(
 | 
			
		||||
                '"ifAny" helper expects at least one argument.'
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $condition = false;
 | 
			
		||||
        foreach ($parsed_args as $parsed_arg) {
 | 
			
		||||
            $value = $context->get($parsed_arg);
 | 
			
		||||
 | 
			
		||||
            if ($value instanceof String) {
 | 
			
		||||
                // Casting any object of \Handlebars\String will have false
 | 
			
		||||
                // positive result even for those with empty internal strings.
 | 
			
		||||
                // Thus we need to check internal string of such objects.
 | 
			
		||||
                $value = $value->getString();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if ($value) {
 | 
			
		||||
                $condition = true;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($condition) {
 | 
			
		||||
            $template->setStopToken('else');
 | 
			
		||||
            $buffer = $template->render($context);
 | 
			
		||||
            $template->setStopToken(false);
 | 
			
		||||
        } else {
 | 
			
		||||
            $template->setStopToken('else');
 | 
			
		||||
            $template->discard();
 | 
			
		||||
            $template->setStopToken(false);
 | 
			
		||||
            $buffer = $template->render($context);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $buffer;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -30,6 +30,7 @@ class Helpers extends BaseHelpers
 | 
			
		||||
        $this->add('formatDate', new Date\FormatDateHelper());
 | 
			
		||||
 | 
			
		||||
        // Comparison helpers
 | 
			
		||||
        $this->add('ifAny', new Comparison\IfAnyHelper());
 | 
			
		||||
        $this->add('ifEqual', new Comparison\IfEqualHelper());
 | 
			
		||||
        $this->add('ifEven', new Comparison\IfEvenHelper());
 | 
			
		||||
        $this->add('ifOdd', new Comparison\IfOddHelper());
 | 
			
		||||
 | 
			
		||||
@ -38,6 +38,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
    public function helpersProvider()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            array('ifAny', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfAnyHelper'),
 | 
			
		||||
            array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'),
 | 
			
		||||
            array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
 | 
			
		||||
            array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										95
									
								
								tests/Comparison/IfAnyHelperTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								tests/Comparison/IfAnyHelperTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,95 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of Handlebars.php Helpers Set
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Dmitriy Simushev <simushevds@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Comparison;
 | 
			
		||||
 | 
			
		||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfAnyHelper;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Test class for "ifAny" helper.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Dmitriy Simushev <simushevds@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
class IfAnyHelperTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Tests conditions work as expected.
 | 
			
		||||
     *
 | 
			
		||||
     * @dataProvider conditionProvider
 | 
			
		||||
     */
 | 
			
		||||
    public function testCondition($template, $data, $result)
 | 
			
		||||
    {
 | 
			
		||||
        $helpers = new \Handlebars\Helpers(array('ifAny' => new IfAnyHelper()));
 | 
			
		||||
        $engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($engine->render($template, $data), $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A data provider for testCondition method.
 | 
			
		||||
     */
 | 
			
		||||
    public function conditionProvider()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            // Single argument. It's an analog of "if" helper.
 | 
			
		||||
            array('{{#ifAny a}}true{{else}}false{{/ifAny}}', array('a' => true), 'true'),
 | 
			
		||||
            array('{{#ifAny a}}true{{else}}false{{/ifAny}}', array('a' => false), 'false'),
 | 
			
		||||
            // Multiple arguments (positive)
 | 
			
		||||
            array(
 | 
			
		||||
                '{{#ifAny a b c}}true{{else}}false{{/ifAny}}',
 | 
			
		||||
                array('a' => false, 'b' => true, 'c' => false),
 | 
			
		||||
                'true',
 | 
			
		||||
            ),
 | 
			
		||||
            // Multiple arguments (negative)
 | 
			
		||||
            array(
 | 
			
		||||
                '{{#ifAny a b c}}true{{else}}false{{/ifAny}}',
 | 
			
		||||
                array('a' => false, 'b' => false, 'c' => false),
 | 
			
		||||
                'false',
 | 
			
		||||
            ),
 | 
			
		||||
            // Multiple arguments (negative). Check different falsy values.
 | 
			
		||||
            array(
 | 
			
		||||
                '{{#ifAny a b c d e}}true{{else}}false{{/ifAny}}',
 | 
			
		||||
                array(
 | 
			
		||||
                    'a' => 0,
 | 
			
		||||
                    'b' => null,
 | 
			
		||||
                    'c' => array(),
 | 
			
		||||
                    'd' => '',
 | 
			
		||||
                    'e' => new \Handlebars\String(''),
 | 
			
		||||
                ),
 | 
			
		||||
                'false',
 | 
			
		||||
            ),
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Tests that exception is thrown if wrong number of arguments is used.
 | 
			
		||||
     *
 | 
			
		||||
     * @expectedException InvalidArgumentException
 | 
			
		||||
     * @dataProvider wrongArgumentsProvider
 | 
			
		||||
     */
 | 
			
		||||
    public function testArgumentsCount($template)
 | 
			
		||||
    {
 | 
			
		||||
        $helpers = new \Handlebars\Helpers(array('ifAny' => new IfAnyHelper()));
 | 
			
		||||
        $engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
 | 
			
		||||
 | 
			
		||||
        $engine->render($template, array());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A data provider for testArgumentsCount method.
 | 
			
		||||
     */
 | 
			
		||||
    public function wrongArgumentsProvider()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            // Not enough arguments
 | 
			
		||||
            array('{{#ifAny}}yes{{else}}no{{/ifAny}}'),
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -42,6 +42,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
            array('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'),
 | 
			
		||||
 | 
			
		||||
            // Comparison helpers
 | 
			
		||||
            array('ifAny', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfAnyHelper'),
 | 
			
		||||
            array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'),
 | 
			
		||||
            array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
 | 
			
		||||
            array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user