mrgoodbytes8667/enum-serializer-bundle

A bundle to enable spatie/enum Serialization support in Symfony

v2.1.0-RC1 2022-06-30 14:14 UTC

README

Packagist Version PHP from Packagist Symfony Versions Supported Symfony Versions Tested Packagist License
GitHub Workflow Status GitHub Workflow Status codecov
A bundle to provide some helper methods for PHP 8.1+ enums inspired by spatie/enum

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 previous Enum class methods that are still relevant and needed with the switch to enums, including a jsonSerializable() 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 both Bytes\EnumSerializerBundle\Enums\EasyAdminChoiceEnumInterface and Bytes\EnumSerializerBundle\Enums\FormChoiceEnumInterfac, 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.