@@ 500-536 (lines=37) @@ | ||
497 | * |
|
498 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
499 | */ |
|
500 | public function find(string $selector, $idx = null) |
|
501 | { |
|
502 | $xPathQuery = SelectorConverter::toXPath($selector); |
|
503 | ||
504 | $xPath = new \DOMXPath($this->document); |
|
505 | ||
506 | if ($this->callbackXPathBeforeQuery) { |
|
507 | $xPathQuery = \call_user_func($this->callbackXPathBeforeQuery, $selector, $xPathQuery, $xPath, $this); |
|
508 | } |
|
509 | ||
510 | $nodesList = $xPath->query($xPathQuery); |
|
511 | ||
512 | $elements = new SimpleHtmlDomNode(); |
|
513 | ||
514 | if ($nodesList) { |
|
515 | foreach ($nodesList as $node) { |
|
516 | $elements[] = new SimpleHtmlDom($node); |
|
517 | } |
|
518 | } |
|
519 | ||
520 | // return all elements |
|
521 | if ($idx === null) { |
|
522 | if (\count($elements) === 0) { |
|
523 | return new SimpleHtmlDomNodeBlank(); |
|
524 | } |
|
525 | ||
526 | return $elements; |
|
527 | } |
|
528 | ||
529 | // handle negative values |
|
530 | if ($idx < 0) { |
|
531 | $idx = \count($elements) + $idx; |
|
532 | } |
|
533 | ||
534 | // return one element |
|
535 | return $elements[$idx] ?? new SimpleHtmlDomBlank(); |
|
536 | } |
|
537 | ||
538 | /** |
|
539 | * Find nodes with a CSS selector. |
@@ 245-281 (lines=37) @@ | ||
242 | * |
|
243 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
|
244 | */ |
|
245 | public function find(string $selector, $idx = null) |
|
246 | { |
|
247 | $xPathQuery = SelectorConverter::toXPath($selector); |
|
248 | ||
249 | $xPath = new \DOMXPath($this->document); |
|
250 | ||
251 | if ($this->callbackXPathBeforeQuery) { |
|
252 | $xPathQuery = \call_user_func($this->callbackXPathBeforeQuery, $selector, $xPathQuery, $xPath, $this); |
|
253 | } |
|
254 | ||
255 | $nodesList = $xPath->query($xPathQuery); |
|
256 | ||
257 | $elements = new SimpleXmlDomNode(); |
|
258 | ||
259 | if ($nodesList) { |
|
260 | foreach ($nodesList as $node) { |
|
261 | $elements[] = new SimpleXmlDom($node); |
|
262 | } |
|
263 | } |
|
264 | ||
265 | // return all elements |
|
266 | if ($idx === null) { |
|
267 | if (\count($elements) === 0) { |
|
268 | return new SimpleXmlDomNodeBlank(); |
|
269 | } |
|
270 | ||
271 | return $elements; |
|
272 | } |
|
273 | ||
274 | // handle negative values |
|
275 | if ($idx < 0) { |
|
276 | $idx = \count($elements) + $idx; |
|
277 | } |
|
278 | ||
279 | // return one element |
|
280 | return $elements[$idx] ?? new SimpleXmlDomBlank(); |
|
281 | } |
|
282 | ||
283 | /** |
|
284 | * Find nodes with a CSS selector. |