4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Represents a node in the AST.
16 * @author Fabien Potencier <fabien@symfony.com>
18 class Twig_Node implements Twig_NodeInterface
21 protected $attributes;
30 * The nodes are automatically made available as properties ($this->node).
31 * The attributes are automatically made available as array items ($this['name']).
33 * @param array $nodes An array of named nodes
34 * @param array $attributes An array of attributes (should not be nodes)
35 * @param int $lineno The line number
36 * @param string $tag The tag name associated with the Node
38 public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
40 foreach ($nodes as $name => $node) {
41 if (!$node instanceof Twig_NodeInterface) {
42 @trigger_error(sprintf('Using "%s" for the value of node "%s" of "%s" is deprecated since version 1.25 and will be removed in 2.0.', is_object($node) ? get_class($node) : null === $node ? 'null' : gettype($node), $name, get_class($this)), E_USER_DEPRECATED);
45 $this->nodes = $nodes;
46 $this->attributes = $attributes;
47 $this->lineno = $lineno;
51 public function __toString()
53 $attributes = array();
54 foreach ($this->attributes as $name => $value) {
55 $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
58 $repr = array(get_class($this).'('.implode(', ', $attributes));
60 if (count($this->nodes)) {
61 foreach ($this->nodes as $name => $node) {
62 $len = strlen($name) + 4;
64 foreach (explode("\n", (string) $node) as $line) {
65 $noderepr[] = str_repeat(' ', $len).$line;
68 $repr[] = sprintf(' %s: %s', $name, ltrim(implode("\n", $noderepr)));
76 return implode("\n", $repr);
80 * @deprecated since 1.16.1 (to be removed in 2.0)
82 public function toXml($asDom = false)
84 @trigger_error(sprintf('%s is deprecated since version 1.16.1 and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
86 $dom = new DOMDocument('1.0', 'UTF-8');
87 $dom->formatOutput = true;
88 $dom->appendChild($xml = $dom->createElement('twig'));
90 $xml->appendChild($node = $dom->createElement('node'));
91 $node->setAttribute('class', get_class($this));
93 foreach ($this->attributes as $name => $value) {
94 $node->appendChild($attribute = $dom->createElement('attribute'));
95 $attribute->setAttribute('name', $name);
96 $attribute->appendChild($dom->createTextNode($value));
99 foreach ($this->nodes as $name => $n) {
104 $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
105 $child = $dom->importNode($child, true);
106 $child->setAttribute('name', $name);
108 $node->appendChild($child);
111 return $asDom ? $dom : $dom->saveXML();
114 public function compile(Twig_Compiler $compiler)
116 foreach ($this->nodes as $node) {
117 $node->compile($compiler);
121 public function getTemplateLine()
123 return $this->lineno;
127 * @deprecated since 1.27 (to be removed in 2.0)
129 public function getLine()
131 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getTemplateLine() instead.', E_USER_DEPRECATED);
133 return $this->lineno;
136 public function getNodeTag()
144 public function hasAttribute($name)
146 return array_key_exists($name, $this->attributes);
152 public function getAttribute($name)
154 if (!array_key_exists($name, $this->attributes)) {
155 throw new LogicException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
158 return $this->attributes[$name];
162 * @param string $name
163 * @param mixed $value
165 public function setAttribute($name, $value)
167 $this->attributes[$name] = $value;
170 public function removeAttribute($name)
172 unset($this->attributes[$name]);
178 public function hasNode($name)
180 return array_key_exists($name, $this->nodes);
186 public function getNode($name)
188 if (!array_key_exists($name, $this->nodes)) {
189 throw new LogicException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
192 return $this->nodes[$name];
195 public function setNode($name, $node = null)
197 if (!$node instanceof Twig_NodeInterface) {
198 @trigger_error(sprintf('Using "%s" for the value of node "%s" of "%s" is deprecated since version 1.25 and will be removed in 2.0.', is_object($node) ? get_class($node) : null === $node ? 'null' : gettype($node), $name, get_class($this)), E_USER_DEPRECATED);
201 $this->nodes[$name] = $node;
204 public function removeNode($name)
206 unset($this->nodes[$name]);
209 public function count()
211 return count($this->nodes);
214 public function getIterator()
216 return new ArrayIterator($this->nodes);
219 public function setTemplateName($name)
222 foreach ($this->nodes as $node) {
223 if (null !== $node) {
224 $node->setTemplateName($name);
229 public function getTemplateName()
235 * @deprecated since 1.27 (to be removed in 2.0)
237 public function setFilename($name)
239 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use setTemplateName() instead.', E_USER_DEPRECATED);
241 $this->setTemplateName($name);
245 * @deprecated since 1.27 (to be removed in 2.0)
247 public function getFilename()
249 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getTemplateName() instead.', E_USER_DEPRECATED);