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
|
|
|
use SimpleXMLElement; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class XML. |
27
|
|
|
*/ |
28
|
|
|
class XML |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* XML to array. |
32
|
|
|
* |
33
|
|
|
* @param string $xml XML string |
34
|
|
|
* |
35
|
|
|
* @return array|\SimpleXMLElement |
36
|
|
|
*/ |
37
|
|
|
public static function parse($xml) |
38
|
|
|
{ |
39
|
|
|
return self::normalize(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* XML encode. |
44
|
|
|
* |
45
|
|
|
* @param mixed $data |
46
|
|
|
* @param string $root |
47
|
|
|
* @param string $item |
48
|
|
|
* @param string $attr |
49
|
|
|
* @param string $id |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public static function build( |
54
|
|
|
$data, |
55
|
|
|
$root = 'xml', |
56
|
|
|
$item = 'item', |
57
|
|
|
$attr = '', |
58
|
|
|
$id = 'id' |
59
|
|
|
) { |
60
|
|
|
if (is_array($attr)) { |
61
|
|
|
$_attr = []; |
62
|
|
|
|
63
|
|
|
foreach ($attr as $key => $value) { |
64
|
|
|
$_attr[] = "{$key}=\"{$value}\""; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$attr = implode(' ', $_attr); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$attr = trim($attr); |
71
|
|
|
$attr = empty($attr) ? '' : " {$attr}"; |
72
|
|
|
$xml = "<{$root}{$attr}>"; |
73
|
|
|
$xml .= self::data2Xml($data, $item, $id); |
74
|
|
|
$xml .= "</{$root}>"; |
75
|
|
|
|
76
|
|
|
return $xml; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Build CDATA. |
81
|
|
|
* |
82
|
|
|
* @param string $string |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function cdata($string) |
87
|
|
|
{ |
88
|
|
|
return sprintf('<![CDATA[%s]]>', $string); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Object to array. |
93
|
|
|
* |
94
|
|
|
* @param string $data |
|
|
|
|
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected static function normalize($obj) |
99
|
|
|
{ |
100
|
|
|
$result = null; |
101
|
|
|
|
102
|
|
|
if (is_object($obj)) { |
103
|
|
|
$obj = get_object_vars($obj); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_array($obj)) { |
107
|
|
|
foreach ($obj as $key => $value) { |
108
|
|
|
$res = self::normalize($value); |
109
|
|
|
if (($key === '@attributes') && ($key)) { |
110
|
|
|
$result = $res; |
111
|
|
|
} else { |
112
|
|
|
$result[$key] = $res; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
$result = $obj; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Array to XML. |
124
|
|
|
* |
125
|
|
|
* @param array $data |
126
|
|
|
* @param string $item |
127
|
|
|
* @param string $id |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected static function data2Xml($data, $item = 'item', $id = 'id') |
132
|
|
|
{ |
133
|
|
|
$xml = $attr = ''; |
134
|
|
|
|
135
|
|
|
foreach ($data as $key => $val) { |
136
|
|
|
if (is_numeric($key)) { |
137
|
|
|
$id && $attr = " {$id}=\"{$key}\""; |
138
|
|
|
$key = $item; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$xml .= "<{$key}{$attr}>"; |
142
|
|
|
|
143
|
|
|
if ((is_array($val) || is_object($val))) { |
144
|
|
|
$xml .= self::data2Xml((array) $val, $item, $id); |
145
|
|
|
} else { |
146
|
|
|
$xml .= is_numeric($val) ? $val : self::cdata($val); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$xml .= "</{$key}>"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $xml; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.