Skip to content

Commit da9a947

Browse files
authored
Merge pull request #7 from goetas-webservices/union-extension
handled buggy class extension
2 parents b283acb + 26e518d commit da9a947

File tree

4 files changed

+139
-16
lines changed

4 files changed

+139
-16
lines changed

src/Jms/YamlConverter.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,23 @@ private function visitAttribute(&$class, Schema $schema, AttributeItem $attribut
372372

373373
private function typeHasValue(Type $type, $parentClass, $name)
374374
{
375-
$collected = array();
376375
do {
376+
if (!($type instanceof SimpleType)) {
377+
return false;
378+
}
379+
377380
if ($alias = $this->getTypeAlias($type)) {
378381
return $alias;
379-
} else {
382+
}
380383

381-
if ($type->getName()) {
382-
$parentClass = $this->visitType($type);
383-
} else {
384-
$parentClass = $this->visitTypeAnonymous($type, $name, $parentClass);
385-
}
386-
$props = reset($parentClass);
387-
if (isset($props['properties']['__value']) && count($props['properties']) === 1) {
388-
return $props['properties']['__value']['type'];
389-
}
384+
if ($type->getName()) {
385+
$parentClass = $this->visitType($type);
386+
} else {
387+
$parentClass = $this->visitTypeAnonymous($type, $name, $parentClass);
388+
}
389+
$props = reset($parentClass);
390+
if (isset($props['properties']['__value']) && count($props['properties']) === 1) {
391+
return $props['properties']['__value']['type'];
390392
}
391393
} while (method_exists($type, 'getRestriction') && $type->getRestriction() && $type = $type->getRestriction()->getBase());
392394

src/Php/PhpConverter.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,42 @@ private function findPHPClass(PHPClass $class, Item $node, $force = false)
441441
if ($node instanceof ElementRef) {
442442
return $this->visitElementDef($node->getReferencedElement());
443443
}
444-
444+
if ($valueProp = $this->typeHasValue($node->getType(), $class, '')) {
445+
return $valueProp;
446+
}
445447
if (!$node->getType()->getName()) {
446448
return $this->visitTypeAnonymous($node->getType(), $node->getName(), $class);
447449
} else {
448450
return $this->visitType($node->getType(), $force);
449451
}
450452
}
453+
454+
private function typeHasValue(Type $type, PHPClass $parentClass, $name)
455+
{
456+
do {
457+
if (!($type instanceof SimpleType)) {
458+
return false;
459+
}
460+
461+
if ($alias = $this->getTypeAlias($type)) {
462+
return PHPClass::createFromFQCN($alias);
463+
}
464+
465+
if ($type->getName()) {
466+
$parentClass = $this->visitType($type);
467+
} else {
468+
$parentClass = $this->visitTypeAnonymous($type, $name, $parentClass);
469+
}
470+
471+
if ($prop = $parentClass->getPropertyInHierarchy('__value')) {
472+
return $prop->getType();
473+
}
474+
} while (
475+
(method_exists($type, 'getRestriction') && ($rest = $type->getRestriction()) && $type = $rest->getBase())
476+
||
477+
(method_exists($type, 'getUnions') && ($unions = $type->getUnions()) && $type = reset($unions))
478+
);
479+
480+
return false;
481+
}
451482
}

tests/Converter/JMS/Xsd2JmsGroupTest.php

+64-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,31 @@ public function testSomeAnonymous()
6262
</xs:sequence>
6363
</xs:complexType>
6464
</xs:element>
65+
<xs:element name="string3">
66+
<xs:simpleType>
67+
<xs:union memberTypes="xs:string xs:int"></xs:union>
68+
</xs:simpleType>
69+
</xs:element>
70+
<xs:element name="string4">
71+
<xs:simpleType>
72+
<xs:restriction base="ex:foo"></xs:restriction>
73+
</xs:simpleType>
74+
</xs:element>
75+
<xs:element name="string5">
76+
<xs:simpleType>
77+
<xs:union memberTypes="ex:foo"></xs:union>
78+
</xs:simpleType>
79+
</xs:element>
6580
</xs:sequence>
6681
<xs:attribute name="att">
6782
<xs:simpleType>
6883
<xs:restriction base="xs:string"></xs:restriction>
6984
</xs:simpleType>
7085
</xs:attribute>
7186
</xs:complexType>
87+
<xs:simpleType name="foo">
88+
<xs:restriction base="xs:string"></xs:restriction>
89+
</xs:simpleType>
7290
</xs:schema>
7391
';
7492
$classes = $this->getClasses($content);
@@ -117,7 +135,52 @@ public function testSomeAnonymous()
117135
'setter' => 'setString2'
118136
),
119137
'type' => 'Example\\ComplexType1Type\\String2AType'
120-
)
138+
),
139+
'string3' => array(
140+
'expose' => true,
141+
'access_type' => 'public_method',
142+
'serialized_name' => 'string3',
143+
/*
144+
'xml_element' => array(
145+
'namespace' => 'http://www.example.com'
146+
),
147+
*/
148+
'accessor' => array(
149+
'getter' => 'getString3',
150+
'setter' => 'setString3'
151+
),
152+
'type' => 'string'
153+
),
154+
'string4' => array(
155+
'expose' => true,
156+
'access_type' => 'public_method',
157+
'serialized_name' => 'string4',
158+
/*
159+
'xml_element' => array(
160+
'namespace' => 'http://www.example.com'
161+
),
162+
*/
163+
'accessor' => array(
164+
'getter' => 'getString4',
165+
'setter' => 'setString4'
166+
),
167+
'type' => 'string'
168+
),
169+
'string5' => array(
170+
'expose' => true,
171+
'access_type' => 'public_method',
172+
'serialized_name' => 'string5',
173+
/*
174+
'xml_element' => array(
175+
'namespace' => 'http://www.example.com'
176+
),
177+
*/
178+
'accessor' => array(
179+
'getter' => 'getString5',
180+
'setter' => 'setString5'
181+
),
182+
'type' => 'string'
183+
),
121184
)
122185
)
123186
), $classes['Example\\ComplexType1Type']);

tests/Converter/PHP/Xsd2PhpGroupTest.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,31 @@ public function testSomeAnonymous()
6060
</xs:sequence>
6161
</xs:complexType>
6262
</xs:element>
63+
<xs:element name="string3">
64+
<xs:simpleType>
65+
<xs:union memberTypes="xs:string xs:int"></xs:union>
66+
</xs:simpleType>
67+
</xs:element>
68+
<xs:element name="string4">
69+
<xs:simpleType>
70+
<xs:restriction base="ex:foo"></xs:restriction>
71+
</xs:simpleType>
72+
</xs:element>
73+
<xs:element name="string5">
74+
<xs:simpleType>
75+
<xs:union memberTypes="ex:foo"></xs:union>
76+
</xs:simpleType>
77+
</xs:element>
6378
</xs:sequence>
6479
<xs:attribute name="att">
6580
<xs:simpleType>
6681
<xs:restriction base="xs:string"></xs:restriction>
6782
</xs:simpleType>
6883
</xs:attribute>
6984
</xs:complexType>
85+
<xs:simpleType name="foo">
86+
<xs:restriction base="xs:string"></xs:restriction>
87+
</xs:simpleType>
7088
</xs:schema>
7189
';
7290
$classes = $this->getClasses($content);
@@ -76,13 +94,22 @@ public function testSomeAnonymous()
7694
$this->assertInstanceOf('GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPClass', $s2 = $classes['Example\ComplexType1Type\String2AType']);
7795

7896
$s1Prop = $complexType1->getProperty('string1');
79-
$this->assertSame('Example\ComplexType1Type\String1AType', $s1Prop->getType()->getFullName());
97+
$this->assertSame('\string', $s1Prop->getType()->getFullName());
8098

8199
$s2Prop = $complexType1->getProperty('string2');
82100
$this->assertSame($s2, $s2Prop->getType());
83101

102+
$s3Prop = $complexType1->getProperty('string3');
103+
$this->assertSame('\string', $s3Prop->getType()->getFullName());
104+
105+
$s4Prop = $complexType1->getProperty('string4');
106+
$this->assertSame('\string', $s4Prop->getType()->getFullName());
107+
108+
$s5Prop = $complexType1->getProperty('string5');
109+
$this->assertSame('\string', $s5Prop->getType()->getFullName());
110+
84111
$a1Prop = $complexType1->getProperty('att');
85-
$this->assertSame('Example\ComplexType1Type\AttAType', $a1Prop->getType()->getFullName());
112+
$this->assertSame('\string', $a1Prop->getType()->getFullName());
86113

87114
}
88115

@@ -265,4 +292,4 @@ public function testGeneralParts()
265292

266293
}
267294

268-
}
295+
}

0 commit comments

Comments
 (0)