Elao / PhpEnums

:nut_and_bolt: Extended PHP 8.1+ enums features & specific integrations with frameworks and libraries
MIT License
326 stars 27 forks source link

EnumCaster is broken with custom array constants #65

Closed marforon closed 6 years ago

marforon commented 6 years ago

We sometimes need to use custom public constants in our enums (e.g. for translations):

class CompanyLicenceGroup extends ReadableEnum implements TranslationSourceLocationContainer
{
    use ChoiceEnumTrait;

    public const FREE = 'free';
    public const CORPORATE = 'corporate';
    public const CUSTOM = 'custom';

    private const PUBLIC_TITLES = [
        self::FREE => 'Two Pizza Team',
        self::CORPORATE => 'Corporate',
        self::CUSTOM => 'Custom',
    ];

    public const REGISTRATION_LABELS = [
        self::FREE => 'Free version - max %people% people',
        self::CORPORATE => 'Corporate - max %people% users (30 day free trial)',
    ];

    /**
     * @return string[]
     */
    public static function choices(): array
    {
        return [
            self::FREE => 'Free',
            self::CORPORATE => 'Corporate',
            self::CUSTOM => 'Custom',
        ];
    }

    /**
     * Return an array of source locations.
     *
     * @return SourceLocation[]
     */
    public static function getTranslationSourceLocations(): array
    {
        return array_map(
            'Translation\Extractor\Model\SourceLocation::createHere',
            array_merge(array_keys(self::REGISTRATION_LABELS), self::PUBLIC_TITLES)
        );
    }
}

But when I want to dump the data, it will end up with this exception.

image

It points here: https://github.com/Elao/PhpEnums/blob/master/src/Bridge/Symfony/VarDumper/Caster/EnumCaster.php#L34

ogizanagi commented 6 years ago

Thanks for the report @marforon ! https://github.com/Elao/PhpEnums/pull/66 should fix it

marforon commented 6 years ago

Thanks for such a quick fix @ogizanagi ! It fixes the issue indeed :) image

ogizanagi commented 6 years ago

And released: https://github.com/Elao/PhpEnums/releases/tag/v1.5.2