A bundle to provide some helper methods for PHP 8.1+ enums inspired by spatie/enum
- Replace any usages of
Bytes\EnumSerializerBundle\Request\EnumParameterConverter
with ValueResolvers. See https://symfony.com/doc/current/controller/value_resolver.html
- Replace the deprecated calls to
easyAdminChoices()
andformChoices()
withprovideFormChoices()
. - Upgrade any overridden versions of
provideFormChoices()
,getFormChoiceKey()
, andgetFormChoiceValue()
fromprotected
topublic
.
Change all classes that extend Bytes\EnumSerializerBundle\Enums\Enum
to be string backed enums, using the new
Bytes\EnumSerializerBundle\Enums\StringBackedEnumTrait
trait and implementing the new
Bytes\EnumSerializerBundle\Enums\StringBackedEnumInterface
interface.
-
Bytes\EnumSerializerBundle\Enums\StringBackedEnumTrait
provides the previousEnum
class methods that are still relevant and needed with the switch to enums, including ajsonSerializable()
method to keep serialization consistent. -
Bytes\EnumSerializerBundle\Enums\StringBackedEnumInterface
must be implemented (or\JsonSerializable
) in order to have the serializer properly return label/value as it did prior to 2.0. - Note:
Bytes\EnumSerializerBundle\Enums\StringBackedEnumInterface
extends bothBytes\EnumSerializerBundle\Enums\EasyAdminChoiceEnumInterface
andBytes\EnumSerializerBundle\Enums\FormChoiceEnumInterface
, which both automatically provide EasyAdminBundle and Symfony form compatible choice methods and are easily overloadable.
<?php
namespace Bytes\EnumSerializerBundle\Tests\Fixtures;
use Spatie\Enum\Enum;
/**
* @method static self streamChanged()
* @method static self userChanged()
*/
class ValuesEnum extends Enum
{
/**
* @return string[]
*/
protected static function values(): array
{
return [
'streamChanged' => 'stream',
'userChanged' => 'user',
];
}
}
<?php
namespace Bytes\EnumSerializerBundle\Tests\Fixtures;
use Bytes\EnumSerializerBundle\Enums\StringBackedEnumInterface;
use Bytes\EnumSerializerBundle\Enums\StringBackedEnumTrait;
enum ValuesEnum: string implements StringBackedEnumInterface
{
use StringBackedEnumTrait;
case streamChanged = 'stream';
case userChanged = 'user';
}
<?php
namespace Bytes\EnumSerializerBundle\Tests\Fixtures;
use Bytes\EnumSerializerBundle\Enums\StringBackedEnumInterface;
use Bytes\EnumSerializerBundle\Enums\StringBackedEnumTrait;
use JetBrains\PhpStorm\Deprecated;
enum ValuesEnum: string implements StringBackedEnumInterface
{
use StringBackedEnumTrait;
case streamChanged = 'stream';
case userChanged = 'user';
#[Deprecated(reason: 'since 1.7.0, use "%name%" instead.', replacement: '%class%::%name%')]
public static function streamChanged(): ValuesEnum
{
return ValuesEnum::streamChanged;
}
#[Deprecated(reason: 'since 1.7.0, use "%name%" instead.', replacement: '%class%::%name%')]
public static function userChanged(): ValuesEnum
{
return ValuesEnum::userChanged;
}
}
Note that the automated replacement will not remove the trailing ()
following the function
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
$ composer require mrgoodbytes8667/enum-serializer-bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require mrgoodbytes8667/enum-serializer-bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php
return [
// ...
Bytes\EnumSerializerBundle\BytesEnumSerializerBundle::class => ['all' => true],
];
Inspired by and based on the spatie/enum package.