1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 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
12namespace Twig\Node\Expression;
13
14use Twig\Compiler;
15use Twig\TwigTest;
16
17class TestExpression extends CallExpression
18{
19    public function __construct(\Twig_NodeInterface $node, $name, \Twig_NodeInterface $arguments = null, $lineno)
20    {
21        $nodes = ['node' => $node];
22        if (null !== $arguments) {
23            $nodes['arguments'] = $arguments;
24        }
25
26        parent::__construct($nodes, ['name' => $name], $lineno);
27    }
28
29    public function compile(Compiler $compiler)
30    {
31        $name = $this->getAttribute('name');
32        $test = $compiler->getEnvironment()->getTest($name);
33
34        $this->setAttribute('name', $name);
35        $this->setAttribute('type', 'test');
36        $this->setAttribute('thing', $test);
37        if ($test instanceof TwigTest) {
38            $this->setAttribute('arguments', $test->getArguments());
39        }
40        if ($test instanceof \Twig_TestCallableInterface || $test instanceof TwigTest) {
41            $this->setAttribute('callable', $test->getCallable());
42        }
43        if ($test instanceof TwigTest) {
44            $this->setAttribute('is_variadic', $test->isVariadic());
45        }
46
47        $this->compileCallable($compiler);
48    }
49}
50
51class_alias('Twig\Node\Expression\TestExpression', 'Twig_Node_Expression_Test');
52