Skip to content

Commit fbd4465

Browse files
committed
Enhanced internal element name detector
1 parent 665214d commit fbd4465

File tree

9 files changed

+97
-24
lines changed

9 files changed

+97
-24
lines changed

src/Metadata/Converter/Types/Configurator/ElementSingleConfigurator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Soap\WsdlReader\Metadata\Converter\Types\Configurator;
55

6+
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
7+
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementRef;
68
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle;
79
use Soap\Engine\Metadata\Model\TypeMeta;
810
use Soap\Engine\Metadata\Model\XsdType as EngineType;
@@ -17,7 +19,11 @@ public function __invoke(EngineType $engineType, mixed $xsdType, TypesConverterC
1719
}
1820

1921
// Elements can have inline types. Mark the attribute as a local type in that case.
20-
$innerType = $xsdType->getType();
22+
$innerType = match (true) {
23+
$xsdType instanceof ElementDef,
24+
$xsdType instanceof ElementRef => null,
25+
default => $xsdType->getType(),
26+
};
2127
$isConsideredAnInlineType = $innerType && $innerType->getName() === null;
2228
if ($isConsideredAnInlineType) {
2329
$engineType = $engineType->withMeta(

src/Metadata/Converter/Types/Detector/ElementTypeNameDetector.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Soap\WsdlReader\Metadata\Converter\Types\Detector;
44

5+
use GoetasWebservices\XML\XSDReader\Schema\Element\Any\Any;
56
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
7+
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementRef;
68
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle;
7-
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
89
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
910
use Soap\WsdlReader\Metadata\Converter\Types\ParentContext;
1011

@@ -14,18 +15,31 @@ public function __invoke(ElementItem $element, ParentContext $parentContext, ?st
1415
{
1516
$type = $element instanceof ElementSingle ? $element->getType() : null;
1617
$typeName = $calculatedTypeName ?? ($type?->getName() ?: $element->getName());
18+
$rootParent = $parentContext->rootParent();
1719

18-
// For inline simple types, we prefix the name of the element with the name of the parent type.
19-
if ($type instanceof SimpleType && !$type->getName()) {
20-
$parent = $parentContext->currentParent();
20+
// Add some conditions to validate if the element type name should be modified:
21+
if (
22+
$rootParent === $element // Dont enhance yourself
23+
|| $element instanceof Any // Any types are just objects. 'any' is a proper name here.
24+
|| $element instanceof ElementRef // Refs already have a proper name
25+
|| $type?->getName() // Named types already have a proper name
26+
) {
27+
return $typeName;
28+
}
29+
30+
// Make sure the root parent has a name:
31+
if (
32+
!$rootParent instanceof Type
33+
&& !$rootParent instanceof ElementItem
34+
) {
35+
return $typeName;
36+
}
2137

22-
if ($parent instanceof Type || $parent instanceof ElementItem) {
23-
if ($parentName = $parent->getName()) {
24-
$typeName = $parentName . ucfirst($typeName);
25-
}
26-
}
38+
$rootParentName = $rootParent->getName();
39+
if (!$rootParentName) {
40+
return $typeName;
2741
}
2842

29-
return $typeName;
43+
return $rootParentName . ucfirst($typeName);
3044
}
3145
}

src/Metadata/Converter/Types/Visitor/ElementVisitor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Soap\Engine\Metadata\Model\Type as EngineType;
99
use Soap\Engine\Metadata\Model\XsdType as MetaType;
1010
use Soap\WsdlReader\Metadata\Converter\Types\Configurator;
11+
use Soap\WsdlReader\Metadata\Converter\Types\Detector\ElementTypeNameDetector;
1112
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext;
1213
use function Psl\Fun\pipe;
1314

@@ -35,9 +36,11 @@ public function __invoke(AbstractElementSingle $element, TypesConverterContext $
3536
static fn (MetaType $metaType): MetaType => (new Configurator\ElementConfigurator())($metaType, $element, $context),
3637
);
3738

39+
$name = (new ElementTypeNameDetector())($element, $context->parent()->unwrap(), $element->getName());
40+
3841
return new TypeCollection(
3942
new EngineType(
40-
$configure(MetaType::guess($element->getName())),
43+
$configure(MetaType::guess($name)),
4144
(new PropertiesVisitor())($xsdType, $context)
4245
),
4346
...((new InlineElementTypeVisitor())($xsdType, $context))

tests/PhpCompatibility/schema036.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Methods:
2626
Types:
2727
> http://test-uri/:testType {
2828
int $int
29-
testType2 $testType2
29+
testTypeTestType2 $testType2
3030
}
31-
> http://test-uri/:testType2 {
31+
> http://test-uri/:testTypeTestType2 {
3232
int $int
3333
}

tests/PhpCompatibility/schema1002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Methods:
2424

2525
Types:
2626
> http://test-uri/:VoluntaryChangesType {
27-
?Penalty $Penalty
27+
?VoluntaryChangesTypePenalty $Penalty
2828
@?boolean $VolChangeInd
2929
}
30-
> http://test-uri/:Penalty {
30+
> http://test-uri/:VoluntaryChangesTypePenalty {
3131
@?string $PenaltyType
3232
@?string $DepartureStatus
3333
}

tests/PhpCompatibility/schema1003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Methods:
2727

2828
Types:
2929
> http://test-uri/:SpecialEquipPrefs {
30-
array<int<1, 15>, SpecialEquipPref> $SpecialEquipPref
30+
array<int<1, 15>, SpecialEquipPrefsSpecialEquipPref> $SpecialEquipPref
3131
}
32-
> http://test-uri/:SpecialEquipPref {
32+
> http://test-uri/:SpecialEquipPrefsSpecialEquipPref {
3333
@?string $Action
3434
}

tests/PhpCompatibility/schema1005.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ Methods:
3131

3232
Types:
3333
> http://test-uri/:LoyaltyTravelInfoType {
34-
?HotelStayInfo $HotelStayInfo
35-
?AirFlightInfo $AirFlightInfo
34+
?LoyaltyTravelInfoTypeHotelStayInfo $HotelStayInfo
35+
?LoyaltyTravelInfoTypeAirFlightInfo $AirFlightInfo
3636
}
37-
> http://test-uri/:HotelStayInfo {
37+
> http://test-uri/:LoyaltyTravelInfoTypeHotelStayInfo {
3838
?string $ReservationID
3939
}
40-
> http://test-uri/:AirFlightInfo {
40+
> http://test-uri/:LoyaltyTravelInfoTypeAirFlightInfo {
4141
?string $FlightSegment
4242
}

tests/PhpCompatibility/schema1007.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ Types:
4040
}
4141
> http://test-uri/:VerificationType {
4242
?string $Email
43-
?StartLocation $StartLocation
43+
?VerificationTypeStartLocation $StartLocation
4444
}
45-
> http://test-uri/:StartLocation extends LocationType {
45+
> http://test-uri/:VerificationTypeStartLocation extends LocationType {
4646
string $_
4747
@?string $LocationCode
4848
@?dateTime $AssociatedDateTime
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
SOAP XML Schema 1016: Group ref with minOccurs / MaxOccurs
3+
--FILE--
4+
<?php
5+
include __DIR__."/test_schema.inc";
6+
$schema = <<<EOF
7+
<complexType name="Code">
8+
<sequence>
9+
<element form="unqualified" name="code" type="string" />
10+
</sequence>
11+
</complexType>
12+
<simpleType name="SomeSpecificTypeCodeEnum">
13+
<restriction base="string">
14+
<enumeration value="foo" />
15+
<enumeration value="bar" />
16+
</restriction>
17+
</simpleType>
18+
<complexType name="SomeType">
19+
<choice>
20+
<element name="code">
21+
<complexType>
22+
<complexContent>
23+
<restriction base="tns:Code">
24+
<sequence>
25+
<element form="unqualified" name="code" type="tns:SomeSpecificTypeCodeEnum" />
26+
</sequence>
27+
</restriction>
28+
</complexContent>
29+
</complexType>
30+
</element>
31+
</choice>
32+
</complexType>
33+
EOF;
34+
test_schema($schema,'type="tns:Element"');
35+
?>
36+
--EXPECT--
37+
Methods:
38+
> test(Element $testParam): void
39+
40+
Types:
41+
> http://test-uri/:Code {
42+
string $code
43+
}
44+
> http://test-uri/:SomeSpecificTypeCodeEnum extends string in (foo|bar)
45+
> http://test-uri/:SomeType {
46+
?SomeTypeCode $code
47+
}
48+
> http://test-uri/:SomeTypeCode extends Code {
49+
?SomeSpecificTypeCodeEnum in (foo|bar) $code
50+
}

0 commit comments

Comments
 (0)