]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/Twig/Node.php
931b4635a940601a15c3233cee2b81cca6f60285
4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 Armin Ronacher
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;
28 * The nodes are automatically made available as properties ($this->node).
29 * The attributes are automatically made available as array items ($this['name']).
31 * @param array $nodes An array of named nodes
32 * @param array $attributes An array of attributes (should not be nodes)
33 * @param integer $lineno The line number
34 * @param string $tag The tag name associated with the Node
36 public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
38 $this->nodes
= $nodes;
39 $this->attributes
= $attributes;
40 $this->lineno
= $lineno;
44 public function __toString()
46 $attributes = array();
47 foreach ($this->attributes
as $name => $value) {
48 $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
51 $repr = array(get_class($this).'('.implode(', ', $attributes));
53 if (count($this->nodes
)) {
54 foreach ($this->nodes
as $name => $node) {
55 $len = strlen($name) +
4;
57 foreach (explode("\n", (string) $node) as $line) {
58 $noderepr[] = str_repeat(' ', $len).$line;
61 $repr[] = sprintf(' %s: %s', $name, ltrim(implode("\n", $noderepr)));
69 return implode("\n", $repr);
72 public function toXml($asDom = false)
74 $dom = new DOMDocument('1.0', 'UTF-8');
75 $dom->formatOutput
= true;
76 $dom->appendChild($xml = $dom->createElement('twig'));
78 $xml->appendChild($node = $dom->createElement('node'));
79 $node->setAttribute('class', get_class($this));
81 foreach ($this->attributes
as $name => $value) {
82 $node->appendChild($attribute = $dom->createElement('attribute'));
83 $attribute->setAttribute('name', $name);
84 $attribute->appendChild($dom->createTextNode($value));
87 foreach ($this->nodes
as $name => $n) {
92 $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
93 $child = $dom->importNode($child, true);
94 $child->setAttribute('name', $name);
96 $node->appendChild($child);
99 return $asDom ? $dom : $dom->saveXml();
102 public function compile(Twig_Compiler
$compiler)
104 foreach ($this->nodes
as $node) {
105 $node->compile($compiler);
109 public function getLine()
111 return $this->lineno
;
114 public function getNodeTag()
120 * Returns true if the attribute is defined.
122 * @param string The attribute name
124 * @return Boolean true if the attribute is defined, false otherwise
126 public function hasAttribute($name)
128 return array_key_exists($name, $this->attributes
);
134 * @param string The attribute name
136 * @return mixed The attribute value
138 public function getAttribute($name)
140 if (!array_key_exists($name, $this->attributes
)) {
141 throw new LogicException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
144 return $this->attributes
[$name];
150 * @param string The attribute name
151 * @param mixed The attribute value
153 public function setAttribute($name, $value)
155 $this->attributes
[$name] = $value;
159 * Removes an attribute.
161 * @param string The attribute name
163 public function removeAttribute($name)
165 unset($this->attributes
[$name]);
169 * Returns true if the node with the given identifier exists.
171 * @param string The node name
173 * @return Boolean true if the node with the given name exists, false otherwise
175 public function hasNode($name)
177 return array_key_exists($name, $this->nodes
);
181 * Gets a node by name.
183 * @param string The node name
185 * @return Twig_Node A Twig_Node instance
187 public function getNode($name)
189 if (!array_key_exists($name, $this->nodes
)) {
190 throw new LogicException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
193 return $this->nodes
[$name];
199 * @param string The node name
200 * @param Twig_Node A Twig_Node instance
202 public function setNode($name, $node = null)
204 $this->nodes
[$name] = $node;
208 * Removes a node by name.
210 * @param string The node name
212 public function removeNode($name)
214 unset($this->nodes
[$name]);
217 public function count()
219 return count($this->nodes
);
222 public function getIterator()
224 return new ArrayIterator($this->nodes
);