Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
48.00% |
12 / 25 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
HtmlXPath | |
48.00% |
12 / 25 |
|
33.33% |
2 / 6 |
41.56 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
node | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
toArrayCore | |
41.67% |
5 / 12 |
|
0.00% |
0 / 1 |
13.15 | |||
query | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
evaluate | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
collections | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Html; |
6 | |
7 | use DOMComment; |
8 | use DOMDocument; |
9 | use DOMElement; |
10 | use DOMNode; |
11 | use DOMNodeList; |
12 | use DOMText; |
13 | use DOMXPath; |
14 | use PeServer\Core\Collection\Arr; |
15 | use PeServer\Core\Collection\Collections as Collections; |
16 | use PeServer\Core\Html\HtmlCommentElement; |
17 | use PeServer\Core\Html\HtmlDocument; |
18 | use PeServer\Core\Html\HtmlTagElement; |
19 | use PeServer\Core\Html\HtmlNodeBase; |
20 | use PeServer\Core\Html\HtmlTextElement; |
21 | use PeServer\Core\Text; |
22 | use PeServer\Core\Throws\HtmlXPathException; |
23 | |
24 | /** |
25 | * `DOMXPath` ラッパー。 |
26 | */ |
27 | class HtmlXPath |
28 | { |
29 | #region variable |
30 | |
31 | /** |
32 | */ |
33 | public readonly DOMXPath $path; |
34 | |
35 | #endregion |
36 | |
37 | public function __construct( |
38 | private readonly HtmlDocument $document, |
39 | private readonly ?HtmlTagElement $element |
40 | ) { |
41 | $this->path = new DOMXPath($document->raw); |
42 | } |
43 | |
44 | #region function |
45 | |
46 | private function node(): ?DOMNode |
47 | { |
48 | if ($this->element === null) { |
49 | return null; |
50 | } |
51 | |
52 | return $this->element->raw; |
53 | } |
54 | |
55 | //@phpstan-ignore-next-line |
56 | private function toArrayCore(DOMNodeList $nodeList): array |
57 | { |
58 | $result = []; |
59 | foreach ($nodeList as $node) { |
60 | if ($node instanceof DOMElement) { |
61 | $result[] = new HtmlTagElement($this->document, $node); |
62 | } elseif ($node instanceof DOMText) { |
63 | $result[] = new HtmlTextElement($this->document, $node); |
64 | } elseif ($node instanceof DOMComment) { |
65 | $result[] = new HtmlCommentElement($this->document, $node); |
66 | } elseif ($node instanceof DOMDocument) { |
67 | $result[] = new HtmlDocument(); |
68 | } else { |
69 | throw new HtmlXPathException(Text::dump($node)); |
70 | } |
71 | } |
72 | return $result; |
73 | } |
74 | |
75 | /** |
76 | * 与えられた XPath 式を評価する |
77 | * |
78 | * https://www.php.net/manual/domxpath.query.php |
79 | * |
80 | * @param string $expression |
81 | * @return HtmlNodeBase[] |
82 | * @throws HtmlXPathException |
83 | */ |
84 | public function query(string $expression): array |
85 | { |
86 | $nodeList = $this->path->query($expression, $this->node()); |
87 | if ($nodeList === false) { |
88 | throw new HtmlXPathException(); |
89 | } |
90 | |
91 | return $this->toArrayCore($nodeList); |
92 | } |
93 | |
94 | /** |
95 | * 与えられた XPath 式を評価し、可能であれば結果を返す |
96 | * |
97 | * https://www.php.net/manual/domxpath.evaluate.php |
98 | * |
99 | * @param string $expression |
100 | * @return HtmlNodeBase[] |
101 | * @throws HtmlXPathException |
102 | */ |
103 | public function evaluate(string $expression): array |
104 | { |
105 | $nodeList = $this->path->evaluate($expression, $this->node()); |
106 | if ($nodeList === false) { |
107 | throw new HtmlXPathException(); |
108 | } |
109 | |
110 | return $this->toArrayCore($nodeList); |
111 | } |
112 | |
113 | /** |
114 | * `evaluate` 結果を `Collections` として返す。 |
115 | * |
116 | * @param string $expression |
117 | * @return Collections |
118 | * @phpstan-return Collections<array-key, HtmlNodeBase> |
119 | * @throws HtmlXPathException |
120 | */ |
121 | public function collections(string $expression): Collections |
122 | { |
123 | return Collections::from($this->evaluate($expression)); |
124 | } |
125 | |
126 | #endregion |
127 | } |