XML::data2Xml()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 10
nop 3
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Support;
4
5
use SimpleXMLElement;
6
7
/**
8
 * Class XML.
9
 */
10
class XML
11
{
12
    /**
13
     * XML to array.
14
     *
15
     * @param string $xml XML string
16
     *
17
     * @return array|\SimpleXMLElement
18
     */
19
    public static function parse($xml)
20
    {
21
        return self::normalize(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS));
22
    }
23
24
    /**
25
     * XML encode.
26
     *
27
     * @param mixed  $data
28
     * @param string $root
29
     * @param string $item
30
     * @param string $attr
31
     * @param string $id
32
     *
33
     * @return string
34
     */
35
    public static function build(
36
        $data,
37
        $root = 'xml',
38
        $item = 'item',
39
        $attr = '',
40
        $id = 'id'
41
    ) {
42
        if (is_array($attr)) {
43
            $_attr = [];
44
45
            foreach ($attr as $key => $value) {
46
                $_attr[] = "{$key}=\"{$value}\"";
47
            }
48
49
            $attr = implode(' ', $_attr);
50
        }
51
52
        $attr = trim($attr);
53
        $attr = empty($attr) ? '' : " {$attr}";
54
        $xml = "<{$root}{$attr}>";
55
        $xml .= self::data2Xml($data, $item, $id);
56
        $xml .= "</{$root}>";
57
58
        return $xml;
59
    }
60
61
    /**
62
     * Build CDATA.
63
     *
64
     * @param string $string
65
     *
66
     * @return string
67
     */
68
    public static function cdata($string)
69
    {
70
        return sprintf('<![CDATA[%s]]>', $string);
71
    }
72
73
    /**
74
     * Object to array.
75
     *
76
     *
77
     * @param SimpleXMLElement $obj
78
     *
79
     * @return array
80
     */
81
    protected static function normalize($obj)
82
    {
83
        $result = null;
84
85
        if (is_object($obj)) {
86
            $obj = (array) $obj;
87
        }
88
89
        if (is_array($obj)) {
90
            foreach ($obj as $key => $value) {
91
                $res = self::normalize($value);
92
                if (($key === '@attributes') && ($key)) {
93
                    $result = $res;
94
                } else {
95
                    $result[$key] = $res;
96
                }
97
            }
98
        } else {
99
            $result = $obj;
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * Array to XML.
107
     *
108
     * @param array  $data
109
     * @param string $item
110
     * @param string $id
111
     *
112
     * @return string
113
     */
114
    protected static function data2Xml($data, $item = 'item', $id = 'id')
115
    {
116
        $xml = $attr = '';
117
118
        foreach ($data as $key => $val) {
119
            if (is_numeric($key)) {
120
                $id && $attr = " {$id}=\"{$key}\"";
121
                $key = $item;
122
            }
123
124
            $xml .= "<{$key}{$attr}>";
125
126
            if ((is_array($val) || is_object($val))) {
127
                $xml .= self::data2Xml((array) $val, $item, $id);
128
            } else {
129
                $xml .= is_numeric($val) ? $val : self::cdata($val);
130
            }
131
132
            $xml .= "</{$key}>";
133
        }
134
135
        return $xml;
136
    }
137
}
138