Completed
Push — master ( 99003e...08a002 )
by Alex
01:25
created

XML2Array::createArray()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 24
c 2
b 0
f 0
rs 6.7272
cc 7
eloc 18
nc 7
nop 1
1
<?php
2
namespace POData\UriProcessor;
3
4
class XML2Array
5
{
6
 /**
7
     * @var string
8
     */
9
    private static $encoding = 'UTF-8';
10
    /**
11
     * @var \DOMDocument
12
     */
13
    private static $xml = null;
14
    /**
15
     * Convert an XML to Array.
16
     *
17
     * @param string|\DOMDocument $input_xml
18
     *
19
     * @return array
20
     *
21
     * @throws \Exception
22
     */
23
    public static function createArray($input_xml)
24
    {
25
        $xml = self::getXMLRoot();
26
        if (is_string($input_xml)) {
27
            try {
28
                $xml->loadXML($input_xml);
29
                if (!is_object($xml) || empty($xml->documentElement)) {
30
                    throw new \Exception();
31
                }
32
            } catch (\Exception $ex) {
33
                throw new \Exception('[XML2Array] Error parsing the XML string.'.PHP_EOL.$ex->getMessage());
34
            }
35
        } elseif (is_object($input_xml)) {
36
            if (get_class($input_xml) != 'DOMDocument') {
37
                throw new \Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
38
            }
39
            $xml = self::$xml = $input_xml;
40
        } else {
41
            throw new \Exception('[XML2Array] Invalid input');
42
        }
43
        $array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
44
        self::$xml = null;    // clear the xml node in the class for 2nd time use.
45
        return $array;
46
    }
47
    /**
48
     * Initialize the root XML node [optional].
49
     *
50
     * @param string $version
51
     * @param string $encoding
52
     * @param bool   $standalone
53
     * @param bool   $format_output
54
     */
55
    public static function init($version = '1.0', $encoding = 'utf-8', $standalone = false, $format_output = true)
56
    {
57
        self::$xml = new \DomDocument($version, $encoding);
58
        self::$xml->xmlStandalone = $standalone;
59
        self::$xml->formatOutput = $format_output;
60
        self::$encoding = $encoding;
61
    }
62
    /**
63
     * Convert an Array to XML.
64
     *
65
     * @param \DOMNode $node - XML as a string or as an object of DOMDocument
66
     *
67
     * @return array
68
     */
69
    private static function convert(DOMNode $node)
70
    {
71
        $output = [];
72
        switch ($node->nodeType) {
73
            case XML_CDATA_SECTION_NODE:
74
                $output['@cdata'] = trim($node->textContent);
75
                break;
76
            case XML_TEXT_NODE:
77
                $output = trim($node->textContent);
78
                break;
79
            case XML_ELEMENT_NODE:
80
                // for each child node, call the covert function recursively
81
                for ($i = 0, $m = $node->childNodes->length; $i < $m; ++$i) {
82
                    $child = $node->childNodes->item($i);
83
                    $v = self::convert($child);
84
                    if (isset($child->tagName)) {
85
                        $t = $child->tagName;
86
                        // assume more nodes of same kind are coming
87
                        if (!array_key_exists($t, $output)) {
88
                            $output[$t] = [];
89
                        }
90
                        $output[$t][] = $v;
91
                    } else {
92
                        //check if it is not an empty node
93
                        if (!empty($v)) {
94
                            $output = $v;
95
                        }
96
                    }
97
                }
98
                if (is_array($output)) {
99
                    // if only one node of its kind, assign it directly instead if array($value);
100
                    foreach ($output as $t => $v) {
101
                        if (is_array($v) && count($v) == 1) {
102
                            $output[$t] = $v[0];
103
                        }
104
                    }
105
                    if (empty($output)) {
106
                        //for empty nodes
107
                        $output = '';
108
                    }
109
                }
110
                // loop through the attributes and collect them
111
                if ($node->attributes->length) {
112
                    $a = [];
113
                    foreach ($node->attributes as $attrName => $attrNode) {
114
                        $a[$attrName] = $attrNode->value;
115
                    }
116
                    // if its an leaf node, store the value in @value instead of directly storing it.
117
                    if (!is_array($output)) {
118
                        $output = ['@value' => $output];
119
                    }
120
                    $output['@attributes'] = $a;
121
                }
122
                break;
123
        }
124
        return $output;
125
    }
126
    /**
127
     * Get the root XML node, if there isn't one, create it.
128
     *
129
     * @return \DOMDocument
130
     */
131
    private static function getXMLRoot()
132
    {
133
        if (empty(self::$xml)) {
134
            self::init();
135
        }
136
        return self::$xml;
137
    }
138
}
139