vendor/twig/twig/src/Node/Node.php line 87

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\Attribute\YieldReady;
  13. use Twig\Compiler;
  14. use Twig\Source;
  15. /**
  16.  * Represents a node in the AST.
  17.  *
  18.  * @author Fabien Potencier <fabien@symfony.com>
  19.  */
  20. #[YieldReady]
  21. class Node implements \Countable\IteratorAggregate
  22. {
  23.     protected $nodes;
  24.     protected $attributes;
  25.     protected $lineno;
  26.     protected $tag;
  27.     private $sourceContext;
  28.     /**
  29.      * @param array  $nodes      An array of named nodes
  30.      * @param array  $attributes An array of attributes (should not be nodes)
  31.      * @param int    $lineno     The line number
  32.      * @param string $tag        The tag name associated with the Node
  33.      */
  34.     public function __construct(array $nodes = [], array $attributes = [], int $lineno 0, ?string $tag null)
  35.     {
  36.         foreach ($nodes as $name => $node) {
  37.             if (!$node instanceof self) {
  38.                 throw new \InvalidArgumentException(sprintf('Using "%s" for the value of node "%s" of "%s" is not supported. You must pass a \Twig\Node\Node instance.'\is_object($node) ? \get_class($node) : (null === $node 'null' \gettype($node)), $name, static::class));
  39.             }
  40.         }
  41.         $this->nodes $nodes;
  42.         $this->attributes $attributes;
  43.         $this->lineno $lineno;
  44.         $this->tag $tag;
  45.     }
  46.     public function __toString()
  47.     {
  48.         $attributes = [];
  49.         foreach ($this->attributes as $name => $value) {
  50.             $attributes[] = sprintf('%s: %s'$namestr_replace("\n"''var_export($valuetrue)));
  51.         }
  52.         $repr = [static::class.'('.implode(', '$attributes)];
  53.         if (\count($this->nodes)) {
  54.             foreach ($this->nodes as $name => $node) {
  55.                 $len \strlen($name) + 4;
  56.                 $noderepr = [];
  57.                 foreach (explode("\n", (string) $node) as $line) {
  58.                     $noderepr[] = str_repeat(' '$len).$line;
  59.                 }
  60.                 $repr[] = sprintf('  %s: %s'$nameltrim(implode("\n"$noderepr)));
  61.             }
  62.             $repr[] = ')';
  63.         } else {
  64.             $repr[0] .= ')';
  65.         }
  66.         return implode("\n"$repr);
  67.     }
  68.     /**
  69.      * @return void
  70.      */
  71.     public function compile(Compiler $compiler)
  72.     {
  73.         foreach ($this->nodes as $node) {
  74.             $compiler->subcompile($node);
  75.         }
  76.     }
  77.     public function getTemplateLine(): int
  78.     {
  79.         return $this->lineno;
  80.     }
  81.     public function getNodeTag(): ?string
  82.     {
  83.         return $this->tag;
  84.     }
  85.     public function hasAttribute(string $name): bool
  86.     {
  87.         return \array_key_exists($name$this->attributes);
  88.     }
  89.     public function getAttribute(string $name)
  90.     {
  91.         if (!\array_key_exists($name$this->attributes)) {
  92.             throw new \LogicException(sprintf('Attribute "%s" does not exist for Node "%s".'$name, static::class));
  93.         }
  94.         return $this->attributes[$name];
  95.     }
  96.     public function setAttribute(string $name$value): void
  97.     {
  98.         $this->attributes[$name] = $value;
  99.     }
  100.     public function removeAttribute(string $name): void
  101.     {
  102.         unset($this->attributes[$name]);
  103.     }
  104.     public function hasNode(string $name): bool
  105.     {
  106.         return isset($this->nodes[$name]);
  107.     }
  108.     public function getNode(string $name): self
  109.     {
  110.         if (!isset($this->nodes[$name])) {
  111.             throw new \LogicException(sprintf('Node "%s" does not exist for Node "%s".'$name, static::class));
  112.         }
  113.         return $this->nodes[$name];
  114.     }
  115.     public function setNode(string $nameself $node): void
  116.     {
  117.         $this->nodes[$name] = $node;
  118.     }
  119.     public function removeNode(string $name): void
  120.     {
  121.         unset($this->nodes[$name]);
  122.     }
  123.     /**
  124.      * @return int
  125.      */
  126.     #[\ReturnTypeWillChange]
  127.     public function count()
  128.     {
  129.         return \count($this->nodes);
  130.     }
  131.     public function getIterator(): \Traversable
  132.     {
  133.         return new \ArrayIterator($this->nodes);
  134.     }
  135.     public function getTemplateName(): ?string
  136.     {
  137.         return $this->sourceContext $this->sourceContext->getName() : null;
  138.     }
  139.     public function setSourceContext(Source $source): void
  140.     {
  141.         $this->sourceContext $source;
  142.         foreach ($this->nodes as $node) {
  143.             $node->setSourceContext($source);
  144.         }
  145.     }
  146.     public function getSourceContext(): ?Source
  147.     {
  148.         return $this->sourceContext;
  149.     }
  150. }