1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\helper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property-read string[] $outertext |
9
|
|
|
* <p>Get dom node's outer html.</p> |
10
|
|
|
* @property-read string[] $plaintext |
11
|
|
|
* <p>Get dom node's plain text.</p> |
12
|
|
|
*/ |
13
|
|
|
class SimpleHtmlDomNode extends \ArrayObject implements SimpleHtmlDomNodeInterface |
14
|
|
|
{ |
15
|
|
|
/** @noinspection MagicMethodsValidityInspection */ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $name |
19
|
|
|
* |
20
|
|
|
* @return array|null |
21
|
|
|
*/ |
22
|
4 |
|
public function __get($name) |
23
|
|
|
{ |
24
|
|
|
// init |
25
|
4 |
|
$name = \strtolower($name); |
26
|
|
|
|
27
|
4 |
|
if ($this->count() > 0) { |
28
|
3 |
|
$return = []; |
29
|
|
|
|
30
|
3 |
|
foreach ($this as $node) { |
31
|
3 |
|
if ($node instanceof SimpleHtmlDom) { |
32
|
3 |
|
$return[] = $node->{$name}; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
return $return; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
if ($name === 'plaintext' || $name === 'outertext') { |
40
|
1 |
|
return []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $selector |
48
|
|
|
* @param int $idx |
49
|
|
|
* |
50
|
|
|
* @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|null |
51
|
|
|
*/ |
52
|
|
|
public function __invoke($selector, $idx = null) |
53
|
|
|
{ |
54
|
|
|
return $this->find($selector, $idx); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
3 |
|
public function __toString() |
61
|
|
|
{ |
62
|
|
|
// init |
63
|
3 |
|
$html = ''; |
64
|
|
|
|
65
|
3 |
|
foreach ($this as $node) { |
66
|
3 |
|
$html .= $node->outertext; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
return $html; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Find list of nodes with a CSS selector. |
74
|
|
|
* |
75
|
|
|
* @param string $selector |
76
|
|
|
* @param int $idx |
77
|
|
|
* |
78
|
|
|
* @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|null |
79
|
|
|
*/ |
80
|
11 |
|
public function find(string $selector, $idx = null) |
81
|
|
|
{ |
82
|
|
|
// init |
83
|
11 |
|
$elements = new self(); |
84
|
|
|
|
85
|
11 |
|
foreach ($this as $node) { |
86
|
11 |
|
foreach ($node->find($selector) as $res) { |
87
|
11 |
|
$elements->append($res); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// return all elements |
92
|
11 |
|
if ($idx === null) { |
93
|
11 |
|
return $elements; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// handle negative values |
97
|
|
|
if ($idx < 0) { |
98
|
|
|
$idx = \count($elements) + $idx; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// return one element |
102
|
|
|
return $elements[$idx] ?? null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Find one node with a CSS selector. |
107
|
|
|
* |
108
|
|
|
* @param string $selector |
109
|
|
|
* |
110
|
|
|
* @return SimpleHtmlDomNode|null |
111
|
|
|
*/ |
112
|
|
|
public function findOne(string $selector) |
113
|
|
|
{ |
114
|
|
|
return $this->find($selector, 0); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get html of elements. |
119
|
|
|
* |
120
|
|
|
* @return string[] |
121
|
|
|
*/ |
122
|
1 |
|
public function innerHtml(): array |
123
|
|
|
{ |
124
|
|
|
// init |
125
|
1 |
|
$html = []; |
126
|
|
|
|
127
|
1 |
|
foreach ($this as $node) { |
128
|
1 |
|
$html[] = $node->outertext; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $html; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
136
|
|
|
*/ |
137
|
|
|
public function innertext() |
138
|
|
|
{ |
139
|
|
|
return $this->innerHtml(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
144
|
|
|
*/ |
145
|
|
|
public function outertext() |
146
|
|
|
{ |
147
|
|
|
return $this->innerHtml(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get plain text. |
152
|
|
|
* |
153
|
|
|
* @return string[] |
154
|
|
|
*/ |
155
|
2 |
|
public function text(): array |
156
|
|
|
{ |
157
|
|
|
// init |
158
|
2 |
|
$text = []; |
159
|
|
|
|
160
|
2 |
|
foreach ($this as $node) { |
161
|
1 |
|
$text[] = $node->plaintext; |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
return $text; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|