From: nikic Date: Sun, 30 Oct 2011 10:19:27 +0000 (+0100) Subject: Allow defined test and default filter on methods X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=930330fff0748a61c88ffdb010db6455dc7d07db;p=web%2Fkonrad%2Ftwig.git Allow defined test and default filter on methods --- diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php index 3a26bcf..8991f52 100644 --- a/lib/Twig/Node/Expression/Test.php +++ b/lib/Twig/Node/Expression/Test.php @@ -35,10 +35,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) { $this->changeIgnoreStrictCheck($node->getNode('node')); } - - if (count($node->getNode('arguments'))) { - throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); - } } public function compile(Twig_Compiler $compiler) diff --git a/test/Twig/Tests/Fixtures/filters/default.test b/test/Twig/Tests/Fixtures/filters/default.test index 1614a87..22e86e4 100644 --- a/test/Twig/Tests/Fixtures/filters/default.test +++ b/test/Twig/Tests/Fixtures/filters/default.test @@ -27,6 +27,10 @@ Precedence: Object methods: {{ object.foo |default('default') is sameas('default') ? 'ko' : 'ok' }} {{ object.undefinedMethod |default('default') is sameas('default') ? 'ok' : 'ko' }} +{{ object.getFoo() |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ object.getFoo('a') |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ object.undefinedMethod() |default('default') is sameas('default') ? 'ok' : 'ko' }} +{{ object.undefinedMethod('a') |default('default') is sameas('default') ? 'ok' : 'ko' }} Deep nested: {{ nested.undefinedVar.foo.bar |default('default') is sameas('default') ? 'ok' : 'ko' }} {{ nested.definedArray.0 |default('default') is sameas('default') ? 'ko' : 'ok' }} @@ -78,6 +82,10 @@ ok Object methods: ok ok +ok +ok +ok +ok Deep nested: ok ok @@ -129,6 +137,10 @@ ok Object methods: ok ok +ok +ok +ok +ok Deep nested: ok ok diff --git a/test/Twig/Tests/Fixtures/tests/defined.test b/test/Twig/Tests/Fixtures/tests/defined.test index 186283d..2fbaa86 100644 --- a/test/Twig/Tests/Fixtures/tests/defined.test +++ b/test/Twig/Tests/Fixtures/tests/defined.test @@ -19,6 +19,10 @@ {{ nested['definedArray'][0] is defined ? 'ok' : 'ko' }} {{ object.foo is defined ? 'ok' : 'ko' }} {{ object.undefinedMethod is defined ? 'ko' : 'ok' }} +{{ object.getFoo() is defined ? 'ok' : 'ko' }} +{{ object.getFoo('a') is defined ? 'ok' : 'ko' }} +{{ object.undefinedMethod() is defined ? 'ko' : 'ok' }} +{{ object.undefinedMethod('a') is defined ? 'ko' : 'ok' }} {{ object.self.foo is defined ? 'ok' : 'ko' }} {{ object.self.undefinedMethod is defined ? 'ko' : 'ok' }} {{ object.undefinedMethod.self is defined ? 'ko' : 'ok' }} @@ -57,6 +61,10 @@ ok ok ok ok +ok +ok +ok +ok --DATA-- return array( 'definedVar' => 'defined', @@ -94,3 +102,7 @@ ok ok ok ok +ok +ok +ok +ok