Completed
Push — master ( 5ddcc0...9e0ead )
by Carlos
42s
created

XML::data2Xml()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 6.7272
cc 7
eloc 13
nc 10
nop 3
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * XML.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Support;
22
23
/**
24
 * Class XML.
25
 */
26
class XML
27
{
28
    /**
29
     * XML to array.
30
     *
31
     * @param string $xml XML string
32
     *
33
     * @return array
34
     *
35
     * @throws InvalidArgumentException
36
     */
37
    public static function parse($xml)
38
    {
39
        $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
40
41
        if (is_object($data) && get_class($data) === 'SimpleXMLElement') {
42
            $data = self::arrarval($data);
43
        }
44
45
        return $data;
46
    }
47
48
    /**
49
     * XML encode.
50
     *
51
     * @param mixed  $data
52
     * @param string $root
53
     * @param string $item
54
     * @param string $attr
55
     * @param string $id
56
     *
57
     * @return string
58
     */
59
    public static function build(
60
        $data,
61
        $root = 'xml',
62
        $item = 'item',
63
        $attr = '',
64
        $id = 'id'
65
    ) {
66
        if (is_array($attr)) {
67
            $_attr = [];
68
69
            foreach ($attr as $key => $value) {
70
                $_attr[] = "{$key}=\"{$value}\"";
71
            }
72
73
            $attr = implode(' ', $_attr);
74
        }
75
76
        $attr = trim($attr);
77
        $attr = empty($attr) ? '' : " {$attr}";
78
        $xml = "<{$root}{$attr}>";
79
        $xml  .= self::data2Xml($data, $item, $id);
80
        $xml  .= "</{$root}>";
81
82
        return $xml;
83
    }
84
85
    /**
86
     * Build CDATA.
87
     *
88
     * @param string $string
89
     *
90
     * @return string
91
     */
92
    public static function cdata($string)
93
    {
94
        return sprintf('<![CDATA[%s]]>', $string);
95
    }
96
97
    /**
98
     * Object to array.
99
     *
100
     * @param string $data
101
     *
102
     * @return array
103
     */
104
    private static function arrarval($data)
105
    {
106
        if (is_object($data) && get_class($data) === 'SimpleXMLElement') {
107
            $data = (array) $data;
108
        }
109
110
        if (is_array($data)) {
111
            foreach ($data as $index => $value) {
112
                $data[$index] = self::arrarval($value);
113
            }
114
        }
115
116
        return $data;
117
    }
118
119
    /**
120
     * Array to XML.
121
     *
122
     * @param array  $data
123
     * @param string $item
124
     * @param string $id
125
     *
126
     * @return string
127
     */
128
    private static function data2Xml($data, $item = 'item', $id = 'id')
129
    {
130
        $xml = $attr = '';
131
132
        foreach ($data as $key => $val) {
133
            if (is_numeric($key)) {
134
                $id && $attr = " {$id}=\"{$key}\"";
135
                $key = $item;
136
            }
137
138
            $xml .= "<{$key}{$attr}>";
139
140
            if ((is_array($val) || is_object($val))) {
141
                $xml .= self::data2Xml((array) $val, $item, $id);
142
            } else {
143
                $xml .= is_numeric($val) ? $val : self::cdata($val);
144
            }
145
146
            $xml .= "</{$key}>";
147
        }
148
149
        return $xml;
150
    }
151
}
152