mrgoodbytes8667 / enum-serializer-bundle
A bundle to enable spatie/enum Serialization support in Symfony
Installs: 27 843
Dependents: 8
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- illuminate/collections: ^9.13 | ^10.0 | ^11.0
- symfony/config: ^6.3 | ^7.0
- symfony/dependency-injection: ^6.3 | ^7.0
- symfony/framework-bundle: ^6.3 | ^7.0
- symfony/property-access: ^6.3 | ^7.0
- symfony/serializer: ^6.3 | ^7.0
- twig/twig: ^2.12 | ^3.0
Requires (Dev)
- ext-json: *
- fakerphp/faker: ^1.13
- rector/rector: ^0.14 | ^0.15 | ^0.16 | ^0.17
- symfony/test-pack: ^1.0.9
- symfony/var-dumper: ^6.3 | ^7.0
Conflicts
- phpunit/phpunit: <9.5
- dev-main
- 7.0.x-dev
- 6.0.x-dev
- 5.0.x-dev
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0-BETA1
- 4.2.x-dev
- 4.1.x-dev
- v4.1.0
- 4.0.x-dev
- v4.0.0
- v4.0.0-BETA1
- 3.2.x-dev
- v3.2.2
- v3.2.1
- v3.2.0
- 3.1.x-dev
- v3.1.1
- v3.1.0
- v3.1.0-RC2
- v3.1.0-RC1
- v3.1.0-BETA2
- v3.1.0-BETA1
- 3.0.x-dev
- v3.0.0
- v3.0.0-RC1
- v3.0.0-BETA3
- v3.0.0-BETA2
- v3.0.0-BETA1
- v3.0.0-ALPHA1
- 2.2.x-dev
- v2.2.0-BETA2
- v2.2.0-BETA1
- 2.1.x-dev
- v2.1.0
- v2.1.0-RC1
- v2.1.0-BETA2
- v2.1.0-BETA1
- 2.0.x-dev
- v2.0.0
- v2.0.0-RC2
- v2.0.0-RC1
- v2.0.0-BETA1
- 1.7.x-dev
- v1.7.0
- 1.6.x-dev
- v1.6.0
- 1.5.x-dev
- v1.5.0
- 1.4.x-dev
- v1.4.0
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.1
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-enumerates
This package is auto-updated.
Last update: 2024-12-20 18:23:14 UTC
README
A bundle to provide some helper methods for PHP 8.1+ enums inspired by spatie/enum
Upgrading to 6.0 from 5.x
- The EnumNormalizer will no longer be registered as a service by default. You must manually enable it to continue using it. It will be removed in v7.
Upgrading to 4.0 from 3.x
- Replace any usages of
Bytes\EnumSerializerBundle\Request\EnumParameterConverter
with ValueResolvers. See https://symfony.com/doc/current/controller/value_resolver.html
Upgrading to 3.0 from 2.x
- Replace the deprecated calls to
easyAdminChoices()
andformChoices()
withprovideFormChoices()
. - Upgrade any overridden versions of
provideFormChoices()
,getFormChoiceKey()
, andgetFormChoiceValue()
fromprotected
topublic
.
Upgrading to 2.0
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.
Before
<?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', ]; } }
After
<?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'; }
With Optional Deprecated Replacement for PHPStorm
<?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
Installation
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
$ composer require mrgoodbytes8667/enum-serializer-bundle
Applications that don't use Symfony Flex
Step 1: Download the 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
Step 2: Enable the 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], ];
License
Inspired by and based on the spatie/enum package.