1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Drone\Dom\Element; |
12
|
|
|
|
13
|
|
|
use Drone\Dom\Attribute; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AbstractElement class |
17
|
|
|
* |
18
|
|
|
* To represent an abstract html element |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractElement |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The node name of the element |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const NODE_NAME = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tells if the element has a end tag |
31
|
|
|
* |
32
|
|
|
* @var boolean |
33
|
|
|
*/ |
34
|
|
|
const HAS_END_TAG = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Start tag of the element |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $startTag; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* End tag of the element |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $endTag; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Element attributes list |
52
|
|
|
* |
53
|
|
|
* @var Attribute[] |
54
|
|
|
*/ |
55
|
|
|
protected $attributes = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Children elements |
59
|
|
|
* |
60
|
|
|
* @var AbstractElement[] |
61
|
|
|
*/ |
62
|
|
|
protected $children = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the startTag attribute |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
3 |
|
public function getStartTag() |
70
|
|
|
{ |
71
|
3 |
|
return $this->startTag; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the endTag attribute |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
3 |
|
public function getEndTag() |
80
|
|
|
{ |
81
|
3 |
|
return $this->endTag; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets all attributes of the element |
86
|
|
|
* |
87
|
|
|
* @return Attribute[] |
88
|
|
|
*/ |
89
|
7 |
|
public function getAttributes() |
90
|
|
|
{ |
91
|
7 |
|
return $this->attributes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets all children elements |
96
|
|
|
* |
97
|
|
|
* @return AbstractElement[] |
98
|
|
|
*/ |
99
|
7 |
|
public function getChildren() |
100
|
|
|
{ |
101
|
7 |
|
return $this->children; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Checks if a particula child exists by label |
106
|
|
|
* |
107
|
|
|
* @param string $label |
108
|
|
|
* |
109
|
|
|
* @return boolean |
110
|
|
|
*/ |
111
|
1 |
|
public function hasChild($label) |
112
|
|
|
{ |
113
|
1 |
|
if (array_key_exists($label, $this->children)) { |
114
|
1 |
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a particular child |
122
|
|
|
* |
123
|
|
|
* @param string $label |
124
|
|
|
* |
125
|
|
|
* @return AbstractElement|null |
126
|
|
|
*/ |
127
|
7 |
|
public function getChild($label) |
128
|
|
|
{ |
129
|
7 |
|
if (array_key_exists($label, $this->children)) { |
130
|
7 |
|
return $this->children[$label]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets a child |
138
|
|
|
* |
139
|
|
|
* @param string $label |
140
|
|
|
* @param AbstractElement $child |
141
|
|
|
* |
142
|
|
|
* @return null |
143
|
|
|
*/ |
144
|
7 |
|
public function setChild($label, AbstractElement $child) |
145
|
|
|
{ |
146
|
7 |
|
$this->children[$label] = $child; |
147
|
7 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Removes a particular child |
151
|
|
|
* |
152
|
|
|
* @param string $label |
153
|
|
|
* |
154
|
|
|
* @throws Exception\ChildNotFoundException |
155
|
|
|
* |
156
|
|
|
* @return AbstractElement|null |
157
|
|
|
*/ |
158
|
|
|
public function removeChild($label) |
159
|
|
|
{ |
160
|
|
|
if (array_key_exists($label, $this->children)) { |
161
|
|
|
unset($this->children[$label]); |
162
|
|
|
} else { |
163
|
|
|
throw new Exception\ChildNotFoundException("The child to remove does not exists"); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Checks if the element has the specified attribute |
169
|
|
|
* |
170
|
|
|
* @param string $name |
171
|
|
|
* |
172
|
|
|
* @return boolean |
173
|
|
|
*/ |
174
|
2 |
|
public function hasAttribute($name) |
175
|
|
|
{ |
176
|
2 |
|
if (count($this->attributes)) { |
177
|
2 |
|
foreach ($this->attributes as $attrib) { |
178
|
2 |
|
if ($attrib->getName() == $name) { |
179
|
2 |
|
return true; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns a particular Attribute |
189
|
|
|
* |
190
|
|
|
* @param string $name |
191
|
|
|
* |
192
|
|
|
* @return Attribute|null |
193
|
|
|
*/ |
194
|
8 |
|
public function getAttribute($name) |
195
|
|
|
{ |
196
|
8 |
|
if (count($this->attributes)) { |
197
|
8 |
|
foreach ($this->attributes as $attrib) { |
198
|
8 |
|
if ($attrib->getName() == $name) { |
199
|
8 |
|
return $attrib; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
8 |
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Sets an attribute |
209
|
|
|
* |
210
|
|
|
* @param Attribute $attribute |
211
|
|
|
* |
212
|
|
|
* @return null |
213
|
|
|
*/ |
214
|
8 |
|
public function setAttribute(Attribute $attribute) |
215
|
|
|
{ |
216
|
8 |
|
$attrib = $this->getAttribute($attribute->getName()); |
217
|
|
|
|
218
|
8 |
|
if (!is_null($attrib)) { |
219
|
|
|
foreach ($this->attributes as $key => $_attrib) { |
220
|
|
|
if ($_attrib->getName() == $attrib->getName()) { |
221
|
|
|
$this->attributes[$key] = $attribute; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} else { |
225
|
8 |
|
$this->attributes[] = $attribute; |
226
|
|
|
} |
227
|
8 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Removes a particular Attribute |
231
|
|
|
* |
232
|
|
|
* @param string $name |
233
|
|
|
* |
234
|
|
|
* @throws Exception\AttributeNotFoundException |
235
|
|
|
* |
236
|
|
|
* @return null |
237
|
|
|
*/ |
238
|
|
|
public function removeAttribute($name) |
239
|
|
|
{ |
240
|
|
|
if (count($this->attributes)) { |
241
|
|
|
foreach ($this->attributes as $key => $attrib) { |
242
|
|
|
if ($attrib->getName() == $name) { |
243
|
|
|
unset($this->attributes[$key]); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
throw new Exception\AttributeNotFoundException("The attribute to remove does not exists"); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Constructor |
253
|
|
|
* |
254
|
|
|
* @param array $options |
255
|
|
|
*/ |
256
|
10 |
|
public function __construct() |
257
|
|
|
{ |
258
|
10 |
|
if (static::HAS_END_TAG) { |
259
|
8 |
|
$this->startTag = "<" .strtolower(static::NODE_NAME). ">"; |
260
|
8 |
|
$this->endTag = "</" .strtolower(static::NODE_NAME). ">"; |
261
|
|
|
} else { |
262
|
9 |
|
$this->startTag = "<" .strtolower(static::NODE_NAME); |
263
|
9 |
|
$this->endTag = "/>"; |
264
|
|
|
} |
265
|
10 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Checks is an element is a form control |
269
|
|
|
* |
270
|
|
|
* @return boolean |
271
|
|
|
*/ |
272
|
5 |
|
public function isFormControl() |
273
|
|
|
{ |
274
|
5 |
|
if (in_array(static::NODE_NAME, ['INPUT'])) { |
275
|
5 |
|
return true; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return false; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|