Completed
Push — master ( ad5348...129ed5 )
by Tian
06:22
created

XML::build()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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