matecat /
xml-dom-parser
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * @author hashashiyyin [email protected] / [email protected] |
||
| 5 | * Date: 23/04/24 |
||
| 6 | * Time: 16:45 |
||
| 7 | * |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace Matecat\XmlParser; |
||
| 11 | |||
| 12 | use ArrayObject; |
||
| 13 | use DOMException; |
||
| 14 | use DOMXPath; |
||
| 15 | use Matecat\XmlParser\Exception\InvalidXmlException; |
||
| 16 | use Matecat\XmlParser\Exception\XmlParsingException; |
||
| 17 | |||
| 18 | class HtmlParser extends AbstractParser { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * This solution is taken from here and then modified: |
||
| 22 | * https://www.php.net/manual/fr/regexp.reference.recursive.php#95568 |
||
| 23 | * |
||
| 24 | * @param string $xml |
||
| 25 | * @param bool $isXmlFragment |
||
| 26 | * |
||
| 27 | * @return ArrayObject |
||
| 28 | * @throws DOMException |
||
| 29 | * @throws InvalidXmlException |
||
| 30 | * @throws XmlParsingException |
||
| 31 | */ |
||
| 32 | public static function parse( $xml, $isXmlFragment = false ) { |
||
| 33 | $parser = new static( $xml, $isXmlFragment, true ); |
||
| 34 | |||
| 35 | return $parser->extractNodes(); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return ArrayObject |
||
| 40 | */ |
||
| 41 | protected function getNodeListFromQueryPath() { |
||
| 42 | |||
| 43 | $xpath = new DOMXPath( $this->dom ); |
||
| 44 | |||
| 45 | if ( $this->isXmlFragment ) { |
||
| 46 | $htmlNodeList = $xpath->query( "/" . self::fragmentDocumentRoot ); |
||
| 47 | } else { |
||
| 48 | $htmlNodeList = $xpath->query( "/html" ); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $htmlNodeList; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 52 | |||
| 53 | } |
||
| 54 | |||
| 55 | } |