1 | <?php |
||
24 | class Reader implements CrawlerInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var DOMDocument |
||
28 | */ |
||
29 | private $document; |
||
30 | |||
31 | /** |
||
32 | * @var SafeXPath |
||
33 | */ |
||
34 | private $xpath; |
||
35 | |||
36 | /** |
||
37 | * @var ElementMap |
||
38 | */ |
||
39 | private $inputMap; |
||
40 | |||
41 | /** |
||
42 | * @param DOMDocument $document |
||
43 | */ |
||
44 | public function __construct(DOMDocument $document) |
||
45 | { |
||
46 | $this->document = $document; |
||
47 | |||
48 | $this->xpath = new SafeXPath($document); |
||
49 | |||
50 | $this->inputMap = new InputMap($this); |
||
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $content |
||
55 | * @return self |
||
56 | */ |
||
57 | 1 | public function setDocumentContent($content) |
|
58 | { |
||
59 | $this->document->loadHtml((string) $content); |
||
60 | $this->xpath = new SafeXPath($this->document); |
||
61 | |||
62 | 1 | return $this; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return DOMDocument |
||
67 | */ |
||
68 | 1 | public function getDocument() |
|
72 | |||
73 | /** |
||
74 | * @return SafeXPath |
||
75 | */ |
||
76 | public function getXPath() |
||
77 | { |
||
78 | return $this->xpath; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return ElementMap |
||
83 | */ |
||
84 | public function getInputMap() |
||
85 | { |
||
86 | return $this->inputMap; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $id |
||
91 | * @throws BadMethodCallException |
||
92 | */ |
||
93 | 3 | public function click($id) |
|
94 | { |
||
95 | $input = $this->getInput($this->getElement($id)); |
||
96 | |||
97 | 3 | if ($input instanceof ClickableInterface) { |
|
98 | $input->click(); |
||
99 | 2 | } elseif ($input instanceof ClickRequestInterface) { |
|
100 | $request = $input->clickRequest(); |
||
101 | $this->sendRequest($request); |
||
102 | } else { |
||
103 | throw new BadMethodCallException( |
||
104 | sprintf('Cannot click on %s, %s', get_class($input), $id) |
||
105 | ); |
||
106 | 2 | } |
|
107 | 2 | } |
|
108 | |||
109 | /** |
||
110 | * @param string $id |
||
111 | * @throws BadMethodCallException |
||
112 | */ |
||
113 | 2 | public function select($id) |
|
114 | { |
||
115 | $input = $this->getInput($this->getElement($id)); |
||
116 | |||
117 | 2 | if ($input instanceof SelectableInterface) { |
|
118 | $input->select(); |
||
119 | } else { |
||
120 | throw new BadMethodCallException( |
||
121 | sprintf('Cannot select on %s, %s', get_class($input), $id) |
||
122 | ); |
||
123 | 1 | } |
|
124 | 1 | } |
|
125 | |||
126 | /** |
||
127 | * @param RequestInterface $input |
||
128 | * @throws BadMethodCallException |
||
129 | */ |
||
130 | 1 | public function sendRequest(RequestInterface $request) |
|
131 | { |
||
132 | throw new BadMethodCallException( |
||
133 | sprintf('Cannot send request to %s', $request->getUri()) |
||
134 | ); |
||
135 | 1 | } |
|
136 | |||
137 | /** |
||
138 | * @param string $url |
||
139 | * @throws BadMethodCallException |
||
140 | */ |
||
141 | 1 | public function open(UriInterface $url) |
|
142 | { |
||
143 | throw new BadMethodCallException( |
||
144 | 1 | sprintf('Method %s not supported by %s', __METHOD__, __CLASS__) |
|
145 | ); |
||
146 | 1 | } |
|
147 | |||
148 | /** |
||
149 | * @return Psr\Http\Message\UriInterface |
||
150 | */ |
||
151 | public function getUri() |
||
152 | { |
||
153 | return new Uri(''); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $xpath |
||
158 | * @param DOMElement|null $scope |
||
159 | * @throws InvalidArgumentException If xpath is not valid |
||
160 | * @return DOMNodeList |
||
161 | */ |
||
162 | 1 | public function query($xpath, DOMElement $scope = null) |
|
166 | |||
167 | /** |
||
168 | * @param string $xpath |
||
169 | * @throws InvalidArgumentException when id not found |
||
170 | * @return DOMElement |
||
171 | */ |
||
172 | 1 | public function getElement($xpath) |
|
173 | { |
||
174 | $items = $this->query($xpath); |
||
175 | |||
176 | if (0 === $items->length) { |
||
177 | throw new InvalidArgumentException( |
||
178 | 1 | sprintf('Node with id %s does not exist', $xpath) |
|
179 | ); |
||
180 | } |
||
181 | |||
182 | return $items->item(0); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string $id |
||
187 | * @throws InvalidArgumentException when id not found |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getText($id) |
||
191 | { |
||
192 | $element = $this->getElement($id); |
||
193 | |||
194 | return trim(preg_replace('/[ \s\f\n\r\t\v ]+/u', ' ', $element->textContent)); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param string $id |
||
199 | * @throws InvalidArgumentException when id not found |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getTagName($id) |
||
203 | { |
||
204 | return $this->getElement($id)->tagName; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param string $id |
||
209 | * @param string $name |
||
210 | * @throws InvalidArgumentException when id not found |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getAttribute($id, $name) |
||
214 | { |
||
215 | return $this->getElement($id)->getAttribute($name); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $id |
||
220 | * @throws InvalidArgumentException when id not found |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getHtml($id) |
||
224 | { |
||
225 | return $this->document->saveXml($this->getElement($id)); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getFullHtml() |
||
232 | { |
||
233 | return $this->document->saveHtml(); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param string $id |
||
238 | * @throws InvalidArgumentException when id not found |
||
239 | * @return boolean |
||
240 | */ |
||
241 | 3 | public function isVisible($id) |
|
242 | { |
||
243 | $element = $this->getElement($id); |
||
244 | |||
245 | $conditions = [ |
||
246 | "contains(@style, 'display:none')", |
||
247 | "contains(@style, 'display: none')", |
||
248 | "self::script", |
||
249 | "self::head", |
||
250 | 3 | ]; |
|
251 | |||
252 | 3 | $hidden = $this->xpath->query( |
|
253 | './ancestor-or-self::*['.join(' or ', $conditions).']', |
||
254 | $element |
||
255 | ); |
||
256 | |||
257 | 3 | return $hidden->length == 0; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param string $id |
||
262 | * @throws InvalidArgumentException when id not found |
||
263 | * @return boolean |
||
264 | */ |
||
265 | public function isSelected($id) |
||
266 | { |
||
267 | return $this->getElement($id)->hasAttribute('selected'); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param string $id |
||
272 | * @throws InvalidArgumentException when id not found |
||
273 | * @return boolean |
||
274 | */ |
||
275 | public function isChecked($id) |
||
276 | { |
||
277 | return $this->getElement($id)->hasAttribute('checked'); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param DOMElement $element |
||
282 | * @throws InvalidArgumentException when id not found |
||
283 | * @return Element\AbstractElement |
||
284 | */ |
||
285 | 9 | public function getInput(DOMElement $element) |
|
289 | |||
290 | /** |
||
291 | * @param string $id |
||
292 | * @throws InvalidArgumentException when id not found |
||
293 | * @return mixed |
||
294 | */ |
||
295 | public function getValue($id) |
||
296 | { |
||
297 | return $this->getInput($this->getElement($id))->getValue(); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param string $id |
||
302 | * @param string $value |
||
303 | * @throws InvalidArgumentException when id not found |
||
304 | */ |
||
305 | 1 | public function setValue($id, $value) |
|
306 | { |
||
307 | $input = $this->getInput($this->getElement($id)); |
||
308 | |||
313 | |||
314 | /** |
||
315 | * @param string $id |
||
316 | * @param string $file |
||
317 | * @throws InvalidArgumentException when id not found or not a file |
||
318 | */ |
||
319 | 2 | public function setFile($id, $file) |
|
333 | |||
334 | /** |
||
335 | * @param AbstractQuery $query |
||
336 | * @param string $parent |
||
337 | * @return array |
||
338 | */ |
||
339 | 1 | public function queryIds(AbstractQuery $query, $parent = null) |
|
351 | } |
||
352 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..