-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
Copy pathTag.php
375 lines (323 loc) · 7.98 KB
/
Tag.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
declare(strict_types=1);
namespace PHPHtmlParser\Dom;
use PHPHtmlParser\DTO\Tag\AttributeDTO;
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
use stringEncode\Encode;
/**
* Class Tag.
*/
class Tag
{
/**
* The name of the tag.
*
* @var string
*/
protected $name;
/**
* The attributes of the tag.
*
* @var AttributeDTO[]
*/
protected $attr = [];
/**
* Is this tag self closing.
*
* @var bool
*/
protected $selfClosing = false;
/**
* If self-closing, will this use a trailing slash. />.
*
* @var bool
*/
protected $trailingSlash = true;
/**
* Tag noise.
*/
protected $noise = '';
/**
* The encoding class to... encode the tags.
*
* @var Encode|null
*/
protected $encode;
/**
* @var bool
*/
private $HtmlSpecialCharsDecode = false;
/**
* What the opening of this tag will be.
*
* @var string
*/
private $opening = '<';
/**
* What the closing tag for self-closing elements should be.
*
* @var string
*/
private $closing = ' />';
/**
* Sets up the tag with a name.
*
* @param $name
*/
public function __construct(string $name)
{
$this->name = $name;
}
/**
* Returns the name of this tag.
*/
public function name(): string
{
return $this->name;
}
/**
* Sets the name of this tag.
*/
public function setName(string $name): Tag
{
$this->name = $name;
return clone $this;
}
/**
* Sets the tag to be self closing.
*/
public function selfClosing(): Tag
{
$this->selfClosing = true;
return clone $this;
}
public function setOpening(string $opening): Tag
{
$this->opening = $opening;
return clone $this;
}
public function setClosing(string $closing): Tag
{
$this->closing = $closing;
return clone $this;
}
/**
* Sets the tag to not use a trailing slash.
*/
public function noTrailingSlash(): Tag
{
$this->trailingSlash = false;
return clone $this;
}
/**
* Checks if the tag is self closing.
*/
public function isSelfClosing(): bool
{
return $this->selfClosing;
}
/**
* Sets the encoding type to be used.
*/
public function setEncoding(Encode $encode): void
{
$this->encode = $encode;
}
/**
* @param bool $htmlSpecialCharsDecode
*/
public function setHtmlSpecialCharsDecode($htmlSpecialCharsDecode = false): void
{
$this->HtmlSpecialCharsDecode = $htmlSpecialCharsDecode;
}
/**
* Sets the noise for this tag (if any).
*/
public function noise(string $noise): Tag
{
$this->noise = $noise;
return clone $this;
}
/**
* Set an attribute for this tag.
*/
public function setAttribute(string $key, ?string $attributeValue, bool $doubleQuote = true): Tag
{
$attributeDTO = AttributeDTO::makeFromPrimitives(
$attributeValue,
$doubleQuote
);
if ($this->HtmlSpecialCharsDecode) {
$attributeDTO->htmlspecialcharsDecode();
}
$this->attr[\strtolower($key)] = $attributeDTO;
return clone $this;
}
/**
* Set inline style attribute value.
*
* @param mixed $attr_key
* @param mixed $attr_value
*/
public function setStyleAttributeValue($attr_key, $attr_value): void
{
$style_array = $this->getStyleAttributeArray();
$style_array[$attr_key] = $attr_value;
$style_string = '';
foreach ($style_array as $key => $value) {
$style_string .= $key . ':' . $value . ';';
}
$this->setAttribute('style', $style_string);
}
/**
* Get style attribute in array.
*/
public function getStyleAttributeArray(): array
{
try {
$value = $this->getAttribute('style')->getValue();
if (\is_null($value)) {
return [];
}
$value = \explode(';', \substr(\trim($value), 0, -1));
$result = [];
foreach ($value as $attr) {
$attr = \explode(':', $attr);
$result[$attr[0]] = $attr[1];
}
return $result;
} catch (AttributeNotFoundException $e) {
unset($e);
return [];
}
}
/**
* Removes an attribute from this tag.
*
* @param mixed $key
*
* @return void
*/
public function removeAttribute($key)
{
$key = \strtolower($key);
unset($this->attr[$key]);
}
/**
* Removes all attributes on this tag.
*
* @return void
*/
public function removeAllAttributes()
{
$this->attr = [];
}
/**
* Sets the attributes for this tag.
*
* @return $this
*/
public function setAttributes(array $attr)
{
foreach ($attr as $key => $info) {
if (\is_array($info)) {
$this->setAttribute($key, $info['value'], $info['doubleQuote']);
} else {
$this->setAttribute($key, $info);
}
}
return $this;
}
/**
* Returns all attributes of this tag.
*
* @throws \stringEncode\Exception
*
* @return AttributeDTO[]
*/
public function getAttributes(): array
{
$return = [];
foreach (\array_keys($this->attr) as $attr) {
try {
$return[$attr] = $this->getAttribute($attr);
} catch (AttributeNotFoundException $e) {
// attribute that was in the array was not found in the array....
unset($e);
}
}
return $return;
}
/**
* Returns an attribute by the key.
*
* @throws AttributeNotFoundException
* @throws \stringEncode\Exception
*/
public function getAttribute(string $key): AttributeDTO
{
$key = \strtolower($key);
if (!isset($this->attr[$key])) {
throw new AttributeNotFoundException('Attribute with key "' . $key . '" not found.');
}
$attributeDTO = $this->attr[$key];
if (!\is_null($this->encode)) {
// convert charset
$attributeDTO->encodeValue($this->encode);
}
return $attributeDTO;
}
/**
* Returns TRUE if node has attribute.
*
* @return bool
*/
public function hasAttribute(string $key)
{
return isset($this->attr[$key]);
}
/**
* Generates the opening tag for this object.
*
* @return string
*/
public function makeOpeningTag()
{
$return = $this->opening . $this->name;
// add the attributes
foreach (\array_keys($this->attr) as $key) {
try {
$attributeDTO = $this->getAttribute($key);
} catch (AttributeNotFoundException $e) {
// attribute that was in the array not found in the array... let's continue.
continue;
} catch (\TypeError $e) {
$val = null;
}
$val = $attributeDTO->getValue();
if (\is_null($val)) {
$return .= ' ' . $key;
} elseif ($attributeDTO->isDoubleQuote()) {
$return .= ' ' . $key . '="' . $val . '"';
} else {
$return .= ' ' . $key . '=\'' . $val . '\'';
}
}
if ($this->selfClosing && $this->trailingSlash) {
return $return . $this->closing;
}
return $return . '>';
}
/**
* Generates the closing tag for this object.
*
* @return string
*/
public function makeClosingTag()
{
if ($this->selfClosing) {
return '';
}
return '</' . $this->name . '>';
}
}