ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Node.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) Armin Ronacher
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12 
18 class Twig_Node implements Twig_NodeInterface
19 {
20  protected $nodes;
21  protected $attributes;
22  protected $lineno;
23  protected $tag;
24 
25  private $name;
26 
38  public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
39  {
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);
43  }
44  }
45  $this->nodes = $nodes;
46  $this->attributes = $attributes;
47  $this->lineno = $lineno;
48  $this->tag = $tag;
49  }
50 
51  public function __toString()
52  {
53  $attributes = array();
54  foreach ($this->attributes as $name => $value) {
55  $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
56  }
57 
58  $repr = array(get_class($this).'('.implode(', ', $attributes));
59 
60  if (count($this->nodes)) {
61  foreach ($this->nodes as $name => $node) {
62  $len = strlen($name) + 4;
63  $noderepr = array();
64  foreach (explode("\n", (string) $node) as $line) {
65  $noderepr[] = str_repeat(' ', $len).$line;
66  }
67 
68  $repr[] = sprintf(' %s: %s', $name, ltrim(implode("\n", $noderepr)));
69  }
70 
71  $repr[] = ')';
72  } else {
73  $repr[0] .= ')';
74  }
75 
76  return implode("\n", $repr);
77  }
78 
82  public function toXml($asDom = false)
83  {
84  @trigger_error(sprintf('%s is deprecated since version 1.16.1 and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
85 
86  $dom = new DOMDocument('1.0', 'UTF-8');
87  $dom->formatOutput = true;
88  $dom->appendChild($xml = $dom->createElement('twig'));
89 
90  $xml->appendChild($node = $dom->createElement('node'));
91  $node->setAttribute('class', get_class($this));
92 
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));
97  }
98 
99  foreach ($this->nodes as $name => $n) {
100  if (null === $n) {
101  continue;
102  }
103 
104  $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
105  $child = $dom->importNode($child, true);
106  $child->setAttribute('name', $name);
107 
108  $node->appendChild($child);
109  }
110 
111  return $asDom ? $dom : $dom->saveXML();
112  }
113 
114  public function compile(Twig_Compiler $compiler)
115  {
116  foreach ($this->nodes as $node) {
117  $node->compile($compiler);
118  }
119  }
120 
121  public function getTemplateLine()
122  {
123  return $this->lineno;
124  }
125 
129  public function getLine()
130  {
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);
132 
133  return $this->lineno;
134  }
135 
136  public function getNodeTag()
137  {
138  return $this->tag;
139  }
140 
144  public function hasAttribute($name)
145  {
146  return array_key_exists($name, $this->attributes);
147  }
148 
152  public function getAttribute($name)
153  {
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)));
156  }
157 
158  return $this->attributes[$name];
159  }
160 
165  public function setAttribute($name, $value)
166  {
167  $this->attributes[$name] = $value;
168  }
169 
170  public function removeAttribute($name)
171  {
172  unset($this->attributes[$name]);
173  }
174 
178  public function hasNode($name)
179  {
180  return array_key_exists($name, $this->nodes);
181  }
182 
186  public function getNode($name)
187  {
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)));
190  }
191 
192  return $this->nodes[$name];
193  }
194 
195  public function setNode($name, $node = null)
196  {
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);
199  }
200 
201  $this->nodes[$name] = $node;
202  }
203 
204  public function removeNode($name)
205  {
206  unset($this->nodes[$name]);
207  }
208 
209  public function count()
210  {
211  return count($this->nodes);
212  }
213 
214  public function getIterator()
215  {
216  return new ArrayIterator($this->nodes);
217  }
218 
219  public function setTemplateName($name)
220  {
221  $this->name = $name;
222  foreach ($this->nodes as $node) {
223  if (null !== $node) {
224  $node->setTemplateName($name);
225  }
226  }
227  }
228 
229  public function getTemplateName()
230  {
231  return $this->name;
232  }
233 
237  public function setFilename($name)
238  {
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);
240 
241  $this->setTemplateName($name);
242  }
243 
247  public function getFilename()
248  {
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);
250 
251  return $this->name;
252  }
253 }
254 
255 class_alias('Twig_Node', 'Twig\Node\Node', false);
256 class_exists('Twig_Compiler');
$lineno
Definition: Node.php:22
Represents a node in the AST.
getTemplateName()
Definition: Node.php:229
$attributes
Definition: Node.php:21
removeAttribute($name)
Definition: Node.php:170
Represents a node in the AST.
Definition: Node.php:18
getLine()
Definition: Node.php:129
setTemplateName($name)
Definition: Node.php:219
hasAttribute($name)
Definition: Node.php:144
getFilename()
Definition: Node.php:247
setNode($name, $node=null)
Definition: Node.php:195
$xml
Definition: metadata.php:240
$nodes
Definition: Node.php:20
getNodeTag()
Definition: Node.php:136
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: Node.php:114
removeNode($name)
Definition: Node.php:204
getAttribute($name)
Definition: Node.php:152
getTemplateLine()
Definition: Node.php:121
$n
Definition: RandomTest.php:85
toXml($asDom=false)
Definition: Node.php:82
hasNode($name)
Definition: Node.php:178
Create styles array
The data for the language used.
getNode($name)
Definition: Node.php:186
getIterator()
Definition: Node.php:214
__construct(array $nodes=array(), array $attributes=array(), $lineno=0, $tag=null)
Constructor.
Definition: Node.php:38
setAttribute($name, $value)
Definition: Node.php:165
setFilename($name)
Definition: Node.php:237
count()
Definition: Node.php:209
__toString()
Definition: Node.php:51