forked from sitya/serializer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmllist.patch
223 lines (203 loc) · 11 KB
/
xmllist.patch
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
From ca953b1b6b88f7ba1a5d66cfd1aa4ea832770a10 Mon Sep 17 00:00:00 2001
From: Mauro Foti <[email protected]>
Date: Tue, 18 Feb 2014 17:20:13 +0100
Subject: [PATCH] XmlList and XmlMap Namespace support
Adding support for namespace param for XmlList and XmlMap Metadata
@XmlList(entry="event", inline = true, namespace="http://mynamespace.net" )
---
src/JMS/Serializer/Annotation/XmlCollection.php | 5 ++
.../Metadata/Driver/AnnotationDriver.php | 2 +
src/JMS/Serializer/Metadata/Driver/XmlDriver.php | 8 +++
src/JMS/Serializer/Metadata/Driver/YamlDriver.php | 8 +++
src/JMS/Serializer/XmlDeserializationVisitor.php | 66 +++++++++++++++++-----
5 files changed, 75 insertions(+), 14 deletions(-)
diff --git a/src/JMS/Serializer/Annotation/XmlCollection.php b/src/JMS/Serializer/Annotation/XmlCollection.php
index dd98dc5..243176a 100644
--- a/src/JMS/Serializer/Annotation/XmlCollection.php
+++ b/src/JMS/Serializer/Annotation/XmlCollection.php
@@ -29,4 +29,9 @@
* @var boolean
*/
public $inline = false;
+
+ /**
+ * @var string
+ */
+ public $namespace;
}
diff --git a/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php b/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php
index d74544f..2a4e303 100644
--- a/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php
+++ b/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php
@@ -169,11 +169,13 @@ public function loadMetadataForClass(\ReflectionClass $class)
$propertyMetadata->xmlCollection = true;
$propertyMetadata->xmlCollectionInline = $annot->inline;
$propertyMetadata->xmlEntryName = $annot->entry;
+ $propertyMetadata->xmlNamespace = $annot->namespace;
} elseif ($annot instanceof XmlMap) {
$propertyMetadata->xmlCollection = true;
$propertyMetadata->xmlCollectionInline = $annot->inline;
$propertyMetadata->xmlEntryName = $annot->entry;
$propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
+ $propertyMetadata->xmlNamespace = $annot->namespace;
} elseif ($annot instanceof XmlKeyValuePairs) {
$propertyMetadata->xmlKeyValuePairs = true;
} elseif ($annot instanceof XmlAttribute) {
diff --git a/src/JMS/Serializer/Metadata/Driver/XmlDriver.php b/src/JMS/Serializer/Metadata/Driver/XmlDriver.php
index e13cd08..58dd0c7 100644
--- a/src/JMS/Serializer/Metadata/Driver/XmlDriver.php
+++ b/src/JMS/Serializer/Metadata/Driver/XmlDriver.php
@@ -168,6 +168,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $path)
if (isset($colConfig->attributes()->{'entry-name'})) {
$pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
}
+
+ if (isset($colConfig->attributes()->namespace)) {
+ $pMetadata->xmlNamespace = (string) $colConfig->attributes()->namespace;
+ }
}
if (isset($pElem->{'xml-map'})) {
@@ -185,6 +189,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $path)
if (isset($colConfig->attributes()->{'key-attribute-name'})) {
$pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'};
}
+
+ if (isset($colConfig->attributes()->namespace)) {
+ $pMetadata->xmlNamespace = (string) $colConfig->attributes()->namespace;
+ }
}
if (isset($pElem->{'xml-element'})) {
diff --git a/src/JMS/Serializer/Metadata/Driver/YamlDriver.php b/src/JMS/Serializer/Metadata/Driver/YamlDriver.php
index efae975..3570afb 100644
--- a/src/JMS/Serializer/Metadata/Driver/YamlDriver.php
+++ b/src/JMS/Serializer/Metadata/Driver/YamlDriver.php
@@ -119,6 +119,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
if (isset($colConfig['entry_name'])) {
$pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
}
+
+ if (isset($colConfig['namespace'])) {
+ $pMetadata->xmlNamespace = (string) $colConfig['namespace'];
+ }
}
if (isset($pConfig['xml_map'])) {
@@ -136,6 +140,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
if (isset($colConfig['key_attribute_name'])) {
$pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
}
+
+ if (isset($colConfig['namespace'])) {
+ $pMetadata->xmlNamespace = (string) $colConfig['namespace'];
+ }
}
if (isset($pConfig['xml_element'])) {
diff --git a/src/JMS/Serializer/XmlDeserializationVisitor.php b/src/JMS/Serializer/XmlDeserializationVisitor.php
index c63819f..552fae1 100644
--- a/src/JMS/Serializer/XmlDeserializationVisitor.php
+++ b/src/JMS/Serializer/XmlDeserializationVisitor.php
@@ -74,6 +74,7 @@ public function prepare($data)
}
$doc = simplexml_load_string($data);
+
libxml_use_internal_errors($previous);
libxml_disable_entity_loader($previousEntityLoaderState);
@@ -145,7 +146,7 @@ public function visitArray($data, array $type, Context $context)
{
$entryName = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryName ? $this->currentMetadata->xmlEntryName : 'entry';
- if ( ! isset($data->$entryName)) {
+ if ( ! isset($data->$entryName) && ! isset($data[$entryName]) ) {
if (null === $this->result) {
return $this->result = array();
}
@@ -163,8 +164,16 @@ public function visitArray($data, array $type, Context $context)
$this->result = &$result;
}
- foreach ($data->$entryName as $v) {
- $result[] = $this->navigator->accept($v, $type['params'][0], $context);
+ if (is_object($data)){
+ foreach ($data->$entryName as $v) {
+ $result[] = $this->navigator->accept($v, $type['params'][0], $context);
+ }
+ }
+
+ if(isset($data[$entryName])){
+ foreach ($data[$entryName] as $v) {
+ $result[] = $this->navigator->accept($v, $type['params'][0], $context);
+ }
}
return $result;
@@ -180,13 +189,26 @@ public function visitArray($data, array $type, Context $context)
$this->result = &$result;
}
- foreach ($data->$entryName as $v) {
- if (!isset($v[$this->currentMetadata->xmlKeyAttribute])) {
- throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $this->currentMetadata->xmlKeyAttribute));
+ if (is_object($data)){
+ foreach ($data->$entryName as $v) {
+ if (!isset($v[$this->currentMetadata->xmlKeyAttribute])) {
+ throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $this->currentMetadata->xmlKeyAttribute));
+ }
+
+ $k = $this->navigator->accept($v[$this->currentMetadata->xmlKeyAttribute], $keyType, $context);
+ $result[$k] = $this->navigator->accept($v, $entryType, $context);
}
+ }
+
+ if(isset($data[$entryName])){
+ foreach ($data[$entryName] as $v) {
+ if (!isset($v[$this->currentMetadata->xmlKeyAttribute])) {
+ throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $this->currentMetadata->xmlKeyAttribute));
+ }
- $k = $this->navigator->accept($v[$this->currentMetadata->xmlKeyAttribute], $keyType, $context);
- $result[$k] = $this->navigator->accept($v, $entryType, $context);
+ $k = $this->navigator->accept($v[$this->currentMetadata->xmlKeyAttribute], $keyType, $context);
+ $result[$k] = $this->navigator->accept($v, $entryType, $context);
+ }
}
return $result;
@@ -224,9 +246,9 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
$nodes = $data->xpath('./@'.$attributeName);
if (!empty($nodes)) {
$v = (string) reset($nodes);
- $metadata->reflection->setValue($this->currentObject, $v);
+ $metadata->reflection->setValue($this->currentObject, $v);
}
-
+
} elseif (isset($data[$name])) {
$v = $this->navigator->accept($data[$name], $metadata->type, $context);
$metadata->reflection->setValue($this->currentObject, $v);
@@ -243,17 +265,33 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
}
if ($metadata->xmlCollection) {
- $enclosingElem = $data;
- if (!$metadata->xmlCollectionInline && isset($data->$name)) {
- $enclosingElem = $data->$name;
+
+ if ('' !== $namespace = (string) $metadata->xmlNamespace) {
+ $registeredNamespaces = $data->getDocNamespaces();
+ if (false === $prefix = array_search($namespace, $registeredNamespaces)) {
+ $prefix = uniqid('ns-');
+ $data->registerXPathNamespace($prefix, $namespace);
+ }
+ $elementName = ($prefix === '')?$name:$prefix.':'.$name;
+ $nodes = $data->xpath('./'.$elementName );
+ if (empty($nodes)) {
+ return;
+ }
+ $node = array($name => $nodes);
+ } else {
+ $node = $data;
+ if (!$metadata->xmlCollectionInline && isset($node->$name)) {
+ $node = $node->$name;
+ }
}
$this->setCurrentMetadata($metadata);
- $v = $this->navigator->accept($enclosingElem, $metadata->type, $context);
+ $v = $this->navigator->accept($node, $metadata->type, $context);
$this->revertCurrentMetadata();
$metadata->reflection->setValue($this->currentObject, $v);
return;
+
}
if ('' !== $namespace = (string) $metadata->xmlNamespace) {