|
0
|
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 |
$name = $this->getAttribute('name'); |
|
|
26 |
$node = $this->getNode('node'); |
|
|
27 |
|
|
|
28 |
// defined is a special case |
|
|
29 |
if ('defined' === $name) { |
|
|
30 |
if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) { |
|
|
31 |
$node->setAttribute('is_defined_test', true); |
|
|
32 |
$compiler->subcompile($node); |
|
|
33 |
$node->removeAttribute('is_defined_test'); |
|
|
34 |
} else { |
|
|
35 |
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); |
|
|
36 |
} |
|
|
37 |
return; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
$compiler |
|
|
41 |
->raw($testMap[$name]->compile().'(') |
|
|
42 |
->subcompile($node) |
|
|
43 |
; |
|
|
44 |
|
|
|
45 |
if (null !== $this->getNode('arguments')) { |
|
|
46 |
$compiler->raw(', '); |
|
|
47 |
|
|
|
48 |
$max = count($this->getNode('arguments')) - 1; |
|
|
49 |
foreach ($this->getNode('arguments') as $i => $arg) { |
|
|
50 |
$compiler->subcompile($arg); |
|
|
51 |
|
|
|
52 |
if ($i != $max) { |
|
|
53 |
$compiler->raw(', '); |
|
|
54 |
} |
|
|
55 |
} |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
$compiler->raw(')'); |
|
|
59 |
} |
|
|
60 |
} |