17
17
class Markup implements ArrayAccess
18
18
{
19
19
/**
20
- * Specifies if attribute values and text input sould be protected from XSS injection
20
+ * Specifies if attribute values and text input sould be protected from XSS injection.
21
21
*/
22
22
public static bool $ avoidXSS = false ;
23
23
@@ -45,9 +45,9 @@ class Markup implements ArrayAccess
45
45
* @var array<int, string>
46
46
*/
47
47
protected array $ autocloseTagsList = [
48
- 'img ' , 'br ' , 'hr ' , 'input ' , 'area ' , 'link ' , 'meta ' , 'param ' , 'base ' , 'col ' , 'command ' , 'keygen ' , 'source '
48
+ 'img ' , 'br ' , 'hr ' , 'input ' , 'area ' , 'link ' , 'meta ' , 'param ' , 'base ' , 'col ' , 'command ' , 'keygen ' , 'source ' ,
49
49
];
50
-
50
+
51
51
/**
52
52
* @var array<mixed>
53
53
*/
@@ -56,23 +56,23 @@ class Markup implements ArrayAccess
56
56
protected function __construct (string $ tag , ?self $ top = null )
57
57
{
58
58
$ this ->tag = $ tag ;
59
- $ this ->top =& $ top ;
59
+ $ this ->top = & $ top ;
60
60
$ this ->attributeList = [];
61
61
$ this ->content = [];
62
62
$ this ->autoclosed = in_array ($ this ->tag , $ this ->autocloseTagsList );
63
63
$ this ->text = '' ;
64
64
}
65
65
66
66
/**
67
- * Alias for getParent()
67
+ * Alias for getParent().
68
68
*/
69
69
public function __invoke (): self
70
70
{
71
71
return $ this ->getParent ();
72
72
}
73
73
74
74
/**
75
- * Create a new Markup
75
+ * Create a new Markup.
76
76
*/
77
77
public static function createElement (string $ tag = '' ): self
78
78
{
@@ -83,8 +83,7 @@ public static function createElement(string $tag = ''): self
83
83
}
84
84
85
85
/**
86
- *
87
- * Add element at an existing Markup
86
+ * Add element at an existing Markup.
88
87
*/
89
88
public function addElement (self |string $ tag = '' ): self
90
89
{
@@ -105,6 +104,7 @@ public function addElement(self|string $tag = ''): self
105
104
106
105
/**
107
106
* (Re)Define an attribute or many attributes.
107
+ *
108
108
* @param string|array<mixed> $attribute
109
109
*/
110
110
public function set (string |array $ attribute , ?string $ value = null ): self
@@ -121,40 +121,41 @@ public function set(string|array $attribute, ?string $value = null): self
121
121
}
122
122
123
123
/**
124
- * alias to method "set"
124
+ * alias to method "set".
125
+ *
125
126
* @param string|array<mixed> $attribute
126
127
*/
127
128
public function attr ($ attribute , ?string $ value = null ): self
128
129
{
129
- return call_user_func_array (array ( $ this , 'set ' ) , func_get_args ());
130
+ return call_user_func_array ([ $ this , 'set ' ] , func_get_args ());
130
131
}
131
132
132
133
/**
133
- * Checks if an attribute is set for this tag and not null
134
+ * Checks if an attribute is set for this tag and not null.
134
135
*/
135
136
public function offsetExists (mixed $ offset ): bool
136
137
{
137
138
return isset ($ this ->attributeList [$ offset ]);
138
139
}
139
140
140
141
/**
141
- * Returns the value the attribute set for this tag
142
+ * Returns the value the attribute set for this tag.
142
143
*/
143
144
public function offsetGet (mixed $ offset ): mixed
144
145
{
145
146
return $ this ->offsetExists ($ offset ) ? $ this ->attributeList [$ offset ] : null ;
146
147
}
147
148
148
149
/**
149
- * Sets the value an attribute for this tag
150
+ * Sets the value an attribute for this tag.
150
151
*/
151
152
public function offsetSet (mixed $ offset , mixed $ value ): void
152
153
{
153
154
$ this ->attributeList [$ offset ] = $ value ;
154
155
}
155
156
156
157
/**
157
- * Removes an attribute
158
+ * Removes an attribute.
158
159
*/
159
160
public function offsetUnset (mixed $ offset ): void
160
161
{
@@ -174,24 +175,23 @@ public function text(?string $value): self
174
175
}
175
176
176
177
/**
177
- * Returns the top element
178
+ * Returns the top element.
178
179
*/
179
180
public function getTop (): self
180
181
{
181
182
return $ this ->top === null ? $ this : $ this ->top ;
182
183
}
183
184
184
185
/**
185
- *
186
- * Return parent of current element
186
+ * Return parent of current element.
187
187
*/
188
188
public function getParent (): ?self
189
189
{
190
190
return $ this ->parent ;
191
191
}
192
192
193
193
/**
194
- * Return first child of parent of current object
194
+ * Return first child of parent of current object.
195
195
*/
196
196
public function getFirst (): ?self
197
197
{
@@ -205,7 +205,7 @@ public function getPrevious(): ?static
205
205
{
206
206
$ prev = $ this ;
207
207
208
- if (! is_null ($ this ->parent )) {
208
+ if (!is_null ($ this ->parent )) {
209
209
foreach ($ this ->parent ->content as $ c ) {
210
210
if ($ c === $ this ) {
211
211
break ;
@@ -234,6 +234,7 @@ public function getNext(): ?self
234
234
}
235
235
}
236
236
}
237
+
237
238
return $ next ;
238
239
}
239
240
@@ -250,6 +251,7 @@ public function remove(): ?self
250
251
foreach ($ parent ->content as $ key => $ value ) {
251
252
if ($ parent ->content [$ key ] == $ this ) {
252
253
unset($ parent ->content [$ key ]);
254
+
253
255
return $ parent ;
254
256
}
255
257
}
@@ -259,27 +261,27 @@ public function remove(): ?self
259
261
}
260
262
261
263
/**
262
- * Generation method
264
+ * Generation method.
263
265
*/
264
266
public function __toString (): string
265
267
{
266
268
return $ this ->getTop ()->toString ();
267
269
}
268
270
269
271
/**
270
- * Generation method
272
+ * Generation method.
271
273
*/
272
274
public function toString (): string
273
275
{
274
276
$ string = '' ;
275
277
276
278
if (!empty ($ this ->tag )) {
277
- $ string .= '< ' . $ this ->tag ;
279
+ $ string .= '< ' . $ this ->tag ;
278
280
$ string .= $ this ->attributesToString ();
279
281
if ($ this ->autoclosed ) {
280
282
$ string .= '/> ' ;
281
283
} else {
282
- $ string .= '> ' . $ this ->contentToString () . '</ ' . $ this ->tag . '> ' ;
284
+ $ string .= '> ' . $ this ->contentToString (). '</ ' . $ this ->tag . '> ' ;
283
285
}
284
286
} else {
285
287
$ string .= $ this ->text ;
@@ -290,38 +292,39 @@ public function toString(): string
290
292
}
291
293
292
294
/**
293
- * Return current list of attribute as a string $key="$val" $key2="$val2"
295
+ * Return current list of attribute as a string $key="$val" $key2="$val2".
294
296
*/
295
297
protected function attributesToString (): string
296
298
{
297
299
$ string = '' ;
298
- $ XMLConvention = in_array (static ::$ outputLanguage , array ( ENT_XML1 , ENT_XHTML ) );
300
+ $ XMLConvention = in_array (static ::$ outputLanguage , [ ENT_XML1 , ENT_XHTML ] );
299
301
if (!empty ($ this ->attributeList )) {
300
302
foreach ($ this ->attributeList as $ key => $ value ) {
301
- if ($ value !==null && ($ value !==false || $ XMLConvention )) {
302
- $ string .= ' ' . $ key ;
303
- if ($ value ===true ) {
303
+ if ($ value !== null && ($ value !== false || $ XMLConvention )) {
304
+ $ string .= ' ' . $ key ;
305
+ if ($ value === true ) {
304
306
if ($ XMLConvention ) {
305
307
$ value = $ key ;
306
308
} else {
307
309
continue ;
308
310
}
309
311
}
310
- $ string .= '=" ' . implode (
312
+ $ string .= '=" ' . implode (
311
313
' ' ,
312
314
array_map (
313
315
static ::$ avoidXSS ? 'static::unXSS ' : 'strval ' ,
314
- is_array ($ value ) ? $ value : array ( $ value)
316
+ is_array ($ value ) ? $ value : [ $ value]
315
317
)
316
- ) . '" ' ;
318
+ ). '" ' ;
317
319
}
318
320
}
319
321
}
322
+
320
323
return $ string ;
321
324
}
322
325
323
326
/**
324
- * return current list of content as a string
327
+ * return current list of content as a string.
325
328
*/
326
329
protected function contentToString (): string
327
330
{
@@ -337,7 +340,7 @@ protected function contentToString(): string
337
340
}
338
341
339
342
/**
340
- * Protects value from XSS injection by replacing some characters by XML / HTML entities
343
+ * Protects value from XSS injection by replacing some characters by XML / HTML entities.
341
344
*/
342
345
public static function unXSS (string $ input ): string
343
346
{
0 commit comments