This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.php
282 lines (238 loc) · 5.69 KB
/
Node.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
namespace Flownode\UI\Common\DOM;
/**
* Base fluid DOM node builder
*
* @author lcallarec <l.callarec@gmail.com>
*/
class Node
{
/**
* Array of DOM attributes for this node
*
* <b>Exemple</b>
* <code>
* $attributes = array('class' => 'top', 'style' => array('float' => 'left'));
* </code>
*
* @var array
*/
protected $attributes;
/**
* String representation of $attributes var
* @see self::setAttributesString()
*
* @var string
*/
protected $sAttributes;
/**
* Node tag name
*
* @var string
*/
protected $tag;
/**
* Parent node. Null for root node
*
* @var Flownode\Common\DOM\Node
*/
protected $parent = null;
/**
* Array of child nodes
*
* @var array<i, Flownode\Common\DOM\Node>
*/
protected $child = array();
/**
* Node string represntation
*
* @var string
*/
protected $text = '';
/**
*
* @var string
*/
protected $template = '%content%';
/**
*
* @param string $tagName
* @param array $attributes
*/
public function __construct($tag = null, $attributes = array())
{
$this->tagName = $tag;
$this->setAttributes($attributes);
//In this case, it's plain text
if (null !== $tag) {
$this->template = '<' . $tag . '%attributes%>%content%</' . $tag . '>';
}
}
/**
* Open a new node inside the current node.
* The created node is added to child node stack.
*
* @return Flownode\Common\DOM\Node
*/
public function open($tag, $attributes = array())
{
$node = new self($tag, $attributes);
$this->addChild($node);
return $node;
}
/**
* Close the current node.
*
* @return Flownode\Common\DOM\Node
*/
public function close()
{
if (null !== $this->parent) {
return $this->parent;
}
return $this;
}
/**
* Set one or sevral node attributes
*
* @param array $attributes ['attribute' => 'value' [, 'other_attribute' => 'other_value']]
*
* @return Flownode\Common\DOM\Node
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
*
* @see Node::setAttribute
*
* @return Flownode\Common\DOM\Node
*/
public function set($attribute, $value)
{
return $this->setAttribute($attribute, $value);
}
/**
* Set a single node attribute
*
* @param string $attribute
* @param string $value
* @return Flownode\Common\DOM\Node
*/
public function setAttribute($attribute, $value)
{
$this->attributes[$attribute] = $value;
return $this;
}
/**
* get the parent node of the current node
*
* @return Flownode\Commin\DOM\Node
*/
public function getParent()
{
return $this->parent;
}
/**
* Set the parent node of the current node
*
* @param Flownode\Commin\DOM\Node $node
*
* @return Flownode\Commin\DOM\Node
*/
public function setParent(Node $node)
{
$this->parent = $node;
return $this;
}
/**
* Add a child to the current node
*
* @param Flownode\Common\DOM\Node $$node
* @return Flownode\Common\DOM\Node
*/
public function addChild(Node $node)
{
$node->setParent($this);
$this->child[] = $node;
return $this;
}
/**
*
* @return string
*/
public function getText()
{
foreach ($this->child as $child) {
$this->text .= $child->getText();
}
$this->sAttributes = $this->getAttributesString($this->attributes);
return strtr($this->template, array('%attributes%' => $this->sAttributes, '%content%' => $this->text));
}
/**
*
* @return type
*/
public function setText($text = '')
{
$this->text .= $text;
return $this;
}
/**
* Get formatted string from array of attributes
*
* @param array $attributes
* @param string $pattern
* @param string $valueSeparator
* @return string
*/
public function getAttributesString($attributes, $pattern = ' %attribute%="%value%"', $valueSeparator = '')
{
$string = '';
foreach ($attributes as $attribute => $value) {
if (true === is_array($value)) {
$string .= strtr($pattern, array('%attribute%' => $attribute, '%value%' => $this->getAttributesString($value, '%attribute%: %value%;')));
} else {
$string .= strtr($pattern, array('%attribute%' => $attribute, '%value%' => $value));
}
}
return $string;
}
public function setTemplate($template = '%attributes% : %content%')
{
$this->template = $template;
return $this;
}
/**
* The lazy way to set an element attributes
*
* <code>
* $node = new Element('a');
* $node->href('www.google.com')->class('button')->close();
*
* @param type $name
* @param type $arguments
* @return Element
*/
public function __call($name, $arguments)
{
if(empty($arguments))
{
$arguments[0] = '';
}
return $this->setAttribute($name, $arguments[0]);
}
/**
* Used to set a new tag name after object initialization
*
* @param string $tag
* @return \Flownode\UI\Common\DOM\Node
*/
public function setTagName($tag)
{
$this->tag = $tag;
return $this;
}
}