Passed
Push — master ( 8c2778...04210d )
by Domenico
02:17
created

XmlParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 34
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 4 1
A getNodeListFromQueryPath() 0 11 2
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 DOMNodeList;
15
use DOMXPath;
16
use Matecat\XmlParser\Exception\InvalidXmlException;
17
use Matecat\XmlParser\Exception\XmlParsingException;
18
19
class XmlParser extends AbstractParser {
20
21
    /**
22
     * This solution is taken from here and then modified:
23
     * https://www.php.net/manual/fr/regexp.reference.recursive.php#95568
24
     *
25
     * @param string $xml
26
     * @param bool   $isXmlFragment
27
     *
28
     * @return ArrayObject
29
     * @throws DOMException
30
     * @throws InvalidXmlException
31
     * @throws XmlParsingException
32
     */
33
    public static function parse( $xml, $isXmlFragment = false ) {
34
        $parser = new static( $xml, $isXmlFragment );
35
36
        return $parser->extractNodes();
37
    }
38
39
    /**
40
     * @return DOMNodeList
41
     */
42
    protected function getNodeListFromQueryPath(){
43
44
        $xpath = new DOMXPath( $this->dom );
45
46
        if ( $this->isXmlFragment ) {
47
            $xmlNodeList = $xpath->query( "/" . self::fragmentDocumentRoot );
48
        } else {
49
            $xmlNodeList = $xpath->query( "*" );
50
        }
51
52
        return $xmlNodeList;
53
    }
54
55
}