1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\helper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class SimpleHtmlDomNode |
9
|
|
|
* |
10
|
|
|
* @package voku\helper |
11
|
|
|
* |
12
|
|
|
* @property-read string outertext <p>Get dom node's outer html.</p> |
13
|
|
|
* @property-read string plaintext <p>Get dom node's plain text.</p> |
14
|
|
|
*/ |
15
|
|
|
class SimpleHtmlDomNode extends \ArrayObject implements SimpleHtmlDomNodeInterface |
16
|
|
|
{ |
17
|
|
|
/** @noinspection MagicMethodsValidityInspection */ |
18
|
|
|
/** |
19
|
|
|
* @param string $name |
20
|
|
|
* |
21
|
|
|
* @return array|null |
22
|
|
|
*/ |
23
|
3 |
|
public function __get($name) |
24
|
|
|
{ |
25
|
3 |
|
$name = strtolower($name); |
26
|
|
|
|
27
|
3 |
|
if ($this->count() > 0) { |
28
|
3 |
|
$return = array(); |
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
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
44
|
|
|
*/ |
45
|
|
|
public function outertext() |
46
|
|
|
{ |
47
|
|
|
$this->innerHtml(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
52
|
|
|
*/ |
53
|
|
|
public function innertext() |
54
|
|
|
{ |
55
|
|
|
$this->innerHtml(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $selector |
60
|
|
|
* @param int $idx |
61
|
|
|
* |
62
|
|
|
* @return SimpleHtmlDomNode[]|SimpleHtmlDomNode|null |
63
|
|
|
*/ |
64
|
|
|
public function __invoke($selector, $idx = null) |
65
|
|
|
{ |
66
|
|
|
return $this->find($selector, $idx); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
public function __toString() |
73
|
|
|
{ |
74
|
1 |
|
$html = ''; |
75
|
1 |
|
foreach ($this as $node) { |
76
|
1 |
|
$html .= $node->outertext; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $html; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Find one node with a CSS selector. |
84
|
|
|
* |
85
|
|
|
* @param string $selector |
86
|
|
|
* |
87
|
|
|
* @return SimpleHtmlDomNode|null |
88
|
|
|
*/ |
89
|
|
|
public function findOne(string $selector) |
90
|
|
|
{ |
91
|
|
|
return $this->find($selector, 0); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Find list of nodes with a CSS selector. |
96
|
|
|
* |
97
|
|
|
* @param string $selector |
98
|
|
|
* @param int $idx |
99
|
|
|
* |
100
|
|
|
* @return SimpleHtmlDomNode[]|SimpleHtmlDomNode|null |
101
|
|
|
*/ |
102
|
11 |
|
public function find(string $selector, $idx = null) |
103
|
|
|
{ |
104
|
11 |
|
$elements = new self(); |
105
|
11 |
|
foreach ($this as $node) { |
106
|
11 |
|
foreach ($node->find($selector) as $res) { |
107
|
11 |
|
$elements->append($res); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// return all elements |
112
|
11 |
|
if (null === $idx) { |
113
|
11 |
|
return $elements; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// handle negative values |
117
|
|
|
if ($idx < 0) { |
118
|
|
|
$idx = \count($elements) + $idx; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// return one element |
122
|
|
|
if (isset($elements[$idx])) { |
123
|
|
|
return $elements[$idx]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get html of elements. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
1 |
|
public function innerHtml(): array |
135
|
|
|
{ |
136
|
1 |
|
$html = array(); |
137
|
1 |
|
foreach ($this as $node) { |
138
|
1 |
|
$html[] = $node->outertext; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
return $html; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get plain text. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
1 |
|
public function text(): array |
150
|
|
|
{ |
151
|
1 |
|
$text = array(); |
152
|
1 |
|
foreach ($this as $node) { |
153
|
1 |
|
$text[] = $node->plaintext; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $text; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: