-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Read additional meta-information from special soap-enc types
- Loading branch information
Showing
36 changed files
with
812 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/Metadata/Converter/Types/Configurator/SoapEnc/Soap11ArrayConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\WsdlReader\Metadata\Converter\Types\Configurator\SoapEnc; | ||
|
||
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle; | ||
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType; | ||
use GoetasWebservices\XML\XSDReader\Schema\Type\Type as XsdType; | ||
use Psl\Option\Option; | ||
use Soap\Engine\Metadata\Model\TypeMeta; | ||
use Soap\Engine\Metadata\Model\XsdType as MetaType; | ||
use Soap\WsdlReader\Metadata\Converter\Types\Detector\AttributeMetaInformationDetector; | ||
use Soap\WsdlReader\Metadata\Converter\Types\Detector\NamedAttributesDetector; | ||
use Soap\WsdlReader\Metadata\Converter\Types\SoapEnc\ArrayTypeInfo; | ||
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext; | ||
use Soap\WsdlReader\Metadata\Detector\Soap11ArrayDetector; | ||
use function Psl\Option\none; | ||
use function Psl\Option\some; | ||
use function Psl\Vec\filter_nulls; | ||
|
||
final class Soap11ArrayConfigurator | ||
{ | ||
public function __invoke(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): MetaType | ||
{ | ||
$base = $xsdType->getRestriction()?->getBase(); | ||
if (!$xsdType instanceof ComplexType || !$base instanceof ComplexType) { | ||
return $metaType; | ||
} | ||
|
||
$namespace = $base->getSchema()->getTargetNamespace(); | ||
$typeName = $base->getName(); | ||
if (!Soap11ArrayDetector::detect($namespace, $typeName)) { | ||
return $metaType; | ||
} | ||
|
||
return $this->parseFromElement($metaType, $xsdType, $context) | ||
->or($this->parseFromAttribute($metaType, $xsdType, $context)) | ||
->unwrapOr($metaType); | ||
} | ||
|
||
/** | ||
* @param ComplexType $xsdType | ||
* @return Option<MetaType> | ||
*/ | ||
private function parseFromElement(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): Option | ||
{ | ||
if (!$xsdType->getElements()) { | ||
return none(); | ||
} | ||
|
||
$element = $xsdType->getElements()[0]; | ||
if (!$element instanceof ElementSingle) { | ||
return none(); | ||
} | ||
|
||
$type = $element->getType(); | ||
$typeName = $type?->getName(); | ||
if (!$type || !$typeName) { | ||
return none(); | ||
} | ||
|
||
$namespace = $type->getSchema()->getTargetNamespace() ?? ''; | ||
$arrayTypeInfo = new ArrayTypeInfo( | ||
$context->knownNamespaces->lookupNameFromNamespace($namespace)->unwrap(), | ||
$typeName, | ||
'['.($element->getMax() > -1 ? (string) $element->getMax() : '').']' | ||
); | ||
|
||
return some( | ||
$this->applyArrayTypInfoToMeta($metaType, $arrayTypeInfo, $namespace, $element->getName()) | ||
); | ||
} | ||
|
||
/** | ||
* @return Option<MetaType> | ||
*/ | ||
private function parseFromAttribute(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): Option | ||
{ | ||
$attrs = (new NamedAttributesDetector())($xsdType); | ||
$arrayTypeResult = (new AttributeMetaInformationDetector())($attrs, 'arrayType', 'arrayType'); | ||
|
||
if ($arrayTypeResult->isNone()) { | ||
return none(); | ||
} | ||
|
||
$arrayType = $arrayTypeResult->unwrap(); | ||
$arrayTypeInfo = ArrayTypeInfo::parseSoap11($arrayType->getValue()); | ||
if (!$arrayTypeInfo->prefix) { | ||
$arrayTypeInfo = $arrayTypeInfo->withPrefix( | ||
$context->knownNamespaces->lookupNameFromNamespace( | ||
$arrayType->getContextSchema()->getTargetNamespace() ?? '' | ||
)->unwrap() | ||
); | ||
} | ||
$namespace = $context->knownNamespaces->lookupNamespaceFromName($arrayTypeInfo->prefix)->unwrap(); | ||
|
||
return some($this->applyArrayTypInfoToMeta($metaType, $arrayTypeInfo, $namespace)); | ||
} | ||
|
||
private function applyArrayTypInfoToMeta( | ||
MetaType $metaType, | ||
ArrayTypeInfo $arrayTypeInfo, | ||
string $namespace, | ||
string $arrayNodeName = 'item' | ||
): MetaType { | ||
return $metaType | ||
->withBaseType('array') | ||
->withMemberTypes([$arrayTypeInfo->type]) | ||
->withMeta( | ||
static fn (TypeMeta $meta): TypeMeta => $meta | ||
->withIsElement(true) | ||
->withIsSimple(false) | ||
->withIsList(true) | ||
->withIsAlias(true) | ||
->withMinOccurs(0) | ||
->withMaxOccurs($arrayTypeInfo->getMaxOccurs()) | ||
->withUnions( | ||
filter_nulls([ | ||
[ | ||
'type' => $arrayTypeInfo->type, | ||
'namespace' => $namespace, | ||
'isList' => false, | ||
] | ||
]) | ||
) | ||
->withArrayType([ | ||
'type' => $arrayTypeInfo->toString(), | ||
'itemType' => $arrayTypeInfo->itemType(), | ||
'namespace' => $namespace, | ||
]) | ||
->withArrayNodeName($arrayNodeName) | ||
); | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
src/Metadata/Converter/Types/Configurator/SoapEnc/Soap12ArrayConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\WsdlReader\Metadata\Converter\Types\Configurator\SoapEnc; | ||
|
||
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle; | ||
use GoetasWebservices\XML\XSDReader\Schema\MetaInformation; | ||
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType; | ||
use GoetasWebservices\XML\XSDReader\Schema\Type\Type as XsdType; | ||
use Psl\Option\Option; | ||
use Soap\Engine\Metadata\Model\TypeMeta; | ||
use Soap\Engine\Metadata\Model\XsdType as MetaType; | ||
use Soap\WsdlReader\Metadata\Converter\Types\Detector\AttributeMetaInformationDetector; | ||
use Soap\WsdlReader\Metadata\Converter\Types\Detector\NamedAttributesDetector; | ||
use Soap\WsdlReader\Metadata\Converter\Types\SoapEnc\ArrayTypeInfo; | ||
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext; | ||
use Soap\WsdlReader\Metadata\Detector\Soap12ArrayDetector; | ||
use function Psl\Option\none; | ||
use function Psl\Option\some; | ||
use function Psl\Vec\filter_nulls; | ||
|
||
final class Soap12ArrayConfigurator | ||
{ | ||
public function __invoke(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): MetaType | ||
{ | ||
$base = $xsdType->getRestriction()?->getBase(); | ||
if (!$xsdType instanceof ComplexType || !$base instanceof ComplexType) { | ||
return $metaType; | ||
} | ||
|
||
$namespace = $base->getSchema()->getTargetNamespace(); | ||
$typeName = $base->getName(); | ||
if (!Soap12ArrayDetector::detect($namespace, $typeName)) { | ||
return $metaType; | ||
} | ||
|
||
return $this->parseFromElement($metaType, $xsdType, $context) | ||
->or($this->parseFromAttribute($metaType, $xsdType, $context)) | ||
->unwrapOr($metaType); | ||
} | ||
|
||
/** | ||
* @param ComplexType $xsdType | ||
* @return Option<MetaType> | ||
*/ | ||
private function parseFromElement(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): Option | ||
{ | ||
if (!$xsdType->getElements()) { | ||
return none(); | ||
} | ||
|
||
$element = $xsdType->getElements()[0]; | ||
if (!$element instanceof ElementSingle) { | ||
return none(); | ||
} | ||
|
||
$type = $element->getType(); | ||
$typeName = $type?->getName(); | ||
if (!$type || !$typeName) { | ||
return none(); | ||
} | ||
|
||
$namespace = $type->getSchema()->getTargetNamespace() ?? ''; | ||
$arrayTypeInfo = new ArrayTypeInfo( | ||
$context->knownNamespaces->lookupNameFromNamespace($namespace)->unwrap(), | ||
$typeName, | ||
'['.($element->getMax() > -1 ? (string) $element->getMax() : '').']' | ||
); | ||
|
||
return some( | ||
$this->applyArrayTypInfoToMeta($metaType, $arrayTypeInfo, $namespace, $element->getName()) | ||
); | ||
} | ||
|
||
/** | ||
* @return Option<MetaType> | ||
*/ | ||
private function parseFromAttribute(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): Option | ||
{ | ||
$attrs = (new NamedAttributesDetector())($xsdType); | ||
|
||
$itemTypeResult = (new AttributeMetaInformationDetector())($attrs, 'itemType', 'itemType'); | ||
$arraySizeResult = (new AttributeMetaInformationDetector())($attrs, 'arraySize', 'arraySize'); | ||
|
||
if (!$itemTypeResult->isSome()) { | ||
return none(); | ||
} | ||
|
||
$itemType = $itemTypeResult->unwrap(); | ||
$arrayTypeInfo = ArrayTypeInfo::parseSoap12( | ||
$itemType->getValue(), | ||
$arraySizeResult->map(static fn (MetaInformation $meta): string => $meta->getValue())->unwrapOr('*') | ||
); | ||
|
||
if (!$arrayTypeInfo->prefix) { | ||
$arrayTypeInfo = $arrayTypeInfo->withPrefix( | ||
$context->knownNamespaces->lookupNameFromNamespace( | ||
$itemType->getContextSchema()->getTargetNamespace() ?? '' | ||
)->unwrap() | ||
); | ||
} | ||
$namespace = $context->knownNamespaces->lookupNamespaceFromName($arrayTypeInfo->prefix)->unwrap(); | ||
|
||
return some($this->applyArrayTypInfoToMeta($metaType, $arrayTypeInfo, $namespace)); | ||
} | ||
|
||
private function applyArrayTypInfoToMeta( | ||
MetaType $metaType, | ||
ArrayTypeInfo $arrayTypeInfo, | ||
string $namespace, | ||
string $arrayNodeName = 'item' | ||
): MetaType { | ||
return $metaType | ||
->withBaseType('array') | ||
->withMemberTypes([$arrayTypeInfo->type]) | ||
->withMeta( | ||
static fn (TypeMeta $meta): TypeMeta => $meta | ||
->withIsElement(true) | ||
->withIsSimple(false) | ||
->withIsList(true) | ||
->withIsAlias(true) | ||
->withMinOccurs(0) | ||
->withMaxOccurs($arrayTypeInfo->getMaxOccurs()) | ||
->withUnions( | ||
filter_nulls([ | ||
[ | ||
'type' => $arrayTypeInfo->type, | ||
'namespace' => $namespace, | ||
'isList' => false, | ||
] | ||
]) | ||
) | ||
->withArrayType([ | ||
'type' => $arrayTypeInfo->toString(), | ||
'itemType' => $arrayTypeInfo->itemType(), | ||
'namespace' => $namespace, | ||
]) | ||
->withArrayNodeName($arrayNodeName) | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Metadata/Converter/Types/Configurator/SoapEnc/SoapEncConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\WsdlReader\Metadata\Converter\Types\Configurator\SoapEnc; | ||
|
||
use GoetasWebservices\XML\XSDReader\Schema\Type\Type as XsdType; | ||
use Soap\Engine\Metadata\Model\XsdType as EngineType; | ||
use Soap\Engine\Metadata\Model\XsdType as MetaType; | ||
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext; | ||
use function Psl\Fun\pipe; | ||
|
||
final class SoapEncConfigurator | ||
{ | ||
public function __invoke(MetaType $metaType, XsdType $xsdType, TypesConverterContext $context): MetaType | ||
{ | ||
return pipe( | ||
static fn (MetaType $metaType): EngineType => (new Soap11ArrayConfigurator())($metaType, $xsdType, $context), | ||
static fn (MetaType $metaType): EngineType => (new Soap12ArrayConfigurator())($metaType, $xsdType, $context), | ||
)($metaType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.