Completed
Push — master ( 847ee0...fd7167 )
by do
02:44
created

XML::cdata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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