Skip to content

Commit f22bbc3

Browse files
authored
Merge pull request #16 from goetas-webservices/unqualified-ns
fix namespace handling for "unqualified elements" schema mode
2 parents a1e3d79 + cd22a93 commit f22bbc3

File tree

3 files changed

+209
-43
lines changed

3 files changed

+209
-43
lines changed

src/Jms/YamlConverter.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public function &visitElementDef(Schema $schema, ElementDef $element)
145145

146146
if ($schema->getTargetNamespace()) {
147147
$data["xml_root_namespace"] = $schema->getTargetNamespace();
148+
149+
if (!$schema->getElementsQualification() && !($element instanceof Element && $element->isQualified())) {
150+
$data["xml_root_name"] = "ns-" . substr(sha1($data["xml_root_namespace"]), 0, 8) . ":" . $data["xml_root_name"];
151+
}
148152
}
149153
$this->classes[spl_object_hash($element)]["class"] = &$class;
150154

@@ -361,7 +365,7 @@ private function visitAttribute(&$class, Schema $schema, AttributeItem $attribut
361365

362366
$property["xml_list"]["inline"] = false;
363367
$property["xml_list"]["entry_name"] = $itemOfArray->getName();
364-
if ($schema->getTargetNamespace()) {
368+
if ($schema->getTargetNamespace() && ($schema->getElementsQualification() || ($itemOfArray instanceof Element && $itemOfArray->isQualified()))) {
365369
$property["xml_list"]["entry_namespace"] = $schema->getTargetNamespace();
366370
}
367371
} else {
@@ -432,7 +436,8 @@ private function visitElement(&$class, Schema $schema, ElementItem $element, $ar
432436
$property["type"] = "array<" . $visited["type"] . ">";
433437
$property["xml_list"]["inline"] = false;
434438
$property["xml_list"]["entry_name"] = $itemOfArray->getName();
435-
if ($schema->getTargetNamespace()) {
439+
440+
if ($schema->getTargetNamespace() && ($schema->getElementsQualification() || ($element instanceof Element && $element->isQualified()))) {
436441
$property["xml_list"]["namespace"] = $schema->getTargetNamespace();
437442
}
438443
return $property;
@@ -453,14 +458,14 @@ private function visitElement(&$class, Schema $schema, ElementItem $element, $ar
453458

454459
$property["xml_list"]["inline"] = false;
455460
$property["xml_list"]["entry_name"] = $itemOfArray->getName();
456-
if ($schema->getTargetNamespace()) {
461+
if ($schema->getTargetNamespace() && ($schema->getElementsQualification() || ($element instanceof Element && $element->isQualified()))) {
457462
$property["xml_list"]["namespace"] = $schema->getTargetNamespace();
458463
}
459464
return $property;
460465
} elseif ($this->isArrayElement($element)) {
461466
$property["xml_list"]["inline"] = true;
462467
$property["xml_list"]["entry_name"] = $element->getName();
463-
if ($schema->getTargetNamespace()) {
468+
if ($schema->getTargetNamespace() && ($schema->getElementsQualification() || ($element instanceof Element && $element->isQualified()))) {
464469
$property["xml_list"]["namespace"] = $schema->getTargetNamespace();
465470
}
466471

tests/Converter/JMS/Xsd2JmsElementTest.php

+193-34
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testElementOfPrimitiveType($xsType, $phpName)
2222
$this->assertEquals(
2323
array(
2424
'Example\ElementOne' => array(
25-
'xml_root_name' => 'element-one',
25+
'xml_root_name' => 'ns-8ece61d2:element-one',
2626
'xml_root_namespace' => 'http://www.example.com',
2727
'properties' => array(
2828
'__value' => array(
@@ -62,7 +62,7 @@ public function testElementOfPrimitiveTypeAnon($xsType, $phpName)
6262
$this->assertEquals(
6363
array(
6464
'Example\\ElementOne' => array(
65-
'xml_root_name' => 'element-one',
65+
'xml_root_name' => 'ns-8ece61d2:element-one',
6666
'xml_root_namespace' => 'http://www.example.com',
6767
'properties' => array(
6868
'__value' => array(
@@ -97,23 +97,20 @@ public function testElementOfBaseType($xsType, $phpName)
9797

9898
$this->assertEquals(array(
9999
'Example\\ElementOne' => array(
100-
'xml_root_name' => 'element-one',
100+
'xml_root_name' => 'ns-8ece61d2:element-one',
101101
'xml_root_namespace' => 'http://www.example.com',
102-
'properties' =>
103-
array(
104-
'__value' =>
105-
array(
106-
'expose' => true,
107-
'xml_value' => true,
108-
'access_type' => 'public_method',
109-
'accessor' =>
110-
array(
111-
'getter' => 'value',
112-
'setter' => 'value',
113-
),
114-
'type' => 'GoetasWebservices\\Xsd\\XsdToPhp\\XMLSchema\\DateTime',
115-
),
116-
))
102+
'properties' => array(
103+
'__value' => array(
104+
'expose' => true,
105+
'xml_value' => true,
106+
'access_type' => 'public_method',
107+
'accessor' => array(
108+
'getter' => 'value',
109+
'setter' => 'value',
110+
),
111+
'type' => 'GoetasWebservices\\Xsd\\XsdToPhp\\XMLSchema\\DateTime',
112+
),
113+
))
117114
), $classes['Example\ElementOne']);
118115
}
119116

@@ -137,26 +134,188 @@ public function testElementOfBaseTypeAnon($xsType, $phpName)
137134

138135
$this->assertEquals(array(
139136
'Example\\ElementOne' => array(
140-
'xml_root_name' => 'element-one',
137+
'xml_root_name' => 'ns-8ece61d2:element-one',
141138
'xml_root_namespace' => 'http://www.example.com',
142-
'properties' =>
143-
array(
144-
'__value' =>
145-
array(
146-
'expose' => true,
147-
'xml_value' => true,
148-
'access_type' => 'public_method',
149-
'accessor' =>
150-
array(
151-
'getter' => 'value',
152-
'setter' => 'value',
153-
),
154-
'type' => 'GoetasWebservices\\Xsd\\XsdToPhp\\XMLSchema\\DateTime',
155-
),
139+
'properties' => array(
140+
'__value' => array(
141+
'expose' => true,
142+
'xml_value' => true,
143+
'access_type' => 'public_method',
144+
'accessor' => array(
145+
'getter' => 'value',
146+
'setter' => 'value',
147+
),
148+
'type' => 'GoetasWebservices\\Xsd\\XsdToPhp\\XMLSchema\\DateTime',
156149
),
150+
),
157151

158152
)
159153
), $classes['Example\ElementOne']);
160154

161155
}
162-
}
156+
157+
public function testUnqualifiedNsQualifiedElement()
158+
{
159+
$xsd = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
160+
<xs:schema version="1.0"
161+
targetNamespace="http://www.example.com"
162+
xmlns:tns="http://www.example.com"
163+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
164+
elementFormDefault="unqualified">
165+
166+
<xs:complexType name="childType">
167+
<xs:sequence>
168+
<xs:element name="id" type="xs:string"/>
169+
</xs:sequence>
170+
</xs:complexType>
171+
172+
<xs:element name="root">
173+
<xs:complexType>
174+
<xs:sequence>
175+
<xs:element name="child" type="tns:childType" maxOccurs="unbounded"/>
176+
<xs:element form="qualified" name="childRoot" type="tns:childType"/>
177+
</xs:sequence>
178+
</xs:complexType>
179+
</xs:element>
180+
</xs:schema>
181+
';
182+
$classes = $this->getClasses($xsd);
183+
184+
$expected = array(
185+
'Example\\Root' => array(
186+
'Example\\Root' => array(
187+
'xml_root_name' => 'ns-8ece61d2:root',
188+
'xml_root_namespace' => 'http://www.example.com',
189+
'properties' => array(
190+
'child' => array(
191+
'expose' => true,
192+
'access_type' => 'public_method',
193+
'serialized_name' => 'child',
194+
'accessor' => array(
195+
'getter' => 'getChild',
196+
'setter' => 'setChild',
197+
),
198+
'xml_list' => array(
199+
'inline' => true,
200+
'entry_name' => 'child',
201+
),
202+
'type' => 'array<Example\\ChildType>',
203+
),
204+
'childRoot' => array(
205+
'expose' => true,
206+
'access_type' => 'public_method',
207+
'serialized_name' => 'childRoot',
208+
'accessor' => array(
209+
'getter' => 'getChildRoot',
210+
'setter' => 'setChildRoot',
211+
),
212+
'type' => 'Example\\ChildType',
213+
'xml_element' => array(
214+
'namespace' => 'http://www.example.com',
215+
),
216+
),
217+
),
218+
),
219+
),
220+
'Example\\ChildType' => array(
221+
'Example\\ChildType' => array(
222+
'properties' => array(
223+
'id' => array(
224+
'expose' => true,
225+
'access_type' => 'public_method',
226+
'serialized_name' => 'id',
227+
'accessor' => array(
228+
'getter' => 'getId',
229+
'setter' => 'setId',
230+
),
231+
'type' => 'string',
232+
),
233+
),
234+
),
235+
),
236+
);
237+
238+
$this->assertEquals($expected, $classes);
239+
}
240+
241+
public function testUnqualifiedNs()
242+
{
243+
$xsd = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
244+
<xs:schema version="1.0"
245+
targetNamespace="http://www.example.com"
246+
xmlns:tns="http://www.example.com"
247+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
248+
elementFormDefault="unqualified">
249+
250+
<xs:complexType name="childType">
251+
<xs:sequence>
252+
<xs:element name="id" type="xs:string"/>
253+
</xs:sequence>
254+
</xs:complexType>
255+
256+
<xs:element name="root">
257+
<xs:complexType>
258+
<xs:sequence>
259+
<xs:element name="child" type="tns:childType" maxOccurs="unbounded"/>
260+
<xs:element name="childRoot" type="tns:childType"/>
261+
</xs:sequence>
262+
</xs:complexType>
263+
</xs:element>
264+
</xs:schema>
265+
';
266+
$classes = $this->getClasses($xsd);
267+
268+
$expected = array(
269+
'Example\\Root' => array(
270+
'Example\\Root' => array(
271+
'xml_root_name' => 'ns-8ece61d2:root',
272+
'xml_root_namespace' => 'http://www.example.com',
273+
'properties' => array(
274+
'child' => array(
275+
'expose' => true,
276+
'access_type' => 'public_method',
277+
'serialized_name' => 'child',
278+
'accessor' => array(
279+
'getter' => 'getChild',
280+
'setter' => 'setChild',
281+
),
282+
'xml_list' => array(
283+
'inline' => true,
284+
'entry_name' => 'child',
285+
),
286+
'type' => 'array<Example\\ChildType>',
287+
),
288+
'childRoot' => array(
289+
'expose' => true,
290+
'access_type' => 'public_method',
291+
'serialized_name' => 'childRoot',
292+
'accessor' => array(
293+
'getter' => 'getChildRoot',
294+
'setter' => 'setChildRoot',
295+
),
296+
'type' => 'Example\\ChildType',
297+
),
298+
),
299+
),
300+
),
301+
'Example\\ChildType' => array(
302+
'Example\\ChildType' => array(
303+
'properties' => array(
304+
'id' => array(
305+
'expose' => true,
306+
'access_type' => 'public_method',
307+
'serialized_name' => 'id',
308+
'accessor' => array(
309+
'getter' => 'getId',
310+
'setter' => 'setId',
311+
),
312+
'type' => 'string',
313+
),
314+
),
315+
),
316+
),
317+
);
318+
319+
$this->assertEquals($expected, $classes);
320+
}
321+
}

tests/Converter/JMS/Xsd2JmsGroupTest.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,19 @@ public function getMaxOccurs()
327327
public function testArray()
328328
{
329329
$content = '
330-
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://www.example.com">
330+
<xs:schema
331+
targetNamespace="http://www.example.com"
332+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
333+
xmlns:ex="http://www.example.com">
331334
<xs:complexType name="complexType-1">
332335
<xs:sequence>
333-
<xs:element name="strings" type="ex:ArrayOfStrings"></xs:element>
336+
<xs:element name="strings" type="ex:ArrayOfStrings"/>
334337
</xs:sequence>
335338
</xs:complexType>
336339
337340
<xs:complexType name="ArrayOfStrings">
338341
<xs:sequence>
339-
<xs:element name="string" type="xs:string" maxOccurs="unbounded" minOccurs="1"></xs:element>
342+
<xs:element name="string" type="xs:string" maxOccurs="unbounded" minOccurs="1"/>
340343
</xs:sequence>
341344
</xs:complexType>
342345
</xs:schema>
@@ -365,7 +368,6 @@ public function testArray()
365368
'xml_list' => array(
366369
'inline' => false,
367370
'entry_name' => 'string',
368-
'namespace' => 'http://www.example.com'
369371
)
370372
)
371373
)
@@ -568,7 +570,7 @@ public function testGeneralParts()
568570
$this->assertEquals(
569571
array(
570572
'Example\\Element1' => array(
571-
'xml_root_name' => 'element-1',
573+
'xml_root_name' => 'ns-8ece61d2:element-1',
572574
'xml_root_namespace' => 'http://www.example.com',
573575
'properties' => array(
574576
'__value' => array(

0 commit comments

Comments
 (0)