From: Fabien Potencier Date: Tue, 1 Nov 2011 07:57:23 +0000 (+0100) Subject: migrated most tests to proper Node classes X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c2371dbbd60f349e7eab90a60e2c00a17672bb81;p=web%2Fkonrad%2Ftwig.git migrated most tests to proper Node classes --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index fb7067a..d03d641 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'), ); } @@ -754,90 +754,6 @@ function twig_test_sameas($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 0000000..6e6b6fd
--- /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 0000000..05563d5 --- /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 0000000..08e6d82 --- /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 0000000..55061db --- /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 0000000..5fecebc --- /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(')') + ; + } +}