xml.php ➔ xml2array()   F
last analyzed

Complexity

Conditions 31
Paths 100

Size

Total Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
nc 100
nop 3
dl 0
loc 107
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Fetch URL with fopen() && returns a well formed array like the structure of the xml-document.
5
 * If somethins goes wrong return an empty array.
6
 * @param string $url
7
 * @param int $get_attributes
8
 * @param string $priority
9
 * @return array
10
 */
11
function xmlUrl2array(string $url, int $get_attributes = 1, string $priority = 'tag') : array
12
{
13
    if (!function_exists('xml_parser_create')) {
14
        return array();
15
    }
16
17
    $contents = file_get_contents($url);
18
19
    return xml2array($contents, $get_attributes, $priority);
20
}
21
22
/**
23
 * Returns a well formed array like the structure of the xml-document
24
 * If somethins goes wrong return an empty array.
25
 * @param string $xml
26
 * @param int $get_attributes
27
 * @param string $priority
28
 * @return array
29
 */
30
function xml2array(string $xml, int $get_attributes = 1, string $priority = 'tag') : array
31
{
32
    if (!function_exists('xml_parser_create')) {
33
        return array();
34
    }
35
    if (!$xml) {
36
        return array();
37
    }
38
    $contents = $xml;
39
    $parser = xml_parser_create('');
40
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
41
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
42
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
43
    xml_parse_into_struct($parser, trim($contents), $xml_values);
44
    xml_parser_free($parser);
45
    if (empty($xml_values)) {
46
        return array();
47
    }
48
    $xml_array = array();
49
    $current = &$xml_array;
50
    $repeated_tag_index = array();
51
    foreach ($xml_values as $data) {
52
        unset ($attributes, $value);
53
        extract($data);
54
        $result = array();
55
        $attributes_data = array();
56
        if (isset ($value)) {
57
            if ($priority == 'tag') {
58
                $result = $value;
59
            } else {
60
                $result['value'] = $value;
61
            }
62
        }
63
        if (isset ($attributes) && $get_attributes) {
64
            foreach ($attributes as $attr => $val) {
65
                if ($priority == 'tag') {
66
                    $attributes_data[$attr] = $val;
67
                } else {
68
                    $result['attr'][$attr] = $val;
69
                } //Set all the attributes in a array called 'attr'
70
            }
71
        }
72
        if ($type == "open") {
73
            $parent[$level - 1] = &$current;
74
            if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
75
                $current[$tag] = $result;
76
                if (!empty($attributes_data)) {
77
                    $current[$tag . '_attr'] = $attributes_data;
78
                }
79
                $repeated_tag_index[$tag . '_' . $level] = 1;
80
                $current = &$current[$tag];
81
            } else {
82
                if (isset ($current[$tag][0])) {
83
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
84
                    $repeated_tag_index[$tag . '_' . $level]++;
85
                } else {
86
                    $current[$tag] = array(
87
                        $current[$tag],
88
                        $result
89
                    );
90
                    $repeated_tag_index[$tag . '_' . $level] = 2;
91
                    if (isset ($current[$tag . '_attr'])) {
92
                        $current[$tag]['0_attr'] = $current[$tag . '_attr'];
93
                        unset ($current[$tag . '_attr']);
94
                    }
95
                }
96
                $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
97
                $current = &$current[$tag][$last_item_index];
98
            }
99
        } elseif ($type == "complete") {
100
            if (!isset ($current[$tag])) {
101
                $current[$tag] = $result;
102
                $repeated_tag_index[$tag . '_' . $level] = 1;
103
                if ($priority == 'tag' && !empty($attributes_data)) {
104
                    $current[$tag . '_attr'] = $attributes_data;
105
                }
106
            } else {
107
                if (is_array($current[$tag]) && isset ($current[$tag][0])) {
108
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
109
                    if ($priority == 'tag' && $get_attributes && !empty($attributes_data)) {
110
                        $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
111
                    }
112
                    $repeated_tag_index[$tag . '_' . $level]++;
113
                } else {
114
                    $current[$tag] = array(
115
                        $current[$tag],
116
                        $result
117
                    );
118
                    $repeated_tag_index[$tag . '_' . $level] = 1;
119
                    if ($priority == 'tag' && $get_attributes) {
120
                        if (isset ($current[$tag . '_attr'])) {
121
                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];
122
                            unset ($current[$tag . '_attr']);
123
                        }
124
                        if (!empty($attributes_data)) {
125
                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
126
                        }
127
                    }
128
                    $repeated_tag_index[$tag . '_' . $level]++; //0 && 1 index is already taken
129
                }
130
            }
131
        } elseif ($type == 'close') {
132
            $current = &$parent[$level - 1];
133
        }
134
    }
135
    return $xml_array;
136
}
137
138
/**
139
 * @param array $arrData
140
 * @param string $rootXml
141
 * @return string
142
 */
143
function array2xml(array $arrData, string $rootXml = '<root></root>') : string
144
{
145
    // creating object of SimpleXMLElement
146
    $objXml = new SimpleXMLElement('<?xml version="1.0"?>' . ($rootXml ? $rootXml : '<root></root>'));
147
    array2SimpleXMLElement($arrData, $objXml);
148
    return trim($objXml->asXML());
149
}
150
151
/**
152
 * @param array $arrData
153
 * @param SimpleXMLElement $objXml
154
 */
155
function array2SimpleXMLElement(array $arrData, SimpleXMLElement $objXml)
156
{
157
    foreach ($arrData as $key => $value) {
158
        if (is_array($value)) {
159
            if (!is_numeric($key)) {
160
                $subnode = $objXml->addChild(htmlspecialchars("$key"));
161
                array2SimpleXMLElement($value, $subnode);
162
            } else {
163
                array2SimpleXMLElement($value, $objXml);
164
            }
165
        } else {
166
            $objXml->addChild(htmlspecialchars("$key"), htmlspecialchars("$value"));
167
        }
168
    }
169
}
170