|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Soap\WsdlReader\Metadata\Converter\Types\Configurator; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer; |
| 7 | +use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax; |
| 8 | +use Soap\Engine\Metadata\Collection\PropertyCollection; |
| 9 | +use Soap\Engine\Metadata\Model\Property; |
| 10 | +use Soap\Engine\Metadata\Model\TypeMeta; |
| 11 | +use Soap\Engine\Metadata\Model\XsdType; |
| 12 | +use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext; |
| 13 | +use function Psl\Fun\pipe; |
| 14 | +use function Psl\Vec\map; |
| 15 | + |
| 16 | +final readonly class ElementContainerConfigurator |
| 17 | +{ |
| 18 | + public function __invoke(PropertyCollection $properties, mixed $xsdType, TypesConverterContext $context): PropertyCollection |
| 19 | + { |
| 20 | + return $properties; |
| 21 | + if (!$xsdType instanceof ElementContainer) { |
| 22 | + return $properties; |
| 23 | + } |
| 24 | + |
| 25 | + $configure = pipe( |
| 26 | + fn (PropertyCollection $properties): PropertyCollection => $this->enhanceMinMaxSettings($properties, $xsdType), |
| 27 | + ); |
| 28 | + |
| 29 | + return $configure($properties); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param Closure(XsdType): XsdType $mapper |
| 34 | + */ |
| 35 | + private function mapPropertyTypes(PropertyCollection $properties, Closure $mapper): PropertyCollection |
| 36 | + { |
| 37 | + return new PropertyCollection( |
| 38 | + ...map( |
| 39 | + $properties, |
| 40 | + static fn (Property $property): Property => new Property( |
| 41 | + $property->getName(), |
| 42 | + $mapper($property->getType()) |
| 43 | + ) |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + private function enhanceMinMaxSettings(PropertyCollection $properties, mixed $xsdType): PropertyCollection |
| 49 | + { |
| 50 | + if (!$xsdType instanceof InterfaceSetMinMax) { |
| 51 | + return $properties; |
| 52 | + } |
| 53 | + |
| 54 | + //dd($xsdType); |
| 55 | + |
| 56 | + $containerMin = $xsdType->getMin(); |
| 57 | + $containerMax = $xsdType->getMax(); |
| 58 | + $forceNullable = $containerMin === 0 && $containerMax === 1; |
| 59 | + $forceList = $containerMax > 1 || $containerMax === -1; |
| 60 | + |
| 61 | + return $this->mapPropertyTypes($properties, static function (XsdType $type) use ( |
| 62 | + $containerMin, $containerMax, $forceList, $forceNullable |
| 63 | + ): XsdType { |
| 64 | + $meta = $type->getMeta(); |
| 65 | + $min = ($forceList || $forceNullable) ? 0 : $meta->minOccurs()->unwrapOr($containerMax); |
| 66 | + $max = $forceList ? -1 : $meta->maxOccurs()->unwrapOr($containerMax); |
| 67 | + $isNullable = $forceNullable || $meta->isNullable()->unwrapOr(false) || ($min === 0 && $max === 1); |
| 68 | + $isList = $forceList || $meta->isList()->unwrapOr(false) || ($max > 1 || $max === -1); |
| 69 | + |
| 70 | + //dump($type->getXmlTargetNodeName(), $meta->maxOccurs()); |
| 71 | + |
| 72 | + return $type |
| 73 | + ->withBaseType($isList ? 'array' : $type->getBaseType()) |
| 74 | + ->withMeta( |
| 75 | + static fn (TypeMeta $meta): TypeMeta => $meta |
| 76 | + ->withMinOccurs($containerMin) |
| 77 | + ->withMaxOccurs($containerMax) |
| 78 | + ->withIsNullable($isNullable) |
| 79 | + ->withIsList($isList) |
| 80 | + ->withIsRepeatingElement($isList) |
| 81 | + ); |
| 82 | + }); |
| 83 | + } |
| 84 | +} |
0 commit comments