|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace voku\helper; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractSimpleHtmlDom |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected static $functionAliases = [ |
|
13
|
|
|
'children' => 'childNodes', |
|
14
|
|
|
'first_child' => 'firstChild', |
|
15
|
|
|
'last_child' => 'lastChild', |
|
16
|
|
|
'next_sibling' => 'nextSibling', |
|
17
|
|
|
'prev_sibling' => 'previousSibling', |
|
18
|
|
|
'parent' => 'parentNode', |
|
19
|
|
|
'outertext' => 'html', |
|
20
|
|
|
'outerhtml' => 'html', |
|
21
|
|
|
'innertext' => 'innerHtml', |
|
22
|
|
|
'innerhtml' => 'innerHtml', |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \DOMElement|\DOMNode|null |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $node; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SimpleHtmlAttributes|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private $classListCache; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $name |
|
37
|
|
|
* @param array $arguments |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \BadMethodCallException |
|
40
|
|
|
* |
|
41
|
|
|
* @return SimpleHtmlDomInterface|string|null |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __call($name, $arguments) |
|
44
|
|
|
{ |
|
45
|
|
|
$name = \strtolower($name); |
|
46
|
|
|
|
|
47
|
|
|
if (isset(self::$functionAliases[$name])) { |
|
48
|
|
|
return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new \BadMethodCallException('Method does not exist'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* |
|
57
|
|
|
* @return SimpleHtmlAttributes|string|string[]|null |
|
58
|
|
|
*/ |
|
59
|
81 |
|
public function __get($name) |
|
60
|
|
|
{ |
|
61
|
81 |
|
$nameOrig = $name; |
|
62
|
81 |
|
$name = \strtolower($name); |
|
63
|
|
|
|
|
64
|
81 |
|
switch ($name) { |
|
65
|
81 |
|
case 'outerhtml': |
|
66
|
73 |
|
case 'outertext': |
|
67
|
63 |
|
case 'html': |
|
68
|
28 |
|
return $this->html(); |
|
69
|
63 |
|
case 'innerhtml': |
|
70
|
51 |
|
case 'innertext': |
|
71
|
20 |
|
return $this->innerHtml(); |
|
72
|
46 |
|
case 'text': |
|
73
|
41 |
|
case 'plaintext': |
|
74
|
21 |
|
return $this->text(); |
|
75
|
29 |
|
case 'tag': |
|
76
|
5 |
|
return $this->node ? $this->node->nodeName : ''; |
|
77
|
27 |
|
case 'attr': |
|
78
|
|
|
return $this->getAllAttributes(); |
|
79
|
27 |
|
case 'classlist': |
|
80
|
12 |
|
if ($this->classListCache === null) { |
|
81
|
12 |
|
$this->classListCache = new SimpleHtmlAttributes($this->node ?? null, 'class'); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
12 |
|
return $this->classListCache; |
|
85
|
|
|
default: |
|
86
|
15 |
|
if ($this->node && \property_exists($this->node, $nameOrig)) { |
|
87
|
2 |
|
if (\is_string($this->node->{$nameOrig})) { |
|
88
|
2 |
|
return HtmlDomParser::putReplacedBackToPreserveHtmlEntities($this->node->{$nameOrig}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->node->{$nameOrig}; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
15 |
|
return $this->getAttribute($name); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $selector |
|
100
|
|
|
* @param int $idx |
|
101
|
|
|
* |
|
102
|
|
|
* @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
|
|
103
|
|
|
*/ |
|
104
|
12 |
|
public function __invoke($selector, $idx = null) |
|
105
|
|
|
{ |
|
106
|
12 |
|
return $this->find($selector, $idx); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
1 |
View Code Duplication |
public function __isset($name) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
1 |
|
$nameOrig = $name; |
|
117
|
1 |
|
$name = \strtolower($name); |
|
118
|
|
|
|
|
119
|
1 |
|
switch ($name) { |
|
120
|
1 |
|
case 'outertext': |
|
121
|
1 |
|
case 'outerhtml': |
|
122
|
1 |
|
case 'innertext': |
|
123
|
1 |
|
case 'innerhtml': |
|
124
|
1 |
|
case 'plaintext': |
|
125
|
1 |
|
case 'text': |
|
126
|
1 |
|
case 'tag': |
|
127
|
|
|
return true; |
|
128
|
|
|
default: |
|
129
|
1 |
|
if ($this->node && \property_exists($this->node, $nameOrig)) { |
|
130
|
|
|
return isset($this->node->{$nameOrig}); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
return $this->hasAttribute($name); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $name |
|
139
|
|
|
* @param mixed $value |
|
140
|
|
|
* |
|
141
|
|
|
* @return SimpleHtmlDomInterface|null |
|
142
|
|
|
*/ |
|
143
|
24 |
|
public function __set($name, $value) |
|
144
|
|
|
{ |
|
145
|
24 |
|
$nameOrig = $name; |
|
146
|
24 |
|
$name = \strtolower($name); |
|
147
|
|
|
|
|
148
|
24 |
|
switch ($name) { |
|
149
|
24 |
|
case 'outerhtml': |
|
150
|
20 |
|
case 'outertext': |
|
151
|
6 |
|
return $this->replaceNodeWithString($value); |
|
152
|
18 |
|
case 'innertext': |
|
153
|
15 |
|
case 'innerhtml': |
|
154
|
9 |
|
return $this->replaceChildWithString($value); |
|
155
|
13 |
|
case 'plaintext': |
|
156
|
1 |
|
return $this->replaceTextWithString($value); |
|
157
|
12 |
|
case 'classlist': |
|
158
|
1 |
|
$name = 'class'; |
|
159
|
1 |
|
$nameOrig = 'class'; |
|
160
|
|
|
// no break |
|
161
|
|
View Code Duplication |
default: |
|
|
|
|
|
|
162
|
12 |
|
if ($this->node && \property_exists($this->node, $nameOrig)) { |
|
163
|
|
|
return $this->node->{$nameOrig} = $value; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
12 |
|
return $this->setAttribute($name, $value); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
3 |
|
public function __toString() |
|
174
|
|
|
{ |
|
175
|
3 |
|
return $this->html(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param string $name |
|
180
|
|
|
* |
|
181
|
|
|
* @return void |
|
182
|
|
|
*/ |
|
183
|
|
|
public function __unset($name) |
|
184
|
|
|
{ |
|
185
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
|
186
|
|
|
$this->removeAttribute($name); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param string $selector |
|
191
|
|
|
* @param int|null $idx |
|
192
|
|
|
* |
|
193
|
|
|
* @return mixed |
|
194
|
|
|
*/ |
|
195
|
|
|
abstract public function find(string $selector, $idx = null); |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return string[]|null |
|
199
|
|
|
*/ |
|
200
|
|
|
abstract public function getAllAttributes(); |
|
201
|
|
|
|
|
202
|
|
|
abstract public function getAttribute(string $name): string; |
|
203
|
|
|
|
|
204
|
|
|
abstract public function hasAttribute(string $name): bool; |
|
205
|
|
|
|
|
206
|
|
|
abstract public function html(bool $multiDecodeNewHtmlEntity = false): string; |
|
207
|
|
|
|
|
208
|
|
|
abstract public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string; |
|
209
|
|
|
|
|
210
|
|
|
abstract public function removeAttribute(string $name): SimpleHtmlDomInterface; |
|
211
|
|
|
|
|
212
|
|
|
abstract protected function replaceChildWithString(string $string): SimpleHtmlDomInterface; |
|
213
|
|
|
|
|
214
|
|
|
abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface; |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param string $string |
|
218
|
|
|
* |
|
219
|
|
|
* @return SimpleHtmlDomInterface |
|
220
|
|
|
*/ |
|
221
|
|
|
abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface; |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $name |
|
225
|
|
|
* @param string|null $value |
|
226
|
|
|
* @param bool $strict |
|
227
|
|
|
* |
|
228
|
|
|
* @return SimpleHtmlDomInterface |
|
229
|
|
|
*/ |
|
230
|
|
|
abstract public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface; |
|
231
|
|
|
|
|
232
|
|
|
abstract public function text(): string; |
|
233
|
|
|
} |
|
234
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.