aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/Twig/Node.php
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2013-08-02 22:43:56 +0200
committerNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2013-08-02 22:43:56 +0200
commit8069e235fd2971675ee5fc05026ffa9bce5cbbb7 (patch)
tree872eaed228b5e1b031c21770fba1701106a52a39 /inc/3rdparty/Twig/Node.php
parent45161a64026cec35fcf07659508143a6f55ddf57 (diff)
downloadwallabag-8069e235fd2971675ee5fc05026ffa9bce5cbbb7.tar.gz
wallabag-8069e235fd2971675ee5fc05026ffa9bce5cbbb7.tar.zst
wallabag-8069e235fd2971675ee5fc05026ffa9bce5cbbb7.zip
move Twig in 3rdparty
Diffstat (limited to 'inc/3rdparty/Twig/Node.php')
-rw-r--r--inc/3rdparty/Twig/Node.php226
1 files changed, 226 insertions, 0 deletions
diff --git a/inc/3rdparty/Twig/Node.php b/inc/3rdparty/Twig/Node.php
new file mode 100644
index 00000000..931b4635
--- /dev/null
+++ b/inc/3rdparty/Twig/Node.php
@@ -0,0 +1,226 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 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
13/**
14 * Represents a node in the AST.
15 *
16 * @author Fabien Potencier <fabien@symfony.com>
17 */
18class Twig_Node implements Twig_NodeInterface
19{
20 protected $nodes;
21 protected $attributes;
22 protected $lineno;
23 protected $tag;
24
25 /**
26 * Constructor.
27 *
28 * The nodes are automatically made available as properties ($this->node).
29 * The attributes are automatically made available as array items ($this['name']).
30 *
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
35 */
36 public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
37 {
38 $this->nodes = $nodes;
39 $this->attributes = $attributes;
40 $this->lineno = $lineno;
41 $this->tag = $tag;
42 }
43
44 public function __toString()
45 {
46 $attributes = array();
47 foreach ($this->attributes as $name => $value) {
48 $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
49 }
50
51 $repr = array(get_class($this).'('.implode(', ', $attributes));
52
53 if (count($this->nodes)) {
54 foreach ($this->nodes as $name => $node) {
55 $len = strlen($name) + 4;
56 $noderepr = array();
57 foreach (explode("\n", (string) $node) as $line) {
58 $noderepr[] = str_repeat(' ', $len).$line;
59 }
60
61 $repr[] = sprintf(' %s: %s', $name, ltrim(implode("\n", $noderepr)));
62 }
63
64 $repr[] = ')';
65 } else {
66 $repr[0] .= ')';
67 }
68
69 return implode("\n", $repr);
70 }
71
72 public function toXml($asDom = false)
73 {
74 $dom = new DOMDocument('1.0', 'UTF-8');
75 $dom->formatOutput = true;
76 $dom->appendChild($xml = $dom->createElement('twig'));
77
78 $xml->appendChild($node = $dom->createElement('node'));
79 $node->setAttribute('class', get_class($this));
80
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));
85 }
86
87 foreach ($this->nodes as $name => $n) {
88 if (null === $n) {
89 continue;
90 }
91
92 $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
93 $child = $dom->importNode($child, true);
94 $child->setAttribute('name', $name);
95
96 $node->appendChild($child);
97 }
98
99 return $asDom ? $dom : $dom->saveXml();
100 }
101
102 public function compile(Twig_Compiler $compiler)
103 {
104 foreach ($this->nodes as $node) {
105 $node->compile($compiler);
106 }
107 }
108
109 public function getLine()
110 {
111 return $this->lineno;
112 }
113
114 public function getNodeTag()
115 {
116 return $this->tag;
117 }
118
119 /**
120 * Returns true if the attribute is defined.
121 *
122 * @param string The attribute name
123 *
124 * @return Boolean true if the attribute is defined, false otherwise
125 */
126 public function hasAttribute($name)
127 {
128 return array_key_exists($name, $this->attributes);
129 }
130
131 /**
132 * Gets an attribute.
133 *
134 * @param string The attribute name
135 *
136 * @return mixed The attribute value
137 */
138 public function getAttribute($name)
139 {
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)));
142 }
143
144 return $this->attributes[$name];
145 }
146
147 /**
148 * Sets an attribute.
149 *
150 * @param string The attribute name
151 * @param mixed The attribute value
152 */
153 public function setAttribute($name, $value)
154 {
155 $this->attributes[$name] = $value;
156 }
157
158 /**
159 * Removes an attribute.
160 *
161 * @param string The attribute name
162 */
163 public function removeAttribute($name)
164 {
165 unset($this->attributes[$name]);
166 }
167
168 /**
169 * Returns true if the node with the given identifier exists.
170 *
171 * @param string The node name
172 *
173 * @return Boolean true if the node with the given name exists, false otherwise
174 */
175 public function hasNode($name)
176 {
177 return array_key_exists($name, $this->nodes);
178 }
179
180 /**
181 * Gets a node by name.
182 *
183 * @param string The node name
184 *
185 * @return Twig_Node A Twig_Node instance
186 */
187 public function getNode($name)
188 {
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)));
191 }
192
193 return $this->nodes[$name];
194 }
195
196 /**
197 * Sets a node.
198 *
199 * @param string The node name
200 * @param Twig_Node A Twig_Node instance
201 */
202 public function setNode($name, $node = null)
203 {
204 $this->nodes[$name] = $node;
205 }
206
207 /**
208 * Removes a node by name.
209 *
210 * @param string The node name
211 */
212 public function removeNode($name)
213 {
214 unset($this->nodes[$name]);
215 }
216
217 public function count()
218 {
219 return count($this->nodes);
220 }
221
222 public function getIterator()
223 {
224 return new ArrayIterator($this->nodes);
225 }
226}