1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
6
|
|
|
|
7
|
|
|
use KingsonDe\Marshal\Data\Collection; |
8
|
|
|
use KingsonDe\Marshal\Data\CollectionCallable; |
9
|
|
|
use KingsonDe\Marshal\Data\DataStructure; |
10
|
|
|
use KingsonDe\Marshal\Data\FlexibleData; |
11
|
|
|
use KingsonDe\Marshal\Exception\XmlDeserializeException; |
12
|
|
|
use KingsonDe\Marshal\Exception\XmlSerializeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @method static string serializeItem(AbstractMapper $mapper, ...$data) |
16
|
|
|
* @method static string serializeItemCallable(callable $mappingFunction, ...$data) |
17
|
|
|
*/ |
18
|
|
|
class MarshalXml extends Marshal { |
19
|
|
|
|
20
|
|
|
const ATTRIBUTES_KEY = '@attributes'; |
21
|
|
|
const DATA_KEY = '@data'; |
22
|
|
|
const CDATA_KEY = '@cdata'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected static $version = '1.0'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected static $encoding = 'UTF-8'; |
33
|
|
|
|
34
|
|
|
public static function setVersion(string $version) { |
35
|
|
|
static::$version = $version; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function setEncoding(string $encoding) { |
39
|
|
|
static::$encoding = $encoding; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param DataStructure $dataStructure |
44
|
|
|
* @return string |
45
|
|
|
* @throws \KingsonDe\Marshal\Exception\XmlSerializeException |
46
|
|
|
*/ |
47
|
|
|
public static function serialize(DataStructure $dataStructure) { |
48
|
|
|
if ($dataStructure instanceof Collection || $dataStructure instanceof CollectionCallable) { |
49
|
|
|
throw new XmlSerializeException('Collections in XML cannot be generated at root level.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$data = static::buildDataStructure($dataStructure); |
53
|
|
|
|
54
|
|
|
if (null === $data) { |
55
|
|
|
throw new XmlSerializeException('No data structure.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$xml = new \DOMDocument(static::$version, static::$encoding); |
60
|
|
|
|
61
|
|
|
static::processNodes($data, $xml); |
62
|
|
|
|
63
|
|
|
return $xml->saveXML(); |
64
|
|
|
} catch (\Exception $e) { |
65
|
|
|
throw new XmlSerializeException($e->getMessage(), $e->getCode(), $e); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function serializeCollection(AbstractMapper $mapper, ...$data) { |
70
|
|
|
throw new XmlSerializeException('Collections in XML cannot be generated at root level.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function serializeCollectionCallable(callable $mappingFunction, ...$data) { |
74
|
|
|
throw new XmlSerializeException('Collections in XML cannot be generated at root level.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $nodes |
79
|
|
|
* @param \DOMElement|\DOMDocument $parentXmlNode |
80
|
|
|
*/ |
81
|
|
|
protected static function processNodes(array $nodes, $parentXmlNode) { |
82
|
|
|
$dom = $parentXmlNode->ownerDocument ?? $parentXmlNode; |
83
|
|
|
|
84
|
|
|
foreach ($nodes as $name => $data) { |
85
|
|
|
$node = XmlNodeParser::parseNode($name, $data); |
86
|
|
|
|
87
|
|
|
// new node with scalar value |
88
|
|
|
if ($node->hasNodeValue()) { |
89
|
|
|
if ($node->isCData()) { |
90
|
|
|
$xmlNode = $dom->createElement($node->getName()); |
|
|
|
|
91
|
|
|
$cdataSection = $dom->createCDATASection($node->getNodeValue()); |
|
|
|
|
92
|
|
|
$xmlNode->appendChild($cdataSection); |
93
|
|
|
} else { |
94
|
|
|
$xmlNode = $dom->createElement($node->getName(), $node->getNodeValue()); |
95
|
|
|
} |
96
|
|
|
static::addAttributes($node, $xmlNode); |
97
|
|
|
$parentXmlNode->appendChild($xmlNode); |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// node collection of the same type |
102
|
|
|
if ($node->isCollection()) { |
103
|
|
|
static::processNodes($node->getChildrenNodes(), $parentXmlNode); |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// new node that might contain other nodes |
108
|
|
|
$xmlNode = $dom->createElement($node->getName()); |
109
|
|
|
static::addAttributes($node, $xmlNode); |
110
|
|
|
$parentXmlNode->appendChild($xmlNode); |
111
|
|
|
if ($node->hasChildrenNodes()) { |
112
|
|
|
static::processNodes($node->getChildrenNodes(), $xmlNode); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected static function addAttributes(XmlNode $node, \DOMElement $xmlNode) { |
118
|
|
|
foreach ($node->getAttributes() as $name => $value) { |
119
|
|
|
$xmlNode->setAttribute($name, $value); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $xml |
125
|
|
|
* @return FlexibleData |
126
|
|
|
* @throws \KingsonDe\Marshal\Exception\XmlDeserializeException |
127
|
|
|
*/ |
128
|
|
|
public static function deserializeXmlToData(string $xml): FlexibleData { |
129
|
|
|
try { |
130
|
|
|
$dom = new \DOMDocument(); |
131
|
|
|
$dom->loadXML($xml); |
132
|
|
|
$data = []; |
133
|
|
|
|
134
|
|
|
static::deserializeNodes($dom, $data); |
135
|
|
|
|
136
|
|
|
// get namespaces |
137
|
|
|
$xpath = new \DOMXPath($dom); |
138
|
|
|
foreach ($xpath->query('namespace::*') as $namespace) { |
139
|
|
|
if ($namespace->nodeName !== 'xmlns:xml') { |
140
|
|
|
$data[$dom->firstChild->nodeName][static::ATTRIBUTES_KEY][$namespace->nodeName] |
141
|
|
|
= $namespace->nodeValue; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
throw new XmlDeserializeException($e->getMessage(), $e->getCode(), $e); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return new FlexibleData($data); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param \DOMElement|\DOMDocument $parentXmlNode |
153
|
|
|
* @param array $data |
154
|
|
|
*/ |
155
|
|
|
protected static function deserializeNodes($parentXmlNode, array &$data) { |
156
|
|
|
$collectionNodeNames = []; |
157
|
|
|
|
158
|
|
|
foreach ($parentXmlNode->childNodes as $node) { |
159
|
|
|
if ($node instanceof \DOMText) { |
160
|
|
|
$data[static::DATA_KEY] = $node->textContent; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($node instanceof \DOMCdataSection) { |
164
|
|
|
$data[static::CDATA_KEY] = $node->data; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($node instanceof \DOMElement) { |
168
|
|
|
$value = []; |
169
|
|
|
|
170
|
|
|
if ($node->hasAttributes()) { |
171
|
|
|
foreach ($node->attributes as $attribute) { |
172
|
|
|
$value[static::ATTRIBUTES_KEY][$attribute->name] = $attribute->value; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// move node to collection if it exists twice |
177
|
|
|
if (isset($data[$node->nodeName])) { |
178
|
|
|
$previousValue = $data[$node->nodeName]; |
179
|
|
|
unset($data[$node->nodeName]); |
180
|
|
|
|
181
|
|
|
$data[][$node->nodeName] = $previousValue; |
182
|
|
|
$collectionNodeNames[$node->nodeName] = true; |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (isset($collectionNodeNames[$node->nodeName])) { |
187
|
|
|
$data[][$node->nodeName] = $value; |
188
|
|
|
|
189
|
|
|
if ($node->hasChildNodes()) { |
190
|
|
|
\end($data); |
191
|
|
|
static::deserializeNodes($node, $data[\key($data)][$node->nodeName]); |
192
|
|
|
} |
193
|
|
|
} else { |
194
|
|
|
$data[$node->nodeName] = $value; |
195
|
|
|
|
196
|
|
|
if ($node->hasChildNodes()) { |
197
|
|
|
static::deserializeNodes($node, $data[$node->nodeName]); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (isset($data[static::DATA_KEY], $data[static::ATTRIBUTES_KEY]) && \count($data) === 2) { |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (isset($data[static::DATA_KEY])) { |
208
|
|
|
if (\count($data) === 1) { |
209
|
|
|
$data = $data[static::DATA_KEY]; |
210
|
|
|
} else { |
211
|
|
|
unset($data[static::DATA_KEY]); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $xml |
218
|
|
|
* @param AbstractObjectMapper $mapper |
219
|
|
|
* @return mixed |
220
|
|
|
*/ |
221
|
|
|
public static function deserializeXml( |
222
|
|
|
string $xml, |
223
|
|
|
AbstractObjectMapper $mapper |
224
|
|
|
) { |
225
|
|
|
return $mapper->map(static::deserializeXmlToData($xml)); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $xml |
230
|
|
|
* @param callable $mappingFunction |
231
|
|
|
* @return mixed |
232
|
|
|
*/ |
233
|
|
|
public static function deserializeXmlCallable( |
234
|
|
|
string $xml, |
235
|
|
|
callable $mappingFunction |
236
|
|
|
) { |
237
|
|
|
return $mappingFunction(static::deserializeXmlToData($xml)); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.