Skip to content

Commit

Permalink
enum support fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dturopoli committed Apr 23, 2024
1 parent b483228 commit 6933824
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/Model/Component/Property/EnumPropertyDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@

class EnumPropertyDoc extends AbstractPropertyDoc
{
/**
* @var array<int, string>
*/
protected array $enumOptions = [];

/**
* @return string[]
* @return array<int, string>
*/
public function getEnumOptions(): array
{
return $this->enumOptions;
}

/**
* @param string[] $enumOptions
* @param array<int, string> $enumOptions
*/
public function setEnumOptions(array $enumOptions): self
{
Expand Down
13 changes: 8 additions & 5 deletions src/Service/DataTypeParser/EnumParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
use Valantic\PimcoreApiDocumentationBundle\Enum\DataTypeEnum;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\AbstractPropertyDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\EnumPropertyDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\SimplePropertyDoc;

/**
* @implements DataTypeParserInterface<SimplePropertyDoc>
* @implements DataTypeParserInterface<EnumPropertyDoc>
*/
class EnumParser implements DataTypeParserInterface
{
public function parse(\ReflectionProperty $reflectionProperty): AbstractPropertyDoc
{
$propertyDoc = new EnumPropertyDoc();

if (!$reflectionProperty->getType() instanceof \ReflectionNamedType) {
return $propertyDoc;
}

$propertyTypeName = $reflectionProperty->getType()->getName();

if (!is_subclass_of($propertyTypeName, \UnitEnum::class)) {
Expand All @@ -29,17 +32,17 @@ public function parse(\ReflectionProperty $reflectionProperty): AbstractProperty

foreach ($propertyTypeName::cases() as $enumCase) {
if ($enumCase instanceof \BackedEnum) {
$enumOptions[] = $enumCase->value;
$enumOptions[] = (string) $enumCase->value;
} else {
$enumOptions[] = $enumCase->name;
$enumOptions[] = (string) $enumCase->name;
}
}

$propertyDoc
->setName($reflectionProperty->getName())
->setType(DataTypeEnum::STRING->value)
->setEnumOptions($enumOptions)
->setNullable($reflectionProperty->getType()?->allowsNull() ?? true);
->setNullable($reflectionProperty->getType()->allowsNull() ?? true);

return $propertyDoc;
}
Expand Down

0 comments on commit 6933824

Please sign in to comment.