diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-02 22:40:51 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-02 22:40:51 +0200 |
commit | a4565e88edbc8e3bd092a475469769c86a4c350c (patch) | |
tree | a6a3c935b03a23ff87575c8c315cf8ba78fe68c2 /inc/Twig/Node/Expression/Filter/Default.php | |
parent | f6c9baab3efeec1d0efa151e276fc08d5b58f9e9 (diff) | |
download | wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.gz wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.zst wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.zip |
add Twig & refactor poche
Diffstat (limited to 'inc/Twig/Node/Expression/Filter/Default.php')
-rw-r--r-- | inc/Twig/Node/Expression/Filter/Default.php | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/inc/Twig/Node/Expression/Filter/Default.php b/inc/Twig/Node/Expression/Filter/Default.php new file mode 100644 index 00000000..1827c888 --- /dev/null +++ b/inc/Twig/Node/Expression/Filter/Default.php | |||
@@ -0,0 +1,43 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of Twig. | ||
5 | * | ||
6 | * (c) 2011 Fabien Potencier | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | /** | ||
13 | * Returns the value or the default value when it is undefined or empty. | ||
14 | * | ||
15 | * <pre> | ||
16 | * {{ var.foo|default('foo item on var is not defined') }} | ||
17 | * </pre> | ||
18 | * | ||
19 | * @author Fabien Potencier <fabien@symfony.com> | ||
20 | */ | ||
21 | class Twig_Node_Expression_Filter_Default extends Twig_Node_Expression_Filter | ||
22 | { | ||
23 | public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null) | ||
24 | { | ||
25 | $default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('default', $node->getLine()), $arguments, $node->getLine()); | ||
26 | |||
27 | if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) { | ||
28 | $test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getLine()); | ||
29 | $false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); | ||
30 | |||
31 | $node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine()); | ||
32 | } else { | ||
33 | $node = $default; | ||
34 | } | ||
35 | |||
36 | parent::__construct($node, $filterName, $arguments, $lineno, $tag); | ||
37 | } | ||
38 | |||
39 | public function compile(Twig_Compiler $compiler) | ||
40 | { | ||
41 | $compiler->subcompile($this->getNode('node')); | ||
42 | } | ||
43 | } | ||