forked from phpro/soap-client
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
287 additions
and
36 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
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
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
77 changes: 77 additions & 0 deletions
77
src/Phpro/SoapClient/CodeGenerator/EnumerationGenerator.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,77 @@ | ||
<?php | ||
|
||
namespace Phpro\SoapClient\CodeGenerator; | ||
|
||
use Laminas\Code\Generator\DocBlockGenerator; | ||
use Phpro\SoapClient\CodeGenerator\Model\Type; | ||
use Phpro\SoapClient\CodeGenerator\Util\Normalizer; | ||
use Laminas\Code\Generator\EnumGenerator\EnumGenerator; | ||
use Laminas\Code\Generator\FileGenerator; | ||
use Soap\Engine\Metadata\Model\XsdType; | ||
use function Psl\Dict\pull; | ||
use function Psl\Type\int; | ||
|
||
/** | ||
* @template-implements GeneratorInterface<Type> | ||
*/ | ||
class EnumerationGenerator implements GeneratorInterface | ||
{ | ||
/** | ||
* @param FileGenerator $file | ||
* @param Type $context | ||
* @return string | ||
*/ | ||
public function generate(FileGenerator $file, $context): string | ||
{ | ||
$file->setNamespace($context->getNamespace()); | ||
$file->setBody($this->generateBody($context)); | ||
|
||
return $file->generate(); | ||
} | ||
|
||
private function generateBody(Type $type): string | ||
{ | ||
$xsdType = $type->getXsdType(); | ||
$xsdMeta = $xsdType->getMeta(); | ||
$enumType = match ($xsdType->getBaseType()) { | ||
'int', 'integer' => 'int', | ||
default => 'string', | ||
}; | ||
|
||
$body = EnumGenerator::withConfig([ | ||
'name' => Normalizer::normalizeClassname($type->getName()), | ||
'backedCases' => [ | ||
'type' => $enumType, | ||
'cases' => $this->buildCases($xsdType, $enumType), | ||
] | ||
])->generate(); | ||
|
||
if ($docs = $xsdMeta->docs()->unwrapOr('')) { | ||
$docblock = (new DocBlockGenerator()) | ||
->setWordWrap(false) | ||
->setLongDescription($docs) | ||
->generate(); | ||
$body = $docblock . $body; | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
/** | ||
* @param 'string'|'int' $enumType | ||
* @return array<string, int|string> | ||
*/ | ||
private function buildCases(XsdType $xsdType, string $enumType): array | ||
{ | ||
$enums = $xsdType->getMeta()->enums()->unwrapOr([]); | ||
|
||
return pull( | ||
$enums, | ||
static fn(string $value): int|string => match ($enumType) { | ||
'int' => int()->coerce($value), | ||
'string' => $value, | ||
}, | ||
static fn(string $value): string => Normalizer::normalizeEnumCaseName($value) | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.