vendor/twig/twig/src/Node/IfNode.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  * (c) Armin Ronacher
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Twig\Node;
  12. use Twig\Compiler;
  13. /**
  14.  * Represents an if node.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  */
  18. class IfNode extends Node
  19. {
  20.     public function __construct(Node $tests, ?Node $elseint $linenostring $tag null)
  21.     {
  22.         $nodes = ['tests' => $tests];
  23.         if (null !== $else) {
  24.             $nodes['else'] = $else;
  25.         }
  26.         parent::__construct($nodes, [], $lineno$tag);
  27.     }
  28.     public function compile(Compiler $compiler): void
  29.     {
  30.         $compiler->addDebugInfo($this);
  31.         for ($i 0$count = \count($this->getNode('tests')); $i $count$i += 2) {
  32.             if ($i 0) {
  33.                 $compiler
  34.                     ->outdent()
  35.                     ->write('} elseif (')
  36.                 ;
  37.             } else {
  38.                 $compiler
  39.                     ->write('if (')
  40.                 ;
  41.             }
  42.             $compiler
  43.                 ->subcompile($this->getNode('tests')->getNode($i))
  44.                 ->raw(") {\n")
  45.                 ->indent()
  46.             ;
  47.             // The node might not exists if the content is empty
  48.             if ($this->getNode('tests')->hasNode($i 1)) {
  49.                 $compiler->subcompile($this->getNode('tests')->getNode($i 1));
  50.             }
  51.         }
  52.         if ($this->hasNode('else')) {
  53.             $compiler
  54.                 ->outdent()
  55.                 ->write("} else {\n")
  56.                 ->indent()
  57.                 ->subcompile($this->getNode('else'))
  58.             ;
  59.         }
  60.         $compiler
  61.             ->outdent()
  62.             ->write("}\n");
  63.     }
  64. }