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