From 5e290cbb4dcb33a04df0769ff0335460bd6934c2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 26 Oct 2011 09:33:02 +0200 Subject: [PATCH] fixed deep nested defined tests --- lib/Twig/Node/Expression/Test.php | 13 +++++++- test/Twig/Tests/Fixtures/filters/default.test | 41 ++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php index 1c000ac..6af4bd0 100644 --- a/lib/Twig/Node/Expression/Test.php +++ b/lib/Twig/Node/Expression/Test.php @@ -16,14 +16,25 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression // defined is a special case if ('defined' === $name) { - if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) { + if ($node instanceof Twig_Node_Expression_Name) { $node->setAttribute('is_defined_test', true); + } elseif ($node instanceof Twig_Node_Expression_GetAttr) { + $this->changeIsDefined($node); } else { throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); } } } + protected function changeIsDefined(Twig_Node_Expression_GetAttr $node) + { + $node->setAttribute('is_defined_test', true); + + if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) { + $this->changeIsDefined($node->getNode('node')); + } + } + public function compile(Twig_Compiler $compiler) { $testMap = $compiler->getEnvironment()->getTests(); diff --git a/test/Twig/Tests/Fixtures/filters/default.test b/test/Twig/Tests/Fixtures/filters/default.test index f6bfb8f..d518332 100644 --- a/test/Twig/Tests/Fixtures/filters/default.test +++ b/test/Twig/Tests/Fixtures/filters/default.test @@ -23,7 +23,7 @@ Precedence: {{ 'o' ~ nullVar|default('k') }} {{ 'o' ~ nested.nullVar|default('k') }} Deep nested: -{{ nested.definedVar.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }} +{{ nested.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }} --DATA-- return array( 'definedVar' => 'defined', @@ -63,3 +63,42 @@ ok ok Deep nested: ok +--DATA-- +return array( + 'definedVar' => 'defined', + 'zeroVar' => 0, + 'emptyVar' => '', + 'nullVar' => null, + 'nested' => array( + 'definedVar' => 'defined', + 'zeroVar' => 0, + 'emptyVar' => '', + 'nullVar' => null, + ), +) +--CONFIG-- +return array('strict_variables' => true) +--EXPECT-- +Variable: +ok +ok +ok +ok +ok +Array access: +ok +ok +ok +ok +ok +ok +Plain values: +ok +ok +ok +ok +Precedence: +ok +ok +Deep nested: +ok -- 1.7.2.5