Node.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 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. /**
  12. * Represents a node in the AST.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Node implements Twig_NodeInterface
  17. {
  18. protected $nodes;
  19. protected $attributes;
  20. protected $lineno;
  21. protected $tag;
  22. /**
  23. * Constructor.
  24. *
  25. * The nodes are automatically made available as properties ($this->node).
  26. * The attributes are automatically made available as array items ($this['name']).
  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(), array $attributes = array(), $lineno = 0, $tag = null)
  34. {
  35. $this->nodes = $nodes;
  36. $this->attributes = $attributes;
  37. $this->lineno = $lineno;
  38. $this->tag = $tag;
  39. }
  40. public function __toString()
  41. {
  42. $attributes = array();
  43. foreach ($this->attributes as $name => $value) {
  44. $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
  45. }
  46. $repr = array(get_class($this).'('.implode(', ', $attributes));
  47. if (count($this->nodes)) {
  48. foreach ($this->nodes as $name => $node) {
  49. $len = strlen($name) + 4;
  50. $noderepr = array();
  51. foreach (explode("\n", (string) $node) as $line) {
  52. $noderepr[] = str_repeat(' ', $len).$line;
  53. }
  54. $repr[] = sprintf(' %s: %s', $name, ltrim(implode("\n", $noderepr)));
  55. }
  56. $repr[] = ')';
  57. } else {
  58. $repr[0] .= ')';
  59. }
  60. return implode("\n", $repr);
  61. }
  62. /**
  63. * @deprecated since 1.16.1 (to be removed in 2.0)
  64. */
  65. public function toXml($asDom = false)
  66. {
  67. @trigger_error(sprintf('%s is deprecated since version 1.16.1 and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
  68. $dom = new DOMDocument('1.0', 'UTF-8');
  69. $dom->formatOutput = true;
  70. $dom->appendChild($xml = $dom->createElement('twig'));
  71. $xml->appendChild($node = $dom->createElement('node'));
  72. $node->setAttribute('class', get_class($this));
  73. foreach ($this->attributes as $name => $value) {
  74. $node->appendChild($attribute = $dom->createElement('attribute'));
  75. $attribute->setAttribute('name', $name);
  76. $attribute->appendChild($dom->createTextNode($value));
  77. }
  78. foreach ($this->nodes as $name => $n) {
  79. if (null === $n) {
  80. continue;
  81. }
  82. $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
  83. $child = $dom->importNode($child, true);
  84. $child->setAttribute('name', $name);
  85. $node->appendChild($child);
  86. }
  87. return $asDom ? $dom : $dom->saveXML();
  88. }
  89. public function compile(Twig_Compiler $compiler)
  90. {
  91. foreach ($this->nodes as $node) {
  92. $node->compile($compiler);
  93. }
  94. }
  95. public function getLine()
  96. {
  97. return $this->lineno;
  98. }
  99. public function getNodeTag()
  100. {
  101. return $this->tag;
  102. }
  103. /**
  104. * Returns true if the attribute is defined.
  105. *
  106. * @param string $name The attribute name
  107. *
  108. * @return bool true if the attribute is defined, false otherwise
  109. */
  110. public function hasAttribute($name)
  111. {
  112. return array_key_exists($name, $this->attributes);
  113. }
  114. /**
  115. * Gets an attribute value by name.
  116. *
  117. * @param string $name
  118. *
  119. * @return mixed
  120. */
  121. public function getAttribute($name)
  122. {
  123. if (!array_key_exists($name, $this->attributes)) {
  124. throw new LogicException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
  125. }
  126. return $this->attributes[$name];
  127. }
  128. /**
  129. * Sets an attribute by name to a value.
  130. *
  131. * @param string $name
  132. * @param mixed $value
  133. */
  134. public function setAttribute($name, $value)
  135. {
  136. $this->attributes[$name] = $value;
  137. }
  138. /**
  139. * Removes an attribute by name.
  140. *
  141. * @param string $name
  142. */
  143. public function removeAttribute($name)
  144. {
  145. unset($this->attributes[$name]);
  146. }
  147. /**
  148. * Returns true if the node with the given name exists.
  149. *
  150. * @param string $name
  151. *
  152. * @return bool
  153. */
  154. public function hasNode($name)
  155. {
  156. return array_key_exists($name, $this->nodes);
  157. }
  158. /**
  159. * Gets a node by name.
  160. *
  161. * @param string $name
  162. *
  163. * @return Twig_Node
  164. */
  165. public function getNode($name)
  166. {
  167. if (!array_key_exists($name, $this->nodes)) {
  168. throw new LogicException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
  169. }
  170. return $this->nodes[$name];
  171. }
  172. /**
  173. * Sets a node.
  174. *
  175. * @param string $name
  176. * @param Twig_Node $node
  177. */
  178. public function setNode($name, $node = null)
  179. {
  180. $this->nodes[$name] = $node;
  181. }
  182. /**
  183. * Removes a node by name.
  184. *
  185. * @param string $name
  186. */
  187. public function removeNode($name)
  188. {
  189. unset($this->nodes[$name]);
  190. }
  191. public function count()
  192. {
  193. return count($this->nodes);
  194. }
  195. public function getIterator()
  196. {
  197. return new ArrayIterator($this->nodes);
  198. }
  199. }