-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlJson.php
151 lines (130 loc) · 4.43 KB
/
XmlJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @property-read string $attributePrefix
* @property-read string $innerTextLable
*/
class XmlJson
{
private $attributePrefix;
private $innerTextLable;
private function json(SimpleXMLElement $xmlElement)
{
$result = array();
foreach ($xmlElement->children() as $child => $value) {
$result[$child][] = $this->json($value);
}
foreach ($result as $key => $value) {
if (count($result[$key]) == 1) {
$result[$key] = $value[0];
}
}
foreach ($xmlElement->attributes() as $attrName => $attrValue) {
$result[$this->attributePrefix . $attrName] = (string)$attrValue;
}
$text = (string)trim(str_replace("\n", "", $xmlElement));
if (!empty($text)) {
if (count($result) == 0) {
$result = $text;
} else {
$result[$this->innerTextLable] = $text;
}
}
if (is_array($result)) {
if (count($result) == 0) {
$result = null;
} else {
foreach ($result as $key => $value) {
if (count($result[$key]) == 0) {
$result[$key] = null;
}
}
}
}
return $result;
}
private function xml(array $jsonItem, SimpleXMLElement $root, SimpleXmlElement $parent)
{
if (array_key_exists($this->innerTextLable, $jsonItem)) {
$root[] = $jsonItem[$this->innerTextLable];
}
foreach ($jsonItem as $key => $value) {
if (is_array($value)) {
// $LOGGER_VARIABLES = $key;
// LOGGER(__FILE__, __METHOD__, __LINE__, function () use ($LOGGER_VARIABLES) {
// echo ($LOGGER_VARIABLES);
// });
if (is_numeric($key)) {
if ($key == 0) {
$this->xml($value, $root, $parent);
} else {
$child = $parent->addChild($root->getName());
$this->xml($value, $child, $parent);
}
} else {
$child = $root->addChild($key);
$this->xml($value, $child, $root);
}
} elseif ($key != $this->innerTextLable) {
if (is_numeric($key)) {
if ($key == 0) {
$root[0] = $value;
} else {
$parent->addChild($root->getName(), $value);
}
// } elseif ($key == $this->innerTextLable) {
// $root[] = $value;
} elseif (substr($key, 0, strlen($this->attributePrefix)) == $this->attributePrefix) {
$root->addAttribute(substr($key, strlen($this->attributePrefix)), $value);
} else {
$root->addChild($key, $value);
}
// $LOGGER_VARIABLES = [$key => $value, "Result" => str_replace("<", "<", $parent->asXML())];
// LOGGER(__FILE__, __METHOD__, __LINE__, function () use ($LOGGER_VARIABLES) {
// var_dump($LOGGER_VARIABLES);
// });
}
}
}
public function __construct($attributePrefix = "@", $innerTextLable = "#text")
{
$this->attributePrefix = $attributePrefix;
$this->innerTextLable = $innerTextLable;
}
public function __get($name)
{
switch ($name) {
case 'attributePrefix':
return $this->attributePrefix;
break;
case "innerTextLable":
return $this->innerTextLable;
break;
default:
# code...
break;
}
}
public function __set($name, $value)
{
}
/**
* @param array $json
* @param string $root
* @return SimpleXMLElement
*/
public function toXml(array $json, $root = "root")
{
$string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><$root></$root>";
$rootElement = new SimpleXMLElement($string);
$this->xml($json, $rootElement, $rootElement);
return $rootElement;
}
/**
* @param SimpleXMLElement $xml
* @return array
*/
public function toJson(SimpleXMLElement $xml)
{
return $this->json($xml);
}
}