|
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
|
|
|
namespace EasyWeChat\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
|
24 |
|
public static function parse($xml) |
|
29
|
|
|
{ |
|
30
|
24 |
|
$backup = libxml_disable_entity_loader(true); |
|
31
|
|
|
|
|
32
|
24 |
|
$result = self::normalize(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS)); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
22 |
|
libxml_disable_entity_loader($backup); |
|
35
|
|
|
|
|
36
|
22 |
|
return $result; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* XML encode. |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $data |
|
43
|
|
|
* @param string $root |
|
44
|
|
|
* @param string $item |
|
45
|
|
|
* @param string $attr |
|
46
|
|
|
* @param string $id |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
11 |
|
public static function build( |
|
51
|
|
|
$data, |
|
52
|
|
|
$root = 'xml', |
|
53
|
|
|
$item = 'item', |
|
54
|
|
|
$attr = '', |
|
55
|
|
|
$id = 'id' |
|
56
|
|
|
) { |
|
57
|
11 |
|
if (is_array($attr)) { |
|
|
|
|
|
|
58
|
1 |
|
$_attr = []; |
|
59
|
|
|
|
|
60
|
1 |
|
foreach ($attr as $key => $value) { |
|
61
|
1 |
|
$_attr[] = "{$key}=\"{$value}\""; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$attr = implode(' ', $_attr); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
11 |
|
$attr = trim($attr); |
|
68
|
11 |
|
$attr = empty($attr) ? '' : " {$attr}"; |
|
|
|
|
|
|
69
|
11 |
|
$xml = "<{$root}{$attr}>"; |
|
|
|
|
|
|
70
|
11 |
|
$xml .= self::data2Xml($data, $item, $id); |
|
71
|
11 |
|
$xml .= "</{$root}>"; |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
11 |
|
return $xml; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Build CDATA. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $string |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
12 |
|
public static function cdata($string) |
|
84
|
|
|
{ |
|
85
|
12 |
|
return sprintf('<![CDATA[%s]]>', $string); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Object to array. |
|
90
|
|
|
* |
|
91
|
|
|
* |
|
92
|
|
|
* @param SimpleXMLElement $obj |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
22 |
|
protected static function normalize($obj) |
|
97
|
|
|
{ |
|
98
|
22 |
|
$result = null; |
|
99
|
|
|
|
|
100
|
22 |
|
if (is_object($obj)) { |
|
101
|
22 |
|
$obj = (array) $obj; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
22 |
|
if (is_array($obj)) { |
|
105
|
22 |
|
foreach ($obj as $key => $value) { |
|
106
|
20 |
|
$res = self::normalize($value); |
|
107
|
20 |
|
if (('@attributes' === $key) && ($key)) { |
|
108
|
|
|
$result = $res; // @codeCoverageIgnore |
|
109
|
|
|
} else { |
|
110
|
22 |
|
$result[$key] = $res; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} else { |
|
114
|
20 |
|
$result = $obj; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
22 |
|
return $result; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Array to XML. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $data |
|
124
|
|
|
* @param string $item |
|
125
|
|
|
* @param string $id |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
11 |
|
protected static function data2Xml($data, $item = 'item', $id = 'id') |
|
130
|
|
|
{ |
|
131
|
11 |
|
$xml = $attr = ''; |
|
132
|
|
|
|
|
133
|
11 |
|
foreach ($data as $key => $val) { |
|
134
|
11 |
|
if (is_numeric($key)) { |
|
135
|
2 |
|
$id && $attr = " {$id}=\"{$key}\""; |
|
|
|
|
|
|
136
|
2 |
|
$key = $item; |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
11 |
|
$xml .= "<{$key}{$attr}>"; |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
11 |
|
if ((is_array($val) || is_object($val))) { |
|
142
|
3 |
|
$xml .= self::data2Xml((array) $val, $item, $id); |
|
143
|
|
|
} else { |
|
144
|
11 |
|
$xml .= is_numeric($val) ? $val : self::cdata($val); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
11 |
|
$xml .= "</{$key}>"; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
11 |
|
return $xml; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|