forked from schmittjoh/JMSSerializerBundle
/
XmlSerializationVisitor.php
290 lines (230 loc) · 8.94 KB
/
XmlSerializationVisitor.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/*
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace JMS\SerializerBundle\Serializer;
use JMS\SerializerBundle\Exception\RuntimeException;
use JMS\SerializerBundle\Metadata\ClassMetadata;
use JMS\SerializerBundle\Metadata\PropertyMetadata;
use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
/**
* XmlSerializationVisitor.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class XmlSerializationVisitor extends AbstractSerializationVisitor
{
public $document;
private $navigator;
private $defaultRootName = 'result';
private $defaultVersion = '1.0';
private $defaultEncoding = 'UTF-8';
private $stack;
private $metadataStack;
private $currentNode;
private $currentMetadata;
private $hasValue;
public function setDefaultRootName($name)
{
$this->defaultRootName = $name;
}
public function setDefaultVersion($version)
{
$this->defaultVersion = $version;
}
public function setDefaultEncoding($encoding)
{
$this->defaultEncoding = $encoding;
}
public function setNavigator(GraphNavigator $navigator)
{
$this->navigator = $navigator;
$this->document = null;
$this->stack = new \SplStack;
$this->metadataStack = new \SplStack;
}
public function getNavigator()
{
return $this->navigator;
}
public function visitString($data, $type)
{
if (null === $this->document) {
$this->document = $this->createDocument(null, null, true);
$this->currentNode->appendChild($this->document->createCDATASection($data));
return;
}
return $this->document->createCDATASection($data);
}
public function visitBoolean($data, $type)
{
if (null === $this->document) {
$this->document = $this->createDocument(null, null, true);
$this->currentNode->appendChild($this->document->createTextNode($data ? 'true' : 'false'));
return;
}
return $this->document->createTextNode($data ? 'true' : 'false');
}
public function visitInteger($data, $type)
{
return $this->visitNumeric($data, $type);
}
public function visitDouble($data, $type)
{
return $this->visitNumeric($data, $type);
}
public function visitArray($data, $type)
{
if (null === $this->document) {
$this->document = $this->createDocument(null, null, true);
}
$entryName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlEntryName) ? $this->currentMetadata->xmlEntryName : 'entry';
$keyAttributeName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlKeyAttribute) ? $this->currentMetadata->xmlKeyAttribute : null;
foreach ($data as $k => $v) {
$entryNode = $this->document->createElement($entryName);
$this->currentNode->appendChild($entryNode);
$this->setCurrentNode($entryNode);
if (null !== $keyAttributeName) {
$entryNode->setAttribute($keyAttributeName, (string) $k);
}
if (null !== $node = $this->navigator->accept($v, null, $this)) {
$this->currentNode->appendChild($node);
}
$this->revertCurrentNode();
}
}
public function visitTraversable($data, $type)
{
return $this->visitArray($data, $type);
}
public function startVisitingObject(ClassMetadata $metadata, $data, $type)
{
if (null === $this->document) {
$this->document = $this->createDocument(null, null, false);
$this->document->appendChild($this->currentNode = $this->document->createElement($metadata->xmlRootName ?: $this->defaultRootName));
}
$this->hasValue = false;
foreach( $metadata->virtualPropertyMethods as $field => $method ) {
$element = $this->getDocument()->createElement($field);
$element->appendChild($this->getDocument()->createCDATASection( (string) $method->invoke($data)));
$this->getCurrentNode()->appendChild($element);
}
}
public function visitProperty(PropertyMetadata $metadata, $object)
{
$v = (null === $metadata->getter ? $metadata->reflection->getValue($object)
: $object->{$metadata->getter}());
if (null === $v) {
return;
}
if ($metadata->xmlAttribute) {
$node = $this->navigator->accept($v, null, $this);
if (!$node instanceof \DOMCharacterData) {
throw new RuntimeException(sprintf('Unsupported value for XML attribute. Expected character data, but got %s.', json_encode($v)));
}
$this->currentNode->setAttribute($this->namingStrategy->translateName($metadata), $node->nodeValue);
return;
}
if (($metadata->xmlValue && $this->currentNode->childNodes->length > 0)
|| (!$metadata->xmlValue && $this->hasValue)) {
throw new \RuntimeException(sprintf('If you make use of @XmlValue, all other properties in the class must have the @XmlAttribute annotation. Invalid usage detected in class %s.', $metadata->reflection->class));
}
if ($metadata->xmlValue) {
$this->hasValue = true;
$node = $this->navigator->accept($v, null, $this);
if (!$node instanceof \DOMCharacterData) {
throw new RuntimeException(sprintf('Unsupported value for property %s::$%s. Expected character data, but got %s.', $metadata->reflection->class, $metadata->reflection->name, is_object($node) ? get_class($node) : gettype($node)));
}
$this->currentNode->appendChild($node);
return;
}
if ($addEnclosingElement = (!$metadata->xmlCollection || !$metadata->xmlCollectionInline) && !$metadata->inline) {
$element = $this->document->createElement($this->namingStrategy->translateName($metadata));
$this->setCurrentNode($element);
}
$this->setCurrentMetadata($metadata);
if (null !== $node = $this->navigator->accept($v, null, $this)) {
$this->currentNode->appendChild($node);
}
$this->revertCurrentMetadata();
if ($addEnclosingElement) {
$this->revertCurrentNode();
if ($element->hasChildNodes() || $element->hasAttributes()) {
$this->currentNode->appendChild($element);
}
}
}
public function endVisitingObject(ClassMetadata $metadata, $data, $type)
{
}
public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
{
// TODO
return false;
}
public function getResult()
{
return $this->document->saveXML();
}
public function getCurrentNode()
{
return $this->currentNode;
}
public function getCurrentMetadata()
{
return $this->currentMetadata;
}
public function getDocument()
{
return $this->document;
}
public function setCurrentMetadata(PropertyMetadata $metadata)
{
$this->metadataStack->push($this->currentMetadata);
$this->currentMetadata = $metadata;
}
public function setCurrentNode(\DOMNode $node)
{
$this->stack->push($this->currentNode);
$this->currentNode = $node;
}
public function revertCurrentNode()
{
return $this->currentNode = $this->stack->pop();
}
public function revertCurrentMetadata()
{
return $this->currentMetadata = $this->metadataStack->pop();
}
public function createDocument($version = null, $encoding = null, $addRoot = true)
{
$doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding);
$doc->formatOutput = true;
if ($addRoot) {
$this->setCurrentNode($rootNode = $doc->createElement($this->defaultRootName));
$doc->appendChild($rootNode);
}
return $doc;
}
private function visitNumeric($data, $type)
{
if (null === $this->document) {
$this->document = $this->createDocument(null, null, true);
$this->currentNode->appendChild($textNode = $this->document->createTextNode((string) $data));
return $textNode;
}
return $this->document->createTextNode((string) $data);
}
}