Test.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Node_Expression_Test extends Twig_Node_Expression
  11. {
  12. public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
  13. {
  14. parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno);
  15. }
  16. public function compile(Twig_Compiler $compiler)
  17. {
  18. $testMap = $compiler->getEnvironment()->getTests();
  19. if (!isset($testMap[$this->getAttribute('name')])) {
  20. throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
  21. }
  22. $name = $this->getAttribute('name');
  23. $node = $this->getNode('node');
  24. $compiler
  25. ->raw($testMap[$name]->compile().'(')
  26. ->subcompile($node)
  27. ;
  28. if (null !== $this->getNode('arguments')) {
  29. $compiler->raw(', ');
  30. $max = count($this->getNode('arguments')) - 1;
  31. foreach ($this->getNode('arguments') as $i => $arg) {
  32. $compiler->subcompile($arg);
  33. if ($i != $max) {
  34. $compiler->raw(', ');
  35. }
  36. }
  37. }
  38. $compiler->raw(')');
  39. }
  40. }