From ed6ec050d40616efbbb6f1ee871edeb25d39bbc4 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 25 Aug 2023 10:34:35 +0200 Subject: [PATCH] Detect attributes base-type --- .../AttributeBaseTypeConfigurator.php | 29 ++++++++++ .../Configurator/AttributeConfigurator.php | 1 + .../Converter/Types/Mapper/BaseTypeMapper.php | 58 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/Metadata/Converter/Types/Configurator/AttributeBaseTypeConfigurator.php create mode 100644 src/Metadata/Converter/Types/Mapper/BaseTypeMapper.php diff --git a/src/Metadata/Converter/Types/Configurator/AttributeBaseTypeConfigurator.php b/src/Metadata/Converter/Types/Configurator/AttributeBaseTypeConfigurator.php new file mode 100644 index 0000000..53d85d8 --- /dev/null +++ b/src/Metadata/Converter/Types/Configurator/AttributeBaseTypeConfigurator.php @@ -0,0 +1,29 @@ + $xsdType->getType(), + $xsdType instanceof AttributeRef => $xsdType->getReferencedAttribute()->getType(), + default => null, + }; + + return (new BaseTypeMapper())($engineType, $baseType, $context); + } +} diff --git a/src/Metadata/Converter/Types/Configurator/AttributeConfigurator.php b/src/Metadata/Converter/Types/Configurator/AttributeConfigurator.php index 28e1bf3..80aa338 100644 --- a/src/Metadata/Converter/Types/Configurator/AttributeConfigurator.php +++ b/src/Metadata/Converter/Types/Configurator/AttributeConfigurator.php @@ -22,6 +22,7 @@ public function __invoke(EngineType $engineType, mixed $xsdType, TypesConverterC static fn (EngineType $engineType): EngineType => (new DocsConfigurator())($engineType, $xsdType, $context), static fn (EngineType $engineType): EngineType => (new AttributeSingleConfigurator())($engineType, $xsdType, $context), static fn (EngineType $engineType): EngineType => (new AbstractAttributeItemConfigurator())($engineType, $xsdType, $context), + static fn (EngineType $engineType): EngineType => (new AttributeBaseTypeConfigurator())($engineType, $xsdType, $context), )( $engineType ->withMeta( diff --git a/src/Metadata/Converter/Types/Mapper/BaseTypeMapper.php b/src/Metadata/Converter/Types/Mapper/BaseTypeMapper.php new file mode 100644 index 0000000..6e5b709 --- /dev/null +++ b/src/Metadata/Converter/Types/Mapper/BaseTypeMapper.php @@ -0,0 +1,58 @@ +isBaseSchema($xsdType->getSchema())) { + return $engineType; + } + + do { + if ($result = $this->detectBaseType($engineType, $xsdType, $context)) { + return $result; + } + } while ($xsdType = $xsdType->getParent()?->getBase()); + + return $engineType; + } + + private function detectBaseType(EngineType $engineType, ?Type $xsdType, TypesConverterContext $contex): ?EngineType + { + if (!$xsdType) { + return $engineType; + } + + if ($contex->isBaseSchema($xsdType->getSchema())) { + return $engineType->withBaseType( + $xsdType->getName() ?: $engineType->getBaseType() + ); + } + + if ($xsdType instanceof SimpleType) { + if ($xsdType->getList()) { + return $engineType + ->withBaseType('array') + ->withMeta( + static fn (TypeMeta $meta): TypeMeta => $meta->withIsList(true) + ); + } + + if ($xsdType->getUnions()) { + return $engineType + ->withBaseType('mixed'); + } + } + + return null; + } +}