@@ 438-464 (lines=27) @@ | ||
435 | * |
|
436 | * @return XmlDomParser |
|
437 | */ |
|
438 | public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface |
|
439 | { |
|
440 | if ( |
|
441 | !\preg_match("/^https?:\/\//i", $filePath) |
|
442 | && |
|
443 | !\file_exists($filePath) |
|
444 | ) { |
|
445 | throw new \RuntimeException("File ${filePath} not found"); |
|
446 | } |
|
447 | ||
448 | try { |
|
449 | if (\class_exists('\voku\helper\UTF8')) { |
|
450 | /** @noinspection PhpUndefinedClassInspection */ |
|
451 | $html = UTF8::file_get_contents($filePath); |
|
452 | } else { |
|
453 | $html = \file_get_contents($filePath); |
|
454 | } |
|
455 | } catch (\Exception $e) { |
|
456 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
457 | } |
|
458 | ||
459 | if ($html === false) { |
|
460 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
461 | } |
|
462 | ||
463 | return $this->loadHtml($html, $libXMLExtraOptions); |
|
464 | } |
|
465 | ||
466 | /** |
|
467 | * @param string $selector |
|
@@ 502-528 (lines=27) @@ | ||
499 | * |
|
500 | * @return XmlDomParser |
|
501 | */ |
|
502 | public function loadXmlFile(string $filePath, $libXMLExtraOptions = null): self |
|
503 | { |
|
504 | if ( |
|
505 | !\preg_match("/^https?:\/\//i", $filePath) |
|
506 | && |
|
507 | !\file_exists($filePath) |
|
508 | ) { |
|
509 | throw new \RuntimeException("File ${filePath} not found"); |
|
510 | } |
|
511 | ||
512 | try { |
|
513 | if (\class_exists('\voku\helper\UTF8')) { |
|
514 | /** @noinspection PhpUndefinedClassInspection */ |
|
515 | $xml = UTF8::file_get_contents($filePath); |
|
516 | } else { |
|
517 | $xml = \file_get_contents($filePath); |
|
518 | } |
|
519 | } catch (\Exception $e) { |
|
520 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
521 | } |
|
522 | ||
523 | if ($xml === false) { |
|
524 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
525 | } |
|
526 | ||
527 | return $this->loadXml($xml, $libXMLExtraOptions); |
|
528 | } |
|
529 | ||
530 | /** |
|
531 | * @param callable $callback |
@@ 767-796 (lines=30) @@ | ||
764 | * |
|
765 | * @return HtmlDomParser |
|
766 | */ |
|
767 | public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface |
|
768 | { |
|
769 | // reset |
|
770 | self::$domBrokenReplaceHelper = []; |
|
771 | ||
772 | if ( |
|
773 | !\preg_match("/^https?:\/\//i", $filePath) |
|
774 | && |
|
775 | !\file_exists($filePath) |
|
776 | ) { |
|
777 | throw new \RuntimeException("File ${filePath} not found"); |
|
778 | } |
|
779 | ||
780 | try { |
|
781 | if (\class_exists('\voku\helper\UTF8')) { |
|
782 | /** @noinspection PhpUndefinedClassInspection */ |
|
783 | $html = UTF8::file_get_contents($filePath); |
|
784 | } else { |
|
785 | $html = \file_get_contents($filePath); |
|
786 | } |
|
787 | } catch (\Exception $e) { |
|
788 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
789 | } |
|
790 | ||
791 | if ($html === false) { |
|
792 | throw new \RuntimeException("Could not load file ${filePath}"); |
|
793 | } |
|
794 | ||
795 | return $this->loadHtml($html, $libXMLExtraOptions); |
|
796 | } |
|
797 | ||
798 | /** |
|
799 | * Get the HTML as XML or plain XML if needed. |