|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the dingtalk. |
|
4
|
|
|
* User: Ilham Tahir <[email protected]> |
|
5
|
|
|
* This source file is subject to the MIT license that is bundled |
|
6
|
|
|
* with this source code in the file LICENSE. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Aplisin\DingTalk\Kernel\Support; |
|
10
|
|
|
|
|
11
|
|
|
use SimpleXMLElement; |
|
12
|
|
|
|
|
13
|
|
|
class XML |
|
14
|
|
|
{ |
|
15
|
|
|
public static function parse($xml) |
|
16
|
|
|
{ |
|
17
|
|
|
$backup = libxml_disable_entity_loader(true); |
|
18
|
|
|
|
|
19
|
|
|
$result = self::normalize( |
|
20
|
|
|
simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS) |
|
|
|
|
|
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
libxml_disable_entity_loader($backup); |
|
24
|
|
|
|
|
25
|
|
|
return $result; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $data |
|
30
|
|
|
* @param string $root |
|
31
|
|
|
* @param string $item |
|
32
|
|
|
* @param string $attr |
|
33
|
|
|
* @param string $id |
|
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)) { |
|
|
|
|
|
|
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
|
|
|
* @param $string |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function cdata($string) |
|
67
|
|
|
{ |
|
68
|
|
|
return sprintf('<![CDATA[%s]]>', $string); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Object to array. |
|
73
|
|
|
* |
|
74
|
|
|
* |
|
75
|
|
|
* @param SimpleXMLElement $obj |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected static function normalize($obj) |
|
80
|
|
|
{ |
|
81
|
|
|
$result = null; |
|
82
|
|
|
|
|
83
|
|
|
if (is_object($obj)) { |
|
84
|
|
|
$obj = (array) $obj; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (is_array($obj)) { |
|
88
|
|
|
foreach ($obj as $key => $value) { |
|
89
|
|
|
$res = self::normalize($value); |
|
90
|
|
|
if (('@attributes' === $key) && ($key)) { |
|
91
|
|
|
$result = $res; // @codeCoverageIgnore |
|
92
|
|
|
} else { |
|
93
|
|
|
$result[$key] = $res; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
|
|
$result = $obj; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected static function data2Xml($data, $item = 'item', $id = 'id') |
|
104
|
|
|
{ |
|
105
|
|
|
$xml = $attr = ''; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($data as $key => $val) { |
|
108
|
|
|
if (is_numeric($key)) { |
|
109
|
|
|
$id && $attr = " {$id}=\"{$key}\""; |
|
|
|
|
|
|
110
|
|
|
$key = $item; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$xml .= "<{$key}{$attr}>"; |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
if ((is_array($val) || is_object($val))) { |
|
116
|
|
|
$xml .= self::data2Xml((array) $val, $item, $id); |
|
117
|
|
|
} else { |
|
118
|
|
|
$xml .= is_numeric($val) ? $val : self::cdata($val); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$xml .= "</{$key}>"; |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $xml; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|