XML::data2Xml()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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