1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace voku\helper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class SimpleHtmlDomNode |
7
|
|
|
* |
8
|
|
|
* @package voku\helper |
9
|
|
|
* |
10
|
|
|
* @property-read string outertext Get dom node's outer html |
11
|
|
|
* @property-read string plaintext Get dom node's plain text |
12
|
|
|
*/ |
13
|
|
|
class SimpleHtmlDomNode extends \ArrayObject |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param $name |
17
|
|
|
* |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
2 |
|
public function __get($name) |
21
|
|
|
{ |
22
|
|
|
switch ($name) { |
23
|
2 |
|
case 'outertext': |
24
|
2 |
|
case 'innertext': |
25
|
1 |
|
return $this->innerHtml(); |
26
|
1 |
|
case 'plaintext': |
27
|
1 |
|
return $this->text(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
35
|
|
|
*/ |
36
|
|
|
public function outertext() |
37
|
|
|
{ |
38
|
|
|
$this->innerHtml(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
43
|
|
|
*/ |
44
|
|
|
public function innertext() |
45
|
|
|
{ |
46
|
|
|
$this->innerHtml(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $selector |
51
|
|
|
* @param int $idx |
52
|
|
|
* |
53
|
|
|
* @return SimpleHtmlDom|SimpleHtmlDomNode|null |
54
|
|
|
*/ |
55
|
|
|
public function __invoke($selector, $idx = null) |
56
|
|
|
{ |
57
|
|
|
return $this->find($selector, $idx); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function __toString() |
64
|
|
|
{ |
65
|
|
|
return $this->innerHtml(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Find list of nodes with a CSS selector |
70
|
|
|
* |
71
|
|
|
* @param string $selector |
72
|
|
|
* @param int $idx |
73
|
|
|
* |
74
|
|
|
* @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|null |
75
|
|
|
*/ |
76
|
11 |
|
public function find($selector, $idx = null) |
77
|
|
|
{ |
78
|
11 |
|
$elements = new self(); |
79
|
11 |
|
foreach ($this as $node) { |
80
|
11 |
|
foreach ($node->find($selector) as $res) { |
81
|
10 |
|
$elements->append($res); |
82
|
11 |
|
} |
83
|
11 |
|
} |
84
|
|
|
|
85
|
11 |
|
if (null === $idx) { |
86
|
11 |
|
return $elements; |
87
|
|
|
} else { |
88
|
|
|
if ($idx < 0) { |
89
|
|
|
$idx = count($elements) + $idx; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return (isset($elements[$idx]) ? $elements[$idx] : null); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get html of Elements |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
1 |
|
public function innerHtml() |
102
|
|
|
{ |
103
|
1 |
|
$text = ''; |
104
|
1 |
|
foreach ($this as $node) { |
105
|
1 |
|
$text .= $node->outertext; |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
return $text; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get plain text |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
1 |
|
public function text() |
117
|
|
|
{ |
118
|
1 |
|
$text = ''; |
119
|
1 |
|
foreach ($this as $node) { |
120
|
1 |
|
$text .= $node->plaintext; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
return $text; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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: