@@ 437-463 (lines=27) @@ | ||
434 | * |
|
435 | * @return XmlDomParser |
|
436 | */ |
|
437 | public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface |
|
438 | { |
|
439 | if ( |
|
440 | !\preg_match("/^https?:\/\//i", $filePath) |
|
441 | && |
|
442 | !\file_exists($filePath) |
|
443 | ) { |
|
444 | throw new \RuntimeException("File ${filePath} not found"); |
|
445 | } |
|
446 | ||
447 | try { |
|
448 | if (\class_exists('\voku\helper\UTF8')) { |
|
449 | /** @noinspection PhpUndefinedClassInspection */ |
|
450 | $html = UTF8::file_get_contents($filePath); |
|
451 | } else { |
|
452 | $html = \file_get_contents($filePath); |
|
453 | } |
|
454 | } catch (\Exception $e) { |
|
455 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
456 | } |
|
457 | ||
458 | if ($html === false) { |
|
459 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
460 | } |
|
461 | ||
462 | return $this->loadHtml($html, $libXMLExtraOptions); |
|
463 | } |
|
464 | ||
465 | /** |
|
466 | * @param string $selector |
|
@@ 501-527 (lines=27) @@ | ||
498 | * |
|
499 | * @return XmlDomParser |
|
500 | */ |
|
501 | public function loadXmlFile(string $filePath, $libXMLExtraOptions = null): self |
|
502 | { |
|
503 | if ( |
|
504 | !\preg_match("/^https?:\/\//i", $filePath) |
|
505 | && |
|
506 | !\file_exists($filePath) |
|
507 | ) { |
|
508 | throw new \RuntimeException("File ${filePath} not found"); |
|
509 | } |
|
510 | ||
511 | try { |
|
512 | if (\class_exists('\voku\helper\UTF8')) { |
|
513 | /** @noinspection PhpUndefinedClassInspection */ |
|
514 | $xml = UTF8::file_get_contents($filePath); |
|
515 | } else { |
|
516 | $xml = \file_get_contents($filePath); |
|
517 | } |
|
518 | } catch (\Exception $e) { |
|
519 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
520 | } |
|
521 | ||
522 | if ($xml === false) { |
|
523 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
524 | } |
|
525 | ||
526 | return $this->loadXml($xml, $libXMLExtraOptions); |
|
527 | } |
|
528 | ||
529 | /** |
|
530 | * @param callable $callback |
@@ 830-859 (lines=30) @@ | ||
827 | * |
|
828 | * @return HtmlDomParser |
|
829 | */ |
|
830 | public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface |
|
831 | { |
|
832 | // reset |
|
833 | self::$domBrokenReplaceHelper = []; |
|
834 | ||
835 | if ( |
|
836 | !\preg_match("/^https?:\/\//i", $filePath) |
|
837 | && |
|
838 | !\file_exists($filePath) |
|
839 | ) { |
|
840 | throw new \RuntimeException("File ${filePath} not found"); |
|
841 | } |
|
842 | ||
843 | try { |
|
844 | if (\class_exists('\voku\helper\UTF8')) { |
|
845 | /** @noinspection PhpUndefinedClassInspection */ |
|
846 | $html = UTF8::file_get_contents($filePath); |
|
847 | } else { |
|
848 | $html = \file_get_contents($filePath); |
|
849 | } |
|
850 | } catch (\Exception $e) { |
|
851 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
852 | } |
|
853 | ||
854 | if ($html === false) { |
|
855 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
856 | } |
|
857 | ||
858 | return $this->loadHtml($html, $libXMLExtraOptions); |
|
859 | } |
|
860 | ||
861 | /** |
|
862 | * Get the HTML as XML or plain XML if needed. |