1
|
|
|
<?php |
2
|
|
|
namespace PHPHtmlParser\Dom; |
3
|
|
|
|
4
|
|
|
use PHPHtmlParser\Dom; |
5
|
|
|
use stringEncode\Encode; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Tag |
9
|
|
|
* |
10
|
|
|
* @package PHPHtmlParser\Dom |
11
|
|
|
*/ |
12
|
|
|
class Tag |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name of the tag. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The attributes of the tag. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $attr = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Is this tag self closing. |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $selfClosing = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tag noise |
38
|
|
|
*/ |
39
|
|
|
protected $noise = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The encoding class to... encode the tags |
43
|
|
|
* |
44
|
|
|
* @var mixed |
45
|
|
|
*/ |
46
|
|
|
protected $encode = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets up the tag with a name. |
50
|
|
|
* |
51
|
|
|
* @param $name |
52
|
|
|
*/ |
53
|
|
|
public function __construct($name) |
54
|
|
|
{ |
55
|
|
|
$this->name = $name; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Magic method to get any of the attributes. |
60
|
|
|
* |
61
|
|
|
* @param string $key |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function __get($key) |
65
|
|
|
{ |
66
|
|
|
return $this->getAttribute($key); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Magic method to set any attribute. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @param mixed $value |
74
|
|
|
*/ |
75
|
|
|
public function __set($key, $value) |
76
|
|
|
{ |
77
|
|
|
$this->setAttribute($key, $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the name of this tag. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function name() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sets the tag to be self closing. |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function selfClosing() |
96
|
|
|
{ |
97
|
|
|
$this->selfClosing = true; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks if the tag is self closing. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isSelfClosing() |
108
|
|
|
{ |
109
|
|
|
return $this->selfClosing; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets the encoding type to be used. |
114
|
|
|
* |
115
|
|
|
* @param Encode $encode |
116
|
|
|
*/ |
117
|
|
|
public function setEncoding(Encode $encode) |
118
|
|
|
{ |
119
|
|
|
$this->encode = $encode; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sets the noise for this tag (if any) |
124
|
|
|
* |
125
|
|
|
* @param $noise |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function noise($noise) |
129
|
|
|
{ |
130
|
|
|
$this->noise = $noise; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set an attribute for this tag. |
137
|
|
|
* |
138
|
|
|
* @param string $key |
139
|
|
|
* @param string|array $value |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setAttribute($key, $value) |
143
|
|
|
{ |
144
|
|
|
$key = strtolower($key); |
145
|
|
|
if ( ! is_array($value)) { |
146
|
|
|
$value = [ |
147
|
|
|
'value' => $value, |
148
|
|
|
'doubleQuote' => true, |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
$this->attr[$key] = $value; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set inline style attribute value. |
158
|
|
|
* |
159
|
|
|
* @param $attr_key |
160
|
|
|
* @param $attr_value |
161
|
|
|
*/ |
162
|
|
|
public function setStyleAttributeValue($attr_key, $attr_value) |
163
|
|
|
{ |
164
|
|
|
|
165
|
|
|
$style_array = $this->getStyleAttributeArray(); |
166
|
|
|
$style_array[$attr_key] = $attr_value; |
167
|
|
|
|
168
|
|
|
$style_string = ''; |
169
|
|
|
foreach ($style_array as $key => $value) { |
170
|
|
|
$style_string .= $key . ':' . $value . ';'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->setAttribute('style', $style_string); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get style attribute in array |
178
|
|
|
* |
179
|
|
|
* @return array|null |
180
|
|
|
*/ |
181
|
|
|
public function getStyleAttributeArray() |
182
|
|
|
{ |
183
|
|
|
$value = $this->getAttribute('style'); |
184
|
|
|
|
185
|
|
|
if ($value === null) { |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$value = explode(';', substr(trim($value), 0, -1)); |
190
|
|
|
$result = []; |
191
|
|
|
foreach ($value as $attr) { |
192
|
|
|
$attr = explode(':', $attr); |
193
|
|
|
$result[$attr[0]] = $attr[1]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $result; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Removes an attribute from this tag. |
203
|
|
|
* |
204
|
|
|
* @param $key |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function removeAttribute($key) |
208
|
|
|
{ |
209
|
|
|
$key = strtolower($key); |
210
|
|
|
unset($this->attr[$key]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Removes all attributes on this tag. |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public function removeAllAttributes() |
219
|
|
|
{ |
220
|
|
|
$this->attr = []; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Sets the attributes for this tag |
225
|
|
|
* |
226
|
|
|
* @param array $attr |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
public function setAttributes(array $attr) |
230
|
|
|
{ |
231
|
|
|
foreach ($attr as $key => $value) { |
232
|
|
|
$this->setAttribute($key, $value); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns all attributes of this tag. |
240
|
|
|
* |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
|
|
public function getAttributes() |
244
|
|
|
{ |
245
|
|
|
$return = []; |
246
|
|
|
foreach ($this->attr as $attr => $info) { |
247
|
|
|
$return[$attr] = $this->getAttribute($attr); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $return; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns an attribute by the key |
255
|
|
|
* |
256
|
|
|
* @param string $key |
257
|
|
|
* @return mixed |
258
|
|
|
*/ |
259
|
|
|
public function getAttribute($key) |
260
|
|
|
{ |
261
|
|
|
if ( ! isset($this->attr[$key])) { |
262
|
|
|
return null; |
263
|
|
|
} |
264
|
|
|
$value = $this->attr[$key]['value']; |
265
|
|
|
if (is_string($value) && ! is_null($this->encode)) { |
266
|
|
|
// convert charset |
267
|
|
|
$this->attr[$key]['value'] = $this->encode->convert($value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $this->attr[$key]; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Generates the opening tag for this object. |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
public function makeOpeningTag() |
279
|
|
|
{ |
280
|
|
|
$return = '<'.$this->name; |
281
|
|
|
|
282
|
|
|
// add the attributes |
283
|
|
|
foreach ($this->attr as $key => $info) { |
284
|
|
|
$info = $this->getAttribute($key); |
285
|
|
|
$val = $info['value']; |
286
|
|
|
if (is_null($val)) { |
287
|
|
|
$return .= ' '.$key; |
288
|
|
|
} elseif ($info['doubleQuote']) { |
289
|
|
|
$return .= ' '.$key.'="'.$val.'"'; |
290
|
|
|
} else { |
291
|
|
|
$return .= ' '.$key.'=\''.$val.'\''; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if ($this->selfClosing) { |
296
|
|
|
return $return.' />'; |
297
|
|
|
} else { |
298
|
|
|
return $return.'>'; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Generates the closing tag for this object. |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function makeClosingTag() |
308
|
|
|
{ |
309
|
|
|
if ($this->selfClosing) { |
310
|
|
|
return ''; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return '</'.$this->name.'>'; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|