Skip to content

Commit

Permalink
PHP compatibility to avoid parse errors in supported versions
Browse files Browse the repository at this point in the history
  • Loading branch information
wol-soft committed Oct 6, 2023
1 parent ba50f98 commit 9884141
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/SchemaProcessor/PostProcessor/EnumPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,20 @@ public function postProcess(): void
parent::postProcess();
}

/**
* @throws SchemaException
*/
private function validateEnum(PropertyInterface $property): bool
{
$throw = fn (string $message) => throw new SchemaException(
sprintf(
$message,
$property->getName(),
$property->getJsonSchema()->getFile()
)
);
$throw = function (string $message) use ($property): void {
throw new SchemaException(
sprintf(
$message,
$property->getName(),
$property->getJsonSchema()->getFile()
)
);
};

$json = $property->getJsonSchema()->getJson();

Expand All @@ -168,7 +173,7 @@ private function validateEnum(PropertyInterface $property): bool
|| count(array_uintersect(
$json['enum-map'],
$json['enum'],
fn($a, $b) => $a === $b ? 0 : 1
function ($a, $b): int { return $a === $b ? 0 : 1; }
)) !== count($json['enum'])
)
) {
Expand Down Expand Up @@ -200,6 +205,12 @@ private function renderEnum(
$cases[ucfirst($map ? array_search($value, $map) : $value)] = var_export($value, true);
}

$backedType = null;
switch ($this->getArrayTypes($values)) {
case ['string']: $backedType = 'string'; break;
case ['integer']: $backedType = 'int'; break;
}

file_put_contents(
$this->targetDirectory . DIRECTORY_SEPARATOR . $name . '.php',
$this->renderer->renderTemplate(
Expand All @@ -208,11 +219,7 @@ private function renderEnum(
'namespace' => $this->namespace,
'name' => $name,
'cases' => $cases,
'backedType' => match ($this->getArrayTypes($values)) {
['string'] => 'string',
['integer'] => 'int',
default => null,
},
'backedType' => $backedType,
]
)
);
Expand Down
1 change: 1 addition & 0 deletions tests/PostProcessor/EnumPostProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
use ReflectionEnum;

// TODO: mixed enums, multiple enums, enum redirect
class EnumPostProcessorTest extends AbstractPHPModelGeneratorTest
{
/**
Expand Down

0 comments on commit 9884141

Please sign in to comment.