Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix local types with parent type-name #41

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Also prefix inline element simple-type names
veewee committed Jan 2, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 77a255088ccc1274ab6c7ca0c9ce8f65c97d37f6
31 changes: 31 additions & 0 deletions src/Metadata/Converter/Types/Detector/ElementTypeNameDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

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

use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle;
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
use Soap\WsdlReader\Metadata\Converter\Types\ParentContext;

final class ElementTypeNameDetector
{
public function __invoke(ElementItem $element, ParentContext $parentContext, ?string $calculatedTypeName = null): string
{
$type = $element instanceof ElementSingle ? $element->getType() : null;
$typeName = $calculatedTypeName ?? ($type?->getName() ?: $element->getName());

// For inline simple types, we prefix the name of the element with the name of the parent type.
if ($type instanceof SimpleType && !$type->getName()) {
$parent = $parentContext->currentParent();

if ($parent instanceof Type || $parent instanceof ElementItem) {
if ($parentName = $parent->getName()) {
$typeName = $parentName . ucfirst($typeName);
}
}
}

return $typeName;
}
}
Original file line number Diff line number Diff line change
@@ -6,13 +6,13 @@
use GoetasWebservices\XML\XSDReader\Schema\Element\Choice;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle;
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
use GoetasWebservices\XML\XSDReader\Schema\Element\Sequence;
use Soap\Engine\Metadata\Collection\PropertyCollection;
use Soap\Engine\Metadata\Model\Property;
use Soap\Engine\Metadata\Model\XsdType as EngineType;
use Soap\WsdlReader\Metadata\Converter\Types\Configurator;
use Soap\WsdlReader\Metadata\Converter\Types\Detector\ElementTypeNameDetector;
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext;
use function Psl\Fun\pipe;
use function Psl\Vec\flat_map;
@@ -35,8 +35,7 @@ private function parseElementItem(ElementItem $element, TypesConverterContext $c
return $this->__invoke($element, $context);
}

$type = $element instanceof ElementSingle ? $element->getType() : null;
$typeName = $type?->getName() ?: $element->getName();
$typeName = (new ElementTypeNameDetector())($element, $context->parent()->unwrap());
$configure = pipe(
static fn (EngineType $engineType): EngineType => (new Configurator\ElementConfigurator())($engineType, $element, $context),
static fn (EngineType $engineType): EngineType => (new Configurator\AnyElementConfigurator())($engineType, $element, $context),
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema1013.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
SOAP XML Schema 1001: Prepend element name before attribute type names for more unique type-names.
SOAP XML Schema 1001: Prepend parent type name before attribute type names for more unique type-names.
--FILE--
<?php
include __DIR__."/test_schema.inc";
31 changes: 31 additions & 0 deletions tests/PhpCompatibility/schema1014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
SOAP XML Schema 1001: Prepend parent type name before local simple element type names for more unique type-names.
--FILE--
<?php
include __DIR__."/test_schema.inc";
$schema = <<<EOF
<element name="Element">
<complexType>
<sequence>
<element name="Enum">
<simpleType>
<restriction base="string">
<enumeration value="foo" />
<enumeration value="bar" />
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
</element>
EOF;
test_schema($schema,'type="tns:Element"');
?>
--EXPECT--
Methods:
> test(Element $testParam): void

Types:
> http://test-uri/:Element {
ElementEnum in (foo|bar) $Enum
}