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
|
|
|
* Removes an attribute from this tag. |
158
|
|
|
* |
159
|
|
|
* @param $key |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function removeAttribute($key) |
163
|
|
|
{ |
164
|
|
|
$key = strtolower($key); |
165
|
|
|
unset($this->attr[$key]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Removes all attributes on this tag. |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
public function removeAllAttributes() |
174
|
|
|
{ |
175
|
|
|
$this->attr = []; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sets the attributes for this tag |
180
|
|
|
* |
181
|
|
|
* @param array $attr |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function setAttributes(array $attr) |
185
|
|
|
{ |
186
|
|
|
foreach ($attr as $key => $value) { |
187
|
|
|
$this->setAttribute($key, $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns all attributes of this tag. |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function getAttributes() |
199
|
|
|
{ |
200
|
|
|
$return = []; |
201
|
|
|
foreach ($this->attr as $attr => $info) { |
202
|
|
|
$return[$attr] = $this->getAttribute($attr); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns an attribute by the key |
210
|
|
|
* |
211
|
|
|
* @param string $key |
212
|
|
|
* @return mixed |
213
|
|
|
*/ |
214
|
|
|
public function getAttribute($key) |
215
|
|
|
{ |
216
|
|
|
if ( ! isset($this->attr[$key])) { |
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
$value = $this->attr[$key]['value']; |
220
|
|
|
if (is_string($value) && ! is_null($this->encode)) { |
221
|
|
|
// convert charset |
222
|
|
|
$this->attr[$key]['value'] = $this->encode->convert($value); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this->attr[$key]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return style array |
230
|
|
|
* |
231
|
|
|
* @return null|array |
232
|
|
|
*/ |
233
|
|
|
public function getStyle() |
234
|
|
|
{ |
235
|
|
|
$style = trim($this->getAttribute('style')); |
236
|
|
|
|
237
|
|
|
if ($style) { |
238
|
|
|
$style_attr = explode(';', $style); |
239
|
|
|
if (!$style_attr) { |
|
|
|
|
240
|
|
|
return null; |
241
|
|
|
} |
242
|
|
|
foreach ($style_attr as $attr) { |
243
|
|
|
$attr = explode(':', $attr); |
244
|
|
|
$style_array[$attr[0]] = $attr[1]; |
|
|
|
|
245
|
|
|
} |
246
|
|
|
return $style_array; |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return null; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Generates the opening tag for this object. |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function makeOpeningTag() |
258
|
|
|
{ |
259
|
|
|
$return = '<'.$this->name; |
260
|
|
|
|
261
|
|
|
// add the attributes |
262
|
|
|
foreach ($this->attr as $key => $info) { |
263
|
|
|
$info = $this->getAttribute($key); |
264
|
|
|
$val = $info['value']; |
265
|
|
|
if (is_null($val)) { |
266
|
|
|
$return .= ' '.$key; |
267
|
|
|
} elseif ($info['doubleQuote']) { |
268
|
|
|
$return .= ' '.$key.'="'.$val.'"'; |
269
|
|
|
} else { |
270
|
|
|
$return .= ' '.$key.'=\''.$val.'\''; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ($this->selfClosing) { |
275
|
|
|
return $return.' />'; |
276
|
|
|
} else { |
277
|
|
|
return $return.'>'; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Generates the closing tag for this object. |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function makeClosingTag() |
287
|
|
|
{ |
288
|
|
|
if ($this->selfClosing) { |
289
|
|
|
return ''; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return '</'.$this->name.'>'; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.