Skip to content

Commit

Permalink
Merge pull request #10 from php-soap/attribute-base-types
Browse files Browse the repository at this point in the history
Detect attributes base-types
  • Loading branch information
veewee authored Aug 25, 2023
2 parents cb65f93 + ed6ec05 commit 879030c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

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

use GoetasWebservices\XML\XSDReader\Schema\Attribute\AbstractAttributeItem;
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeRef;
use Soap\Engine\Metadata\Model\XsdType as EngineType;
use Soap\WsdlReader\Metadata\Converter\Types\Mapper\BaseTypeMapper;
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext;

final class AttributeBaseTypeConfigurator
{
public function __invoke(EngineType $engineType, mixed $xsdType, TypesConverterContext $context): EngineType
{
if (!$xsdType instanceof AttributeItem) {
return $engineType;
}

$baseType = match (true) {
$xsdType instanceof AbstractAttributeItem => $xsdType->getType(),
$xsdType instanceof AttributeRef => $xsdType->getReferencedAttribute()->getType(),
default => null,
};

return (new BaseTypeMapper())($engineType, $baseType, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
58 changes: 58 additions & 0 deletions src/Metadata/Converter/Types/Mapper/BaseTypeMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

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

use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\Engine\Metadata\Model\XsdType as EngineType;
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext;

final class BaseTypeMapper
{
public function __invoke(EngineType $engineType, ?Type $xsdType, TypesConverterContext $context): EngineType
{
if (!$xsdType || $context->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;
}
}

0 comments on commit 879030c

Please sign in to comment.