1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace voku\helper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class SimpleHtmlDomNode |
7
|
|
|
* |
8
|
|
|
* @package FastSimpleHTMLDom |
9
|
|
|
* @property-read string outertext Get dom node's outer html |
10
|
|
|
* @property-read string plaintext Get dom node's plain text |
11
|
|
|
*/ |
12
|
|
|
class SimpleHtmlDomNode extends \ArrayObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param $name |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
2 |
|
public function __get($name) |
20
|
|
|
{ |
21
|
|
|
switch ($name) { |
22
|
2 |
|
case 'innertext': |
23
|
1 |
|
return $this->innerHtml(); |
24
|
1 |
|
case 'plaintext': |
25
|
1 |
|
return $this->text(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $selector |
33
|
|
|
* @param int $idx |
34
|
|
|
* |
35
|
|
|
* @return SimpleHtmlDom|SimpleHtmlDomNode|null |
36
|
|
|
*/ |
37
|
|
|
public function __invoke($selector, $idx = null) |
38
|
|
|
{ |
39
|
|
|
return $this->find($selector, $idx); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function __toString() |
46
|
|
|
{ |
47
|
|
|
return $this->innerHtml(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Find list of nodes with a CSS selector |
52
|
|
|
* |
53
|
|
|
* @param string $selector |
54
|
|
|
* @param int $idx |
55
|
|
|
* |
56
|
|
|
* @return SimpleHtmlDomNode|SimpleHtmlDom|null |
57
|
|
|
*/ |
58
|
11 |
|
public function find($selector, $idx = null) |
59
|
|
|
{ |
60
|
11 |
|
$elements = new SimpleHtmlDomNode(); |
61
|
11 |
|
foreach ($this as $node) { |
62
|
11 |
|
foreach ($node->find($selector) as $res) { |
63
|
10 |
|
$elements->append($res); |
64
|
11 |
|
} |
65
|
11 |
|
} |
66
|
|
|
|
67
|
11 |
View Code Duplication |
if (null === $idx) { |
|
|
|
|
68
|
11 |
|
return $elements; |
69
|
|
|
} else { |
70
|
|
|
if ($idx < 0) { |
71
|
|
|
$idx = count($elements) + $idx; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return (isset($elements[$idx]) ? $elements[$idx] : null); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get html of Elements |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
1 |
|
public function innerHtml() |
84
|
|
|
{ |
85
|
1 |
|
$text = ''; |
86
|
1 |
|
foreach ($this as $node) { |
87
|
1 |
|
$text .= $node->outertext; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
return $text; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get plain text |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
1 |
|
public function text() |
99
|
|
|
{ |
100
|
1 |
|
$text = ''; |
101
|
1 |
|
foreach ($this as $node) { |
102
|
1 |
|
$text .= $node->plaintext; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
return $text; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.