1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of Twig. |
---|
5 | * |
---|
6 | * (c) 2010 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 | class Twig_Node_Expression_Test extends Twig_Node_Expression |
---|
12 | { |
---|
13 | public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno) |
---|
14 | { |
---|
15 | parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno); |
---|
16 | } |
---|
17 | |
---|
18 | public function compile(Twig_Compiler $compiler) |
---|
19 | { |
---|
20 | $testMap = $compiler->getEnvironment()->getTests(); |
---|
21 | if (!isset($testMap[$this->getAttribute('name')])) { |
---|
22 | throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine()); |
---|
23 | } |
---|
24 | |
---|
25 | // defined is a special case |
---|
26 | if ('defined' === $this->getAttribute('name')) { |
---|
27 | if ($this->getNode('node') instanceof Twig_Node_Expression_Name) { |
---|
28 | $compiler |
---|
29 | ->raw($testMap['defined']->compile().'(') |
---|
30 | ->repr($this->getNode('node')->getAttribute('name')) |
---|
31 | ->raw(', $context)') |
---|
32 | ; |
---|
33 | } elseif ($this->getNode('node') instanceof Twig_Node_Expression_GetAttr) { |
---|
34 | $this->getNode('node')->setAttribute('is_defined_test', true); |
---|
35 | $compiler->subcompile($this->getNode('node')); |
---|
36 | } else { |
---|
37 | throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); |
---|
38 | } |
---|
39 | return; |
---|
40 | } |
---|
41 | |
---|
42 | $compiler |
---|
43 | ->raw($testMap[$this->getAttribute('name')]->compile().'(') |
---|
44 | ->subcompile($this->getNode('node')) |
---|
45 | ; |
---|
46 | |
---|
47 | if (null !== $this->getNode('arguments')) { |
---|
48 | $compiler->raw(', '); |
---|
49 | |
---|
50 | $max = count($this->getNode('arguments')) - 1; |
---|
51 | foreach ($this->getNode('arguments') as $i => $node) { |
---|
52 | $compiler->subcompile($node); |
---|
53 | |
---|
54 | if ($i != $max) { |
---|
55 | $compiler->raw(', '); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | $compiler->raw(')'); |
---|
61 | } |
---|
62 | } |
---|