Skip to content

Commit

Permalink
fix: avoid ReflectionEnum usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Mar 13, 2023
1 parent a93573d commit df35718
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/JsonSchema/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,16 @@ private function getClassType(?string $className, bool $nullable, string $format
];
}
if (!$this->isResourceClass($className) && is_a($className, \BackedEnum::class, true)) {
$rEnum = new \ReflectionEnum($className);
$enumCases = array_map(static fn (\ReflectionEnumBackedCase $rCase) => $rCase->getBackingValue(), $rEnum->getCases());
$enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());

$type = \is_string($enumCases[0] ?? '') ? 'string' : 'int';

if ($nullable) {
$enumCases[] = null;
}

return [
'type' => (string) $rEnum->getBackingType(),
'type' => $type,
'enum' => $enumCases,
];
}
Expand Down

0 comments on commit df35718

Please sign in to comment.