GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Xml::toDOMDocument()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
1
<?php
2
/**
3
 * Util
4
 *
5
 * @copyright Copyright (c)  Gjero Krsteski (http://krsteski.de)
6
 * @license   http://opensource.org/licenses/MIT MIT License
7
 */
8
9
namespace Pimf\Util;
10
11
/**
12
 * An XML util for converting XML to DOMDocument or SimpleXMLElement or to Array.
13
 *
14
 * @package Util
15
 * @author  Gjero Krsteski <[email protected]>
16
 */
17
class Xml
18
{
19
    /**
20
     * Convert anything DOMDocument|SimpleXMLElement|string to DOMDocument.
21
     *
22
     * @param \DOMDocument|\SimpleXMLElement|string $xml String may be filename or xml string
23
     *
24
     * @throws \InvalidArgumentException
25
     * @return \DOMDocument
26
     */
27
    public static function toDOMDocument($xml)
28
    {
29
        if ($xml instanceof \DOMDocument) {
30
            return $xml;
31
        }
32
33
        if ($xml instanceof \SimpleXMLElement) {
34
            $doc = new \DOMDocument();
35
            $doc->loadXML('' . $xml->asXML());
36
37
            return $doc;
38
        }
39
40
        if (is_string($xml)) {
41
            $doc = new \DOMDocument();
42
43
            if (is_file($xml)) {
44
                $doc->load($xml);
45
46
                return $doc;
47
            }
48
49
            $doc->loadXML($xml);
50
51
            return $doc;
52
        }
53
54
        $type = is_object($xml) ? get_class($xml) : gettype($xml);
55
56
        throw new \InvalidArgumentException("Cannot convert instance of '$type' to DOMDocument");
57
    }
58
59
    /**
60
     * Convert anything DOMDocument|SimpleXMLElement|string to SimpleXMLElement.
61
     *
62
     * @param \DOMDocument|\SimpleXMLElement|string $xml String may be filename or xml string
63
     *
64
     * @throws \InvalidArgumentException
65
     * @return \SimpleXMLElement
66
     */
67
    public static function toSimpleXMLElement($xml)
68
    {
69
        if ($xml instanceof \SimpleXMLElement) {
70
            return $xml;
71
        }
72
73
        if ($xml instanceof \DOMDocument) {
74
            return simplexml_import_dom($xml);
75
        }
76
77
        if (is_string($xml)) {
78
79
            if (is_file($xml)) {
80
                return simplexml_load_file($xml);
81
            }
82
83
            return simplexml_load_string($xml);
84
        }
85
86
        $type = is_object($xml) ? get_class($xml) : gettype($xml);
87
88
        throw new \InvalidArgumentException("Cannot convert instance of '$type' to DOMDocument");
89
    }
90
91
    /**
92
     * Convert SimpleXMLElement to multidimensional array.
93
     *
94
     * @param \SimpleXMLElement $xml
95
     * @param string            $namespace The namespace that schould be used.
96
     *
97
     * @throws \OutOfBoundsException If namespace not found in the xml.
98
     * @return array
99
     */
100
    public static function toArray(\SimpleXMLElement $xml, $namespace = null)
101
    {
102
        if ($namespace !== null) {
103
104
            $namespaces = $xml->getNamespaces();
105
106
            if (false === isset($namespaces[$namespace])) {
107
                throw new \OutOfBoundsException('namespace [' . $namespace . '] not found');
108
            }
109
110
            $xml = $xml->children($namespaces[$namespace]);
111
        }
112
113
        return Json::decode(Json::encode($xml), true);
114
    }
115
}
116