PEEL Shopping
Open source ecommerce : PEEL Shopping
Node.php
Go to the documentation of this file.
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 
18 class Twig_Node implements Twig_NodeInterface
19 {
20  protected $nodes;
21  protected $attributes;
22  protected $lineno;
23  protected $tag;
24 
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 
126  public function hasAttribute($name)
127  {
128  return array_key_exists($name, $this->attributes);
129  }
130 
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 
153  public function setAttribute($name, $value)
154  {
155  $this->attributes[$name] = $value;
156  }
157 
163  public function removeAttribute($name)
164  {
165  unset($this->attributes[$name]);
166  }
167 
175  public function hasNode($name)
176  {
177  return array_key_exists($name, $this->nodes);
178  }
179 
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 
202  public function setNode($name, $node = null)
203  {
204  $this->nodes[$name] = $node;
205  }
206 
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 }
getLine()
Definition: Node.php:109
hasNode($name)
Returns true if the node with the given identifier exists.
Definition: Node.php:175
setNode($name, $node=null)
Sets a node.
Definition: Node.php:202
Compiles a node to PHP code.
Definition: Compiler.php:18
$attributes
Definition: Node.php:21
__toString()
Definition: Node.php:44
Represents a node in the AST.
Definition: Node.php:18
removeAttribute($name)
Removes an attribute.
Definition: Node.php:163
removeNode($name)
Removes a node by name.
Definition: Node.php:212
getNodeTag()
Definition: Node.php:114
getIterator()
Definition: Node.php:222
getAttribute($name)
Gets an attribute.
Definition: Node.php:138
$lineno
Definition: Node.php:22
hasAttribute($name)
Returns true if the attribute is defined.
Definition: Node.php:126
$nodes
Definition: Node.php:20
setAttribute($name, $value)
Sets an attribute.
Definition: Node.php:153
__construct(array $nodes=array(), array $attributes=array(), $lineno=0, $tag=null)
Constructor.
Definition: Node.php:36
Represents a node in the AST.
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: Node.php:102
toXml($asDom=false)
Definition: Node.php:72
count()
Definition: Node.php:217
getNode($name)
Gets a node by name.
Definition: Node.php:187

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:18 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.