1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Html; |
3
|
|
|
|
4
|
|
|
use function array_diff; |
5
|
|
|
use function array_filter; |
6
|
|
|
use function array_key_exists; |
7
|
|
|
use function array_map; |
8
|
|
|
use function array_merge; |
9
|
|
|
use function array_search; |
10
|
|
|
use function array_splice; |
11
|
|
|
use function array_unique; |
12
|
|
|
use function explode; |
13
|
|
|
use function implode; |
14
|
|
|
use function in_array; |
15
|
|
|
use function is_array; |
16
|
|
|
use function strip_tags; |
17
|
|
|
use function strlen; |
18
|
|
|
use function strtolower; |
19
|
|
|
use function trim; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* children class to create a element |
23
|
|
|
* |
24
|
|
|
* @since 2.6.0 |
25
|
|
|
* |
26
|
|
|
* @package Redaxscript |
27
|
|
|
* @category Html |
28
|
|
|
* @author Henry Ruhs |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class Element extends HtmlAbstract |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* tag of the element |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
protected $_tag; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* array of singleton tags |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
protected $_singletonTags = |
48
|
|
|
[ |
49
|
|
|
'area', |
50
|
|
|
'base', |
51
|
|
|
'br', |
52
|
|
|
'col', |
53
|
|
|
'command', |
54
|
|
|
'embed', |
55
|
|
|
'hr', |
56
|
|
|
'img', |
57
|
|
|
'input', |
58
|
|
|
'keygen', |
59
|
|
|
'link', |
60
|
|
|
'meta', |
61
|
|
|
'param', |
62
|
|
|
'source', |
63
|
|
|
'track', |
64
|
|
|
'wbr', |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* attributes of the element |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
|
73
|
|
|
protected $_attributeArray = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* stringify the element |
77
|
|
|
* |
78
|
|
|
* @since 2.2.0 |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
11 |
|
*/ |
82
|
|
|
|
83
|
11 |
|
public function __toString() : string |
84
|
|
|
{ |
85
|
|
|
return $this->render(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* init the class |
90
|
|
|
* |
91
|
|
|
* @since 2.2.0 |
92
|
|
|
* |
93
|
|
|
* @param string $tag tag of the element |
94
|
|
|
* @param array $attributeArray attributes of the element |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
18 |
|
*/ |
98
|
|
|
|
99
|
18 |
|
public function init(string $tag = null, array $attributeArray = []) : self |
100
|
|
|
{ |
101
|
|
|
$this->_tag = strtolower($tag); |
102
|
|
|
|
103
|
18 |
|
/* process attributes */ |
104
|
|
|
|
105
|
18 |
|
if (is_array($attributeArray)) |
106
|
|
|
{ |
107
|
18 |
|
$this->attr($attributeArray); |
108
|
|
|
} |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* copy the element |
114
|
|
|
* |
115
|
|
|
* @since 2.2.0 |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
1 |
|
*/ |
119
|
|
|
|
120
|
1 |
|
public function copy() : self |
121
|
|
|
{ |
122
|
|
|
return clone $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* set the attribute to element |
127
|
|
|
* |
128
|
|
|
* @since 2.2.0 |
129
|
|
|
* |
130
|
|
|
* @param string|array $attribute key or array of attributes |
131
|
|
|
* @param string $value value of the attribute |
132
|
|
|
* |
133
|
|
|
* @return self |
134
|
18 |
|
*/ |
135
|
|
|
|
136
|
18 |
|
public function attr($attribute = null, string $value = null) : self |
137
|
|
|
{ |
138
|
18 |
|
if (is_array($attribute)) |
139
|
|
|
{ |
140
|
4 |
|
$this->_attributeArray = array_merge($this->_attributeArray, array_map('trim', $attribute)); |
141
|
|
|
} |
142
|
3 |
|
else if (strlen($attribute) && strlen($value)) |
143
|
|
|
{ |
144
|
18 |
|
$this->_attributeArray[$attribute] = trim($value); |
145
|
|
|
} |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* remove the attribute from element |
151
|
|
|
* |
152
|
|
|
* @since 2.2.0 |
153
|
|
|
* |
154
|
|
|
* @param string $attribute name of attributes |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
3 |
|
*/ |
158
|
|
|
|
159
|
3 |
|
public function removeAttr(string $attribute = null) : self |
160
|
|
|
{ |
161
|
1 |
|
if (is_array($this->_attributeArray) && array_key_exists($attribute, $this->_attributeArray)) |
162
|
|
|
{ |
163
|
3 |
|
array_splice($this->_attributeArray, array_search($attribute, $this->_attributeArray ), 1); |
164
|
|
|
} |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* add the class to element |
170
|
|
|
* |
171
|
|
|
* @since 2.2.0 |
172
|
|
|
* |
173
|
|
|
* @param string $className name of the classes |
174
|
|
|
* |
175
|
|
|
* @return self |
176
|
3 |
|
*/ |
177
|
|
|
|
178
|
3 |
|
public function addClass(string $className = null) : self |
179
|
3 |
|
{ |
180
|
|
|
$this->_editClass($className, 'add'); |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* remove the class from element |
186
|
|
|
* |
187
|
|
|
* @since 2.2.0 |
188
|
|
|
* |
189
|
|
|
* @param string $className name of the classes |
190
|
|
|
* |
191
|
|
|
* @return self |
192
|
3 |
|
*/ |
193
|
|
|
|
194
|
3 |
|
public function removeClass(string $className = null) : self |
195
|
3 |
|
{ |
196
|
|
|
$this->_editClass($className, 'remove'); |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* edit class helper |
202
|
|
|
* |
203
|
|
|
* @since 2.2.0 |
204
|
|
|
* |
205
|
|
|
* @param string $className name of the classes |
206
|
|
|
* @param string $type add or remove |
207
|
3 |
|
*/ |
208
|
|
|
|
209
|
3 |
|
protected function _editClass(string $className = null, string $type = null) : void |
210
|
3 |
|
{ |
211
|
|
|
$classArray = array_filter(explode(' ', $className)); |
212
|
3 |
|
if (is_array($this->_attributeArray) && array_key_exists('class', $this->_attributeArray)) |
213
|
|
|
{ |
214
|
|
|
$attributeClassArray = array_filter(explode(' ', $this->_attributeArray['class'])); |
215
|
|
|
} |
216
|
3 |
|
else |
217
|
|
|
{ |
218
|
|
|
$attributeClassArray = []; |
219
|
|
|
} |
220
|
|
|
|
221
|
3 |
|
/* add or remove */ |
222
|
|
|
|
223
|
3 |
|
if (is_array($attributeClassArray) && is_array($classArray)) |
224
|
|
|
{ |
225
|
3 |
|
if ($type === 'add') |
226
|
|
|
{ |
227
|
3 |
|
$attributeClassArray = array_merge($attributeClassArray, $classArray); |
228
|
|
|
} |
229
|
3 |
|
else if ($type === 'remove') |
230
|
|
|
{ |
231
|
3 |
|
$attributeClassArray = array_diff($attributeClassArray, $classArray); |
232
|
|
|
} |
233
|
3 |
|
$this->_attributeArray['class'] = implode(' ', array_unique($attributeClassArray)); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* set the value to element |
239
|
|
|
* |
240
|
|
|
* @since 2.2.0 |
241
|
|
|
* |
242
|
|
|
* @param string|int $value value of the element |
243
|
|
|
* |
244
|
|
|
* @return self |
245
|
3 |
|
*/ |
246
|
|
|
|
247
|
3 |
|
public function val($value = null) : self |
248
|
3 |
|
{ |
249
|
|
|
$this->_attributeArray['value'] = trim($value); |
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* set the text to element |
255
|
|
|
* |
256
|
|
|
* @since 4.0.0 |
257
|
|
|
* |
258
|
|
|
* @param string|int $text text of the element |
259
|
|
|
* |
260
|
|
|
* @return self |
261
|
4 |
|
*/ |
262
|
|
|
|
263
|
4 |
|
public function text($text = null) : self |
264
|
4 |
|
{ |
265
|
|
|
$this->_html = strip_tags($text); |
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* render the element |
271
|
|
|
* |
272
|
|
|
* @since 2.2.0 |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
17 |
|
*/ |
276
|
|
|
|
277
|
17 |
|
public function render() : string |
278
|
|
|
{ |
279
|
|
|
$output = '<' . $this->_tag; |
280
|
|
|
|
281
|
17 |
|
/* process attributes */ |
282
|
|
|
|
283
|
10 |
|
foreach ($this->_attributeArray as $attribute => $value) |
284
|
|
|
{ |
285
|
6 |
|
if (strlen($attribute) && strlen($value)) |
286
|
|
|
{ |
287
|
|
|
$output .= ' ' . $attribute . '="' . $value . '"'; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
17 |
|
/* singleton tag */ |
292
|
|
|
|
293
|
5 |
|
if (in_array($this->_tag, $this->_singletonTags)) |
294
|
|
|
{ |
295
|
|
|
$output .= ' />'; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/* else normal tag */ |
299
|
|
|
|
300
|
12 |
|
else |
301
|
|
|
{ |
302
|
17 |
|
$output .= '>' . $this->_html . '</' . $this->_tag . '>'; |
303
|
|
|
} |
304
|
|
|
return $output; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|