-
-
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.
Merge pull request #10 from php-soap/attribute-base-types
Detect attributes base-types
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Metadata/Converter/Types/Configurator/AttributeBaseTypeConfigurator.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,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); | ||
} | ||
} |
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
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; | ||
} | ||
} |