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
|
|
|
public function getStartTag() |
70
|
|
|
{ |
71
|
|
|
return $this->startTag; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the endTag attribute |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getEndTag() |
80
|
|
|
{ |
81
|
|
|
return $this->endTag; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets all attributes of the element |
86
|
|
|
* |
87
|
|
|
* @return Attribute[] |
88
|
|
|
*/ |
89
|
|
|
public function getAttributes() |
90
|
|
|
{ |
91
|
|
|
return $this->attributes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets all children elements |
96
|
|
|
* |
97
|
|
|
* @return AbstractElement[] |
98
|
|
|
*/ |
99
|
|
|
public function getChildren() |
100
|
|
|
{ |
101
|
|
|
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
|
|
|
public function hasChild($label) |
112
|
|
|
{ |
113
|
|
|
if (array_key_exists($label, $this->children)) |
114
|
|
|
return true; |
115
|
|
|
|
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns a particular child |
121
|
|
|
* |
122
|
|
|
* @param string $label |
123
|
|
|
* |
124
|
|
|
* @return AbstractElement|null |
125
|
|
|
*/ |
126
|
|
|
public function getChild($label) |
127
|
|
|
{ |
128
|
|
|
if (array_key_exists($label, $this->children)) |
129
|
|
|
return $this->children[$label]; |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Sets a child |
136
|
|
|
* |
137
|
|
|
* @param string $label |
138
|
|
|
* @param AbstractElement $child |
139
|
|
|
* |
140
|
|
|
* @return null |
141
|
|
|
*/ |
142
|
|
|
public function setChild($label, AbstractElement $child) |
143
|
|
|
{ |
144
|
|
|
$this->children[$label] = $child; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Removes a particular child |
149
|
|
|
* |
150
|
|
|
* @param string $label |
151
|
|
|
* |
152
|
|
|
* @throws Exception\ChildNotFoundException |
153
|
|
|
* |
154
|
|
|
* @return AbstractElement|null |
155
|
|
|
*/ |
156
|
|
|
public function removeChild($label) |
157
|
|
|
{ |
158
|
|
|
if (array_key_exists($label, $this->children)) |
159
|
|
|
unset($this->children[$label]); |
160
|
|
|
else |
161
|
|
|
throw new Exception\ChildNotFoundException("The child to remove does not exists"); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Checks if the element has the specified attribute |
166
|
|
|
* |
167
|
|
|
* @param string $name |
168
|
|
|
* |
169
|
|
|
* @return boolean |
170
|
|
|
*/ |
171
|
|
|
public function hasAttribute($name) |
172
|
|
|
{ |
173
|
|
|
if (count($this->attributes)) |
174
|
|
|
{ |
175
|
|
|
foreach ($this->attributes as $attrib) |
176
|
|
|
{ |
177
|
|
|
if ($attrib->getName() == $name) |
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns a particular Attribute |
187
|
|
|
* |
188
|
|
|
* @param string $name |
189
|
|
|
* |
190
|
|
|
* @return Attribute|null |
191
|
|
|
*/ |
192
|
|
|
public function getAttribute($name) |
193
|
|
|
{ |
194
|
|
|
if (count($this->attributes)) |
195
|
|
|
{ |
196
|
|
|
foreach ($this->attributes as $attrib) |
197
|
|
|
{ |
198
|
|
|
if ($attrib->getName() == $name) |
199
|
|
|
return $attrib; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Sets an attribute |
208
|
|
|
* |
209
|
|
|
* @param Attribute $attribute |
210
|
|
|
* |
211
|
|
|
* @return null |
212
|
|
|
*/ |
213
|
|
|
public function setAttribute(Attribute $attribute) |
214
|
|
|
{ |
215
|
|
|
$attrib = $this->getAttribute($attribute->getName()); |
216
|
|
|
|
217
|
|
|
if (!is_null($attrib)) |
218
|
|
|
{ |
219
|
|
|
foreach ($this->attributes as $key => $_attrib) |
220
|
|
|
{ |
221
|
|
|
if ($_attrib->getName() == $attrib->getName()) |
222
|
|
|
$this->attributes[$key] = $attribute; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
else |
226
|
|
|
$this->attributes[] = $attribute; |
227
|
|
|
} |
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
|
|
|
{ |
242
|
|
|
foreach ($this->attributes as $key => $attrib) |
243
|
|
|
{ |
244
|
|
|
if ($attrib->getName() == $name) |
245
|
|
|
unset($this->attributes[$key]); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
throw new Exception\AttributeNotFoundException("The attribute to remove does not exists"); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Constructor |
254
|
|
|
* |
255
|
|
|
* @param array $options |
256
|
|
|
*/ |
257
|
|
|
public function __construct() |
258
|
|
|
{ |
259
|
|
|
if (static::HAS_END_TAG) |
260
|
|
|
{ |
261
|
|
|
$this->startTag = "<" .strtolower(static::NODE_NAME). ">"; |
262
|
|
|
$this->endTag = "</" .strtolower(static::NODE_NAME). ">"; |
263
|
|
|
} |
264
|
|
|
else |
265
|
|
|
{ |
266
|
|
|
$this->startTag = "<" .strtolower(static::NODE_NAME); |
267
|
|
|
$this->endTag = "/>"; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Checks is an element is a form control |
273
|
|
|
* |
274
|
|
|
* @return boolean |
275
|
|
|
*/ |
276
|
|
|
public function isFormControl() |
277
|
|
|
{ |
278
|
|
|
if (in_array(static::NODE_NAME, ['INPUT'])) |
279
|
|
|
return true; |
280
|
|
|
|
281
|
|
|
return false; |
282
|
|
|
} |
283
|
|
|
} |