-
-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an enum model describer (#1965)
* Create an enum model describer * Bump Api-Platform Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
- Loading branch information
Showing
7 changed files
with
78 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Nelmio\ApiDocBundle\ModelDescriber; | ||
|
||
use Nelmio\ApiDocBundle\Model\Model; | ||
use OpenApi\Annotations\Schema; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class EnumModelDescriber implements ModelDescriberInterface | ||
{ | ||
public function describe(Model $model, Schema $schema) | ||
{ | ||
$enumClass = $model->getType()->getClassName(); | ||
|
||
$enums = []; | ||
foreach ($enumClass::cases() as $enumCase) { | ||
$enums[] = $enumCase->value; | ||
} | ||
|
||
$schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) ? 'int' : 'string'; | ||
$schema->enum = $enums; | ||
} | ||
|
||
public function supports(Model $model): bool | ||
{ | ||
if (!function_exists('enum_exists')) { | ||
return false; | ||
} | ||
|
||
return Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType() | ||
&& enum_exists($model->getType()->getClassName()) | ||
&& is_subclass_of($model->getType()->getClassName(), \BackedEnum::class); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; | ||
|
||
class Article81 | ||
{ | ||
public function __construct( | ||
public readonly int $id, | ||
public readonly ArticleType81 $type, | ||
) { | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; | ||
|
||
enum ArticleType81: string | ||
{ | ||
case DRAFT = 'draft'; | ||
case FINAL = 'final'; | ||
} |
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