diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index fb7067a8d..d03d641e9 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -108,14 +108,14 @@ class Twig_Extension_Core extends Twig_Extension public function getTests() { return array( - 'even' => new Twig_Test_Function('twig_test_even'), - 'odd' => new Twig_Test_Function('twig_test_odd'), + 'even' => new Twig_Test_Node('Twig_Node_Expression_Test_Even'), + 'odd' => new Twig_Test_Node('Twig_Node_Expression_Test_Odd'), 'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'), 'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'), - 'none' => new Twig_Test_Function('twig_test_none'), - 'null' => new Twig_Test_Function('twig_test_none'), - 'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'), - 'constant' => new Twig_Test_Function('twig_test_constant'), + 'none' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'), + 'null' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'), + 'divisibleby' => new Twig_Test_Node('Twig_Node_Expression_Test_Divisibleby'), + 'constant' => new Twig_Test_Node('Twig_Node_Expression_Test_Constant'), 'empty' => new Twig_Test_Function('twig_test_empty'), ); } @@ -753,90 +753,6 @@ function twig_test_sameas($value, $test) return $value === $test; } -/** - * Checks that a variable is null. - * - *
- *  {{ var is none }}
- * 
- * - * @param mixed $value a PHP variable. - * - * @return Boolean true if the value is null, false otherwise - */ -function twig_test_none($value) -{ - return null === $value; -} - -/** - * Checks if a variable is divisible by a number. - * - *
- *  {% if loop.index is divisibleby(3) %}
- * 
- * - * @param integer $value A PHP value - * @param integer $num A number - * - * @return Boolean true if the value is divisible by the number, false otherwise - */ -function twig_test_divisibleby($value, $num) -{ - return 0 == $value % $num; -} - -/** - * Checks if a number is even. - * - *
- *  {{ var is even }}
- * 
- * - * @param integer $value An integer - * - * @return Boolean true if the value is even, false otherwise - */ -function twig_test_even($value) -{ - return $value % 2 == 0; -} - -/** - * Checks if a number is odd. - * - *
- *  {{ var is odd }}
- * 
- * - * @param integer $value An integer - * - * @return Boolean true if the value is odd, false otherwise - */ -function twig_test_odd($value) -{ - return $value % 2 == 1; -} - -/** - * Checks if a variable is the exact same value as a constant. - * - *
- *  {% if post.status is constant('Post::PUBLISHED') %}
- *    the status attribute is exactly the same as Post::PUBLISHED
- *  {% endif %}
- * 
- * - * @param mixed $value A PHP value - * @param mixed $constant The constant to test against - * - * @return Boolean true if the value is the same as the constant, false otherwise - */ -function twig_test_constant($value, $constant) -{ - return constant($constant) === $value; -} - /** * Checks if a variable is empty. * diff --git a/lib/Twig/Node/Expression/Test/Constant.php b/lib/Twig/Node/Expression/Test/Constant.php new file mode 100644 index 000000000..6e6b6fd7a --- /dev/null +++ b/lib/Twig/Node/Expression/Test/Constant.php @@ -0,0 +1,36 @@ + + * {% if post.status is constant('Post::PUBLISHED') %} + * the status attribute is exactly the same as Post::PUBLISHED + * {% endif %} + * + * + * @package twig + * @author Fabien Potencier + */ +class Twig_Node_Expression_Test_Constant extends Twig_Node_Expression_Test +{ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(') + ->subcompile($this->getNode('node')) + ->raw(' === constant(') + ->subcompile($this->getNode('arguments')->getNode(0)) + ->raw('))') + ; + } +} diff --git a/lib/Twig/Node/Expression/Test/Divisibleby.php b/lib/Twig/Node/Expression/Test/Divisibleby.php new file mode 100644 index 000000000..05563d576 --- /dev/null +++ b/lib/Twig/Node/Expression/Test/Divisibleby.php @@ -0,0 +1,34 @@ + + * {% if loop.index is divisibleby(3) %} + * + * + * @package twig + * @author Fabien Potencier + */ +class Twig_Node_Expression_Test_Divisibleby extends Twig_Node_Expression_Test +{ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(0 == ') + ->subcompile($this->getNode('node')) + ->raw(' % ') + ->subcompile($this->getNode('arguments')->getNode(0)) + ->raw(')') + ; + } +} diff --git a/lib/Twig/Node/Expression/Test/Even.php b/lib/Twig/Node/Expression/Test/Even.php new file mode 100644 index 000000000..08e6d829f --- /dev/null +++ b/lib/Twig/Node/Expression/Test/Even.php @@ -0,0 +1,33 @@ + + * {{ var is even }} + * + * + * @package twig + * @author Fabien Potencier + */ +class Twig_Node_Expression_Test_Even extends Twig_Node_Expression_Test +{ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(') + ->subcompile($this->getNode('node')) + ->raw(' % 2 == 0') + ->raw(')') + ; + } +} diff --git a/lib/Twig/Node/Expression/Test/Null.php b/lib/Twig/Node/Expression/Test/Null.php new file mode 100644 index 000000000..55061db3f --- /dev/null +++ b/lib/Twig/Node/Expression/Test/Null.php @@ -0,0 +1,32 @@ + + * {{ var is none }} + * + * + * @package twig + * @author Fabien Potencier + */ +class Twig_Node_Expression_Test_Null extends Twig_Node_Expression_Test +{ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(null === ') + ->subcompile($this->getNode('node')) + ->raw(')') + ; + } +} diff --git a/lib/Twig/Node/Expression/Test/Odd.php b/lib/Twig/Node/Expression/Test/Odd.php new file mode 100644 index 000000000..5fecebcb1 --- /dev/null +++ b/lib/Twig/Node/Expression/Test/Odd.php @@ -0,0 +1,33 @@ + + * {{ var is odd }} + * + * + * @package twig + * @author Fabien Potencier + */ +class Twig_Node_Expression_Test_Odd extends Twig_Node_Expression_Test +{ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(') + ->subcompile($this->getNode('node')) + ->raw(' % 2 == 1') + ->raw(')') + ; + } +}