@@ 7-119 (lines=113) @@ | ||
4 | ||
5 | namespace voku\helper; |
|
6 | ||
7 | class SimpleHtmlDomNode extends AbstractSimpleHtmlDomNode implements SimpleHtmlDomNodeInterface |
|
8 | { |
|
9 | /** |
|
10 | * Find list of nodes with a CSS selector. |
|
11 | * |
|
12 | * @param string $selector |
|
13 | * @param int|null $idx |
|
14 | * |
|
15 | * @return SimpleHtmlDomNodeInterface|SimpleHtmlDomNodeInterface[]|null |
|
16 | */ |
|
17 | public function find(string $selector, $idx = null) |
|
18 | { |
|
19 | // init |
|
20 | $elements = new static(); |
|
21 | ||
22 | foreach ($this as $node) { |
|
23 | foreach ($node->find($selector) as $res) { |
|
24 | $elements->append($res); |
|
25 | } |
|
26 | } |
|
27 | ||
28 | // return all elements |
|
29 | if ($idx === null) { |
|
30 | if (\count($elements) === 0) { |
|
31 | return new SimpleHtmlDomNodeBlank(); |
|
32 | } |
|
33 | ||
34 | return $elements; |
|
35 | } |
|
36 | ||
37 | // handle negative values |
|
38 | if ($idx < 0) { |
|
39 | $idx = \count($elements) + $idx; |
|
40 | } |
|
41 | ||
42 | // return one element |
|
43 | return $elements[$idx] ?? null; |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * Find nodes with a CSS selector. |
|
48 | * |
|
49 | * @param string $selector |
|
50 | * |
|
51 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
|
52 | */ |
|
53 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
54 | { |
|
55 | return $this->find($selector, null); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * Find one node with a CSS selector. |
|
60 | * |
|
61 | * @param string $selector |
|
62 | * |
|
63 | * @return SimpleHtmlDomNodeInterface|null |
|
64 | */ |
|
65 | public function findOne(string $selector) |
|
66 | { |
|
67 | return $this->find($selector, 0); |
|
68 | } |
|
69 | ||
70 | /** |
|
71 | * Get html of elements. |
|
72 | * |
|
73 | * @return string[] |
|
74 | */ |
|
75 | public function innerHtml(): array |
|
76 | { |
|
77 | // init |
|
78 | $html = []; |
|
79 | ||
80 | foreach ($this as $node) { |
|
81 | $html[] = $node->outertext; |
|
82 | } |
|
83 | ||
84 | return $html; |
|
85 | } |
|
86 | ||
87 | /** |
|
88 | * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
|
89 | */ |
|
90 | public function innertext() |
|
91 | { |
|
92 | return $this->innerHtml(); |
|
93 | } |
|
94 | ||
95 | /** |
|
96 | * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
|
97 | */ |
|
98 | public function outertext() |
|
99 | { |
|
100 | return $this->innerHtml(); |
|
101 | } |
|
102 | ||
103 | /** |
|
104 | * Get plain text. |
|
105 | * |
|
106 | * @return string[] |
|
107 | */ |
|
108 | public function text(): array |
|
109 | { |
|
110 | // init |
|
111 | $text = []; |
|
112 | ||
113 | foreach ($this as $node) { |
|
114 | $text[] = $node->plaintext; |
|
115 | } |
|
116 | ||
117 | return $text; |
|
118 | } |
|
119 | } |
|
120 |
@@ 7-119 (lines=113) @@ | ||
4 | ||
5 | namespace voku\helper; |
|
6 | ||
7 | class SimpleXmlDomNode extends AbstractSimpleXmlDomNode implements SimpleXmlDomNodeInterface |
|
8 | { |
|
9 | /** |
|
10 | * Find list of nodes with a CSS selector. |
|
11 | * |
|
12 | * @param string $selector |
|
13 | * @param int|null $idx |
|
14 | * |
|
15 | * @return SimpleXmlDomNodeInterface|SimpleXmlDomNodeInterface[]|null |
|
16 | */ |
|
17 | public function find(string $selector, $idx = null) |
|
18 | { |
|
19 | // init |
|
20 | $elements = new static(); |
|
21 | ||
22 | foreach ($this as $node) { |
|
23 | foreach ($node->find($selector) as $res) { |
|
24 | $elements->append($res); |
|
25 | } |
|
26 | } |
|
27 | ||
28 | // return all elements |
|
29 | if ($idx === null) { |
|
30 | if (\count($elements) === 0) { |
|
31 | return new SimpleXmlDomNodeBlank(); |
|
32 | } |
|
33 | ||
34 | return $elements; |
|
35 | } |
|
36 | ||
37 | // handle negative values |
|
38 | if ($idx < 0) { |
|
39 | $idx = \count($elements) + $idx; |
|
40 | } |
|
41 | ||
42 | // return one element |
|
43 | return $elements[$idx] ?? null; |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * Find nodes with a CSS selector. |
|
48 | * |
|
49 | * @param string $selector |
|
50 | * |
|
51 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
|
52 | */ |
|
53 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
|
54 | { |
|
55 | return $this->find($selector, null); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * Find one node with a CSS selector. |
|
60 | * |
|
61 | * @param string $selector |
|
62 | * |
|
63 | * @return SimpleXmlDomNodeInterface|null |
|
64 | */ |
|
65 | public function findOne(string $selector) |
|
66 | { |
|
67 | return $this->find($selector, 0); |
|
68 | } |
|
69 | ||
70 | /** |
|
71 | * Get html of elements. |
|
72 | * |
|
73 | * @return string[] |
|
74 | */ |
|
75 | public function innerHtml(): array |
|
76 | { |
|
77 | // init |
|
78 | $html = []; |
|
79 | ||
80 | foreach ($this as $node) { |
|
81 | $html[] = $node->outertext; |
|
82 | } |
|
83 | ||
84 | return $html; |
|
85 | } |
|
86 | ||
87 | /** |
|
88 | * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
|
89 | */ |
|
90 | public function innertext() |
|
91 | { |
|
92 | return $this->innerHtml(); |
|
93 | } |
|
94 | ||
95 | /** |
|
96 | * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
|
97 | */ |
|
98 | public function outertext() |
|
99 | { |
|
100 | return $this->innerHtml(); |
|
101 | } |
|
102 | ||
103 | /** |
|
104 | * Get plain text. |
|
105 | * |
|
106 | * @return string[] |
|
107 | */ |
|
108 | public function text(): array |
|
109 | { |
|
110 | // init |
|
111 | $text = []; |
|
112 | ||
113 | foreach ($this as $node) { |
|
114 | $text[] = $node->plaintext; |
|
115 | } |
|
116 | ||
117 | return $text; |
|
118 | } |
|
119 | } |
|
120 |