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

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