1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Visitor\Xml; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Accessor\AccessorInterface; |
15
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
16
|
|
|
use Ivory\Serializer\Mapping\PropertyMetadataInterface; |
17
|
|
|
use Ivory\Serializer\Mapping\TypeMetadataInterface; |
18
|
|
|
use Ivory\Serializer\Visitor\AbstractVisitor; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author GeLo <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class XmlSerializationVisitor extends AbstractVisitor |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var AccessorInterface |
27
|
|
|
*/ |
28
|
|
|
private $accessor; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DOMDocument|null |
32
|
|
|
*/ |
33
|
|
|
private $document; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \DOMNode|null |
37
|
|
|
*/ |
38
|
|
|
private $node; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \DOMNode[] |
42
|
|
|
*/ |
43
|
|
|
private $stack; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $version; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $encoding; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $root; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $entry; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $entryAttribute; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param AccessorInterface $accessor |
72
|
|
|
* @param string $version |
73
|
|
|
* @param string $encoding |
74
|
|
|
* @param string $root |
75
|
|
|
* @param string $entry |
76
|
|
|
* @param string $entryAttribute |
77
|
|
|
*/ |
78
|
585 |
|
public function __construct( |
79
|
|
|
AccessorInterface $accessor, |
80
|
|
|
$version = '1.0', |
81
|
|
|
$encoding = 'UTF-8', |
82
|
|
|
$root = 'result', |
83
|
|
|
$entry = 'entry', |
84
|
|
|
$entryAttribute = 'key' |
85
|
|
|
) { |
86
|
585 |
|
$this->accessor = $accessor; |
87
|
585 |
|
$this->version = $version; |
88
|
585 |
|
$this->encoding = $encoding; |
89
|
585 |
|
$this->root = $root; |
90
|
585 |
|
$this->entry = $entry; |
91
|
585 |
|
$this->entryAttribute = $entryAttribute; |
92
|
585 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
111 |
|
public function prepare($data, ContextInterface $context) |
98
|
|
|
{ |
99
|
111 |
|
$this->document = null; |
100
|
111 |
|
$this->node = null; |
101
|
111 |
|
$this->stack = []; |
102
|
|
|
|
103
|
111 |
|
return parent::prepare($data, $context); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
9 |
|
public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context) |
110
|
|
|
{ |
111
|
9 |
|
return $this->visitText($data ? 'true' : 'false'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
51 |
|
public function visitData($data, TypeMetadataInterface $type, ContextInterface $context) |
118
|
|
|
{ |
119
|
51 |
|
return $this->visitText($data); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
60 |
|
public function visitString($data, TypeMetadataInterface $type, ContextInterface $context) |
126
|
|
|
{ |
127
|
60 |
|
$document = $this->getDocument(); |
128
|
60 |
|
$data = (string) $data; |
129
|
|
|
|
130
|
60 |
|
$node = $this->requireCData($data) |
131
|
40 |
|
? $document->createCDATASection($data) |
132
|
60 |
|
: $document->createTextNode($data); |
133
|
|
|
|
134
|
60 |
|
return $this->visitNode($node); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
111 |
|
public function getResult() |
141
|
|
|
{ |
142
|
111 |
|
$document = $this->getDocument(); |
143
|
|
|
|
144
|
111 |
|
if ($document->formatOutput) { |
145
|
111 |
|
$document->loadXML($document->saveXML()); |
146
|
74 |
|
} |
147
|
|
|
|
148
|
111 |
|
return $document->saveXML(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
84 |
|
protected function doVisitObjectProperty( |
155
|
|
|
$data, |
156
|
|
|
$name, |
157
|
|
|
PropertyMetadataInterface $property, |
158
|
|
|
ContextInterface $context |
159
|
|
|
) { |
160
|
84 |
|
$node = $this->createNode($name); |
161
|
84 |
|
$this->enterNodeScope($node); |
162
|
|
|
|
163
|
|
|
// FIXME - Detect errors |
164
|
84 |
|
$this->navigator->navigate( |
165
|
84 |
|
$this->accessor->getValue( |
166
|
56 |
|
$data, |
167
|
84 |
|
$property->hasAccessor() ? $property->getAccessor() : $property->getName() |
168
|
56 |
|
), |
169
|
56 |
|
$context, |
170
|
84 |
|
$property->getType() |
171
|
56 |
|
); |
172
|
|
|
|
173
|
84 |
|
$this->leaveNodeScope(); |
174
|
84 |
|
$this->visitNode($node); |
175
|
|
|
|
176
|
84 |
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
24 |
|
protected function doVisitArray($data, TypeMetadataInterface $type, ContextInterface $context) |
183
|
|
|
{ |
184
|
24 |
|
$this->getDocument(); |
185
|
|
|
|
186
|
24 |
|
foreach ($data as $key => $value) { |
187
|
12 |
|
$node = $this->createNode(is_string($key) ? $key : $this->entry); |
188
|
|
|
|
189
|
12 |
|
if (is_int($key)) { |
190
|
12 |
|
$node->setAttribute($this->entryAttribute, $key); |
191
|
8 |
|
} |
192
|
|
|
|
193
|
12 |
|
$this->enterNodeScope($node); |
194
|
12 |
|
$this->navigator->navigate($value, $context, $type->getOption('value')); |
195
|
12 |
|
$this->leaveNodeScope(); |
196
|
12 |
|
$this->visitNode($node); |
197
|
16 |
|
} |
198
|
24 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
54 |
|
private function visitText($data) |
204
|
|
|
{ |
205
|
54 |
|
return $this->visitNode($this->getDocument()->createTextNode((string) $data)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param \DOMNode $node |
210
|
|
|
* |
211
|
|
|
* @return \DOMNode |
212
|
|
|
*/ |
213
|
108 |
|
private function visitNode(\DOMNode $node) |
214
|
|
|
{ |
215
|
108 |
|
if ($this->node !== $node) { |
216
|
108 |
|
$this->node->appendChild($node); |
217
|
72 |
|
} |
218
|
|
|
|
219
|
108 |
|
return $node; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param \DOMNode $node |
224
|
|
|
*/ |
225
|
87 |
|
private function enterNodeScope(\DOMNode $node) |
226
|
|
|
{ |
227
|
87 |
|
$this->stack[] = $this->node; |
228
|
87 |
|
$this->node = $node; |
229
|
87 |
|
} |
230
|
|
|
|
231
|
87 |
|
private function leaveNodeScope() |
232
|
|
|
{ |
233
|
87 |
|
$this->node = array_pop($this->stack); |
234
|
87 |
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $data |
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
60 |
|
private function requireCData($data) |
242
|
|
|
{ |
243
|
60 |
|
return strpos($data, '<') !== false || strpos($data, '>') !== false || strpos($data, '&') !== false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return \DOMDocument |
248
|
|
|
*/ |
249
|
111 |
|
private function getDocument() |
250
|
|
|
{ |
251
|
111 |
|
return $this->document !== null ? $this->document : $this->document = $this->createDocument(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $name |
256
|
|
|
* |
257
|
|
|
* @return \DOMElement |
258
|
|
|
*/ |
259
|
87 |
|
private function createNode($name) |
260
|
|
|
{ |
261
|
87 |
|
return $this->getDocument()->createElement($name); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return \DOMDocument |
266
|
|
|
*/ |
267
|
111 |
|
private function createDocument() |
268
|
|
|
{ |
269
|
111 |
|
$document = new \DOMDocument($this->version, $this->encoding); |
270
|
111 |
|
$document->formatOutput = true; |
271
|
|
|
|
272
|
111 |
|
$this->node = $document->createElement($this->root); |
273
|
111 |
|
$document->appendChild($this->node); |
274
|
|
|
|
275
|
111 |
|
return $document; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|