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
|
$name = $this->getAttribute('name');
|
21
|
$testMap = $compiler->getEnvironment()->getTests();
|
22
|
if (!isset($testMap[$name])) {
|
23
|
$message = sprintf('The test "%s" does not exist', $name);
|
24
|
if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getTests()))) {
|
25
|
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
26
|
}
|
27
|
|
28
|
throw new Twig_Error_Syntax($message, $this->getLine());
|
29
|
}
|
30
|
|
31
|
$name = $this->getAttribute('name');
|
32
|
$node = $this->getNode('node');
|
33
|
|
34
|
$compiler
|
35
|
->raw($testMap[$name]->compile().'(')
|
36
|
->subcompile($node)
|
37
|
;
|
38
|
|
39
|
if (null !== $this->getNode('arguments')) {
|
40
|
$compiler->raw(', ');
|
41
|
|
42
|
$max = count($this->getNode('arguments')) - 1;
|
43
|
foreach ($this->getNode('arguments') as $i => $arg) {
|
44
|
$compiler->subcompile($arg);
|
45
|
|
46
|
if ($i != $max) {
|
47
|
$compiler->raw(', ');
|
48
|
}
|
49
|
}
|
50
|
}
|
51
|
|
52
|
$compiler->raw(')');
|
53
|
}
|
54
|
}
|