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();
104 foreach ($this->nodes as $node) {
105 $node->compile($compiler);
128 return array_key_exists($name, $this->attributes);
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];
155 $this->attributes[$name] = $value;
165 unset($this->attributes[$name]);
177 return array_key_exists($name, $this->nodes);
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];
204 $this->nodes[$name] = $node;
214 unset($this->nodes[$name]);
219 return count($this->nodes);
224 return new ArrayIterator($this->nodes);
hasNode($name)
Returns true if the node with the given identifier exists.
setNode($name, $node=null)
Sets a node.
Compiles a node to PHP code.
Represents a node in the AST.
removeAttribute($name)
Removes an attribute.
removeNode($name)
Removes a node by name.
getAttribute($name)
Gets an attribute.
hasAttribute($name)
Returns true if the attribute is defined.
setAttribute($name, $value)
Sets an attribute.
__construct(array $nodes=array(), array $attributes=array(), $lineno=0, $tag=null)
Constructor.
Represents a node in the AST.
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
getNode($name)
Gets a node by name.