/wp-content/plugins/mailpoet/vendor-prefixed/twig/twig/src/Node/Node.php

https://gitlab.com/remyvianne/krowkaramel · PHP · 140 lines · 137 code · 0 blank · 3 comment · 8 complexity · 52e349760391483e7eb560201a752fa1 MD5 · raw file

  1. <?php
  2. namespace MailPoetVendor\Twig\Node;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Twig\Compiler;
  5. use MailPoetVendor\Twig\Source;
  6. class Node implements \Countable, \IteratorAggregate
  7. {
  8. protected $nodes;
  9. protected $attributes;
  10. protected $lineno;
  11. protected $tag;
  12. private $name;
  13. private $sourceContext;
  14. public function __construct(array $nodes = [], array $attributes = [], int $lineno = 0, string $tag = null)
  15. {
  16. foreach ($nodes as $name => $node) {
  17. if (!$node instanceof self) {
  18. 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));
  19. }
  20. }
  21. $this->nodes = $nodes;
  22. $this->attributes = $attributes;
  23. $this->lineno = $lineno;
  24. $this->tag = $tag;
  25. }
  26. public function __toString()
  27. {
  28. $attributes = [];
  29. foreach ($this->attributes as $name => $value) {
  30. $attributes[] = \sprintf('%s: %s', $name, \str_replace("\n", '', \var_export($value, \true)));
  31. }
  32. $repr = [static::class . '(' . \implode(', ', $attributes)];
  33. if (\count($this->nodes)) {
  34. foreach ($this->nodes as $name => $node) {
  35. $len = \strlen($name) + 4;
  36. $noderepr = [];
  37. foreach (\explode("\n", (string) $node) as $line) {
  38. $noderepr[] = \str_repeat(' ', $len) . $line;
  39. }
  40. $repr[] = \sprintf(' %s: %s', $name, \ltrim(\implode("\n", $noderepr)));
  41. }
  42. $repr[] = ')';
  43. } else {
  44. $repr[0] .= ')';
  45. }
  46. return \implode("\n", $repr);
  47. }
  48. public function compile(Compiler $compiler)
  49. {
  50. foreach ($this->nodes as $node) {
  51. $node->compile($compiler);
  52. }
  53. }
  54. public function getTemplateLine()
  55. {
  56. return $this->lineno;
  57. }
  58. public function getNodeTag()
  59. {
  60. return $this->tag;
  61. }
  62. public function hasAttribute($name)
  63. {
  64. return \array_key_exists($name, $this->attributes);
  65. }
  66. public function getAttribute($name)
  67. {
  68. if (!\array_key_exists($name, $this->attributes)) {
  69. throw new \LogicException(\sprintf('Attribute "%s" does not exist for Node "%s".', $name, static::class));
  70. }
  71. return $this->attributes[$name];
  72. }
  73. public function setAttribute($name, $value)
  74. {
  75. $this->attributes[$name] = $value;
  76. }
  77. public function removeAttribute($name)
  78. {
  79. unset($this->attributes[$name]);
  80. }
  81. public function hasNode($name)
  82. {
  83. return isset($this->nodes[$name]);
  84. }
  85. public function getNode($name)
  86. {
  87. if (!isset($this->nodes[$name])) {
  88. throw new \LogicException(\sprintf('Node "%s" does not exist for Node "%s".', $name, static::class));
  89. }
  90. return $this->nodes[$name];
  91. }
  92. public function setNode($name, self $node)
  93. {
  94. $this->nodes[$name] = $node;
  95. }
  96. public function removeNode($name)
  97. {
  98. unset($this->nodes[$name]);
  99. }
  100. #[\ReturnTypeWillChange]
  101. public function count()
  102. {
  103. return \count($this->nodes);
  104. }
  105. #[\ReturnTypeWillChange]
  106. public function getIterator()
  107. {
  108. return new \ArrayIterator($this->nodes);
  109. }
  110. public function setTemplateName($name)
  111. {
  112. $triggerDeprecation = 2 > \func_num_args() || \func_get_arg(1);
  113. if ($triggerDeprecation) {
  114. @\trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.8 and will be removed in 3.0. Use setSourceContext() instead.', \E_USER_DEPRECATED);
  115. }
  116. $this->name = $name;
  117. foreach ($this->nodes as $node) {
  118. $node->setTemplateName($name, $triggerDeprecation);
  119. }
  120. }
  121. public function getTemplateName()
  122. {
  123. return $this->sourceContext ? $this->sourceContext->getName() : null;
  124. }
  125. public function setSourceContext(Source $source)
  126. {
  127. $this->sourceContext = $source;
  128. foreach ($this->nodes as $node) {
  129. $node->setSourceContext($source);
  130. }
  131. $this->setTemplateName($source->getName(), \false);
  132. }
  133. public function getSourceContext()
  134. {
  135. return $this->sourceContext;
  136. }
  137. }
  138. \class_alias('MailPoetVendor\\Twig\\Node\\Node', 'MailPoetVendor\\Twig_Node');
  139. // Ensure that the aliased name is loaded to keep BC for classes implementing the typehint with the old aliased name.
  140. \class_exists('MailPoetVendor\\Twig\\Compiler');