1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sulao\HtmlQuery; |
4
|
|
|
|
5
|
|
|
use DOMDocument, DOMNode, DOMNodeList; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait Selector |
9
|
|
|
* |
10
|
|
|
* @package Sulao\HtmlQuery |
11
|
|
|
*/ |
12
|
|
|
trait Resolver |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DOMDocument |
16
|
|
|
*/ |
17
|
|
|
protected $doc; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var DOMNode[] |
21
|
|
|
*/ |
22
|
|
|
protected $nodes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Selector constructor. |
26
|
|
|
* |
27
|
|
|
* @param DOMDocument $doc |
28
|
|
|
* @param DOMNode|DOMNode[]|DOMNodeList|static $nodes |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
abstract public function __construct(DOMDocument $doc, $nodes); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the descendants of each matched node, filtered by a selector. |
36
|
|
|
* |
37
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $selector |
38
|
|
|
* |
39
|
|
|
* @return static |
40
|
|
|
*/ |
41
|
|
|
abstract public function find($selector); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Resolve DOMNode(s) to a static instance. |
45
|
|
|
* |
46
|
|
|
* @param DOMNode|DOMNode[]|DOMNodeList|static $nodes |
47
|
|
|
* |
48
|
|
|
* @return static |
49
|
|
|
*/ |
50
|
|
|
protected function resolve($nodes) |
51
|
|
|
{ |
52
|
|
|
if ($nodes instanceof static) { |
53
|
|
|
return $nodes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new static($this->doc, $nodes); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* If the parameter is a css selector, get the descendants |
61
|
|
|
* of dom document filtered by the css selector. |
62
|
|
|
* If the parameter is selection, resolve that selection to static object. |
63
|
|
|
* |
64
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $selector |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
|
|
protected function targetResolve($selector) |
69
|
|
|
{ |
70
|
|
|
if (is_string($selector)) { |
71
|
|
|
return $this->resolve($this->doc)->find($selector); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->resolve($selector); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* If the parameter is string, consider it as raw html, |
79
|
|
|
* then create document fragment for it. |
80
|
|
|
* If the parameter is selection, resolve that selection to static instance. |
81
|
|
|
* |
82
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
|
|
protected function contentResolve($content) |
87
|
|
|
{ |
88
|
|
|
if (is_string($content)) { |
89
|
|
|
return $this->htmlResolve($content); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->resolve($content); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Resolve the html content to static instance. |
97
|
|
|
* |
98
|
|
|
* @param string $html |
99
|
|
|
* |
100
|
|
|
* @return static |
101
|
|
|
*/ |
102
|
|
|
protected function htmlResolve(string $html) |
103
|
|
|
{ |
104
|
|
|
$frag = $this->doc->createDocumentFragment(); |
105
|
|
|
$frag->appendXML($html); |
106
|
|
|
|
107
|
|
|
return $this->resolve($frag); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Resolve the nodes under the relation to static instance. |
112
|
|
|
* up to but not including the node matched by the $until selector. |
113
|
|
|
* |
114
|
|
|
* @param string $relation |
115
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $until |
116
|
|
|
* |
117
|
|
|
* @return static |
118
|
|
|
*/ |
119
|
|
|
protected function relationResolve(string $relation, ?string $until = null) |
120
|
|
|
{ |
121
|
|
|
$untilNodes = !is_null($until) |
122
|
|
|
? $this->targetResolve($until)->nodes |
123
|
|
|
: []; |
124
|
|
|
|
125
|
|
|
$nodes = []; |
126
|
|
|
foreach ($this->nodes as $node) { |
127
|
|
|
while (($node = $node->$relation) |
128
|
|
|
&& $node->nodeType !== XML_DOCUMENT_NODE |
129
|
|
|
) { |
130
|
|
|
if ($node->nodeType !== XML_ELEMENT_NODE) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (in_array($node, $untilNodes, true)) { |
135
|
|
|
break; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!in_array($node, $nodes, true)) { |
139
|
|
|
$nodes[] = $node; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->resolve($nodes); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Resolve the xpath to static instance. |
149
|
|
|
* |
150
|
|
|
* @param string $xpath |
151
|
|
|
* |
152
|
|
|
* @return static |
153
|
|
|
*/ |
154
|
|
|
protected function xpathResolve(string $xpath) |
155
|
|
|
{ |
156
|
|
|
$nodes = []; |
157
|
|
|
foreach ($this->nodes as $node) { |
158
|
|
|
$nodes = array_merge($nodes, $this->xpathQuery($xpath, $node)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$nodes = Helper::strictArrayUnique($nodes); |
162
|
|
|
|
163
|
|
|
return $this->resolve($nodes); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Query xpath to an array of DOMNode |
168
|
|
|
* |
169
|
|
|
* @param string $xpath |
170
|
|
|
* @param DOMNode|null $node |
171
|
|
|
* |
172
|
|
|
* @return DOMNode[] |
173
|
|
|
*/ |
174
|
|
|
protected function xpathQuery( |
175
|
|
|
string $xpath, |
176
|
|
|
?DOMNode $node = null |
177
|
|
|
): array { |
178
|
|
|
return Helper::xpathQuery($xpath, $this->doc, $node); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|