neatous/multi-enum

Multi enum support for the PHP 8.1+ enums.

1.0.0 2022-11-09 14:06 UTC

This package is not auto-updated.

Last update: 2024-06-06 19:38:33 UTC


README

Multi enum support for the PHP 8.1+ enums.

Usage

Basic example

Let's say we have a standard enumeration, e.g. for Suits:

<?php declare(strict_types = 1);

namespace Neatous\MultiEnum;

enum Suit: int
{
    case HEARTS = 1;
    case DIAMONDS = 2;
    case CLUBS = 4;
    case SPADES = 8;
}

Then it is easy to create a multi variant:

<?php declare(strict_types = 1);

namespace Neatous\MultiEnum;

/** @extends MultiEnum<Suit, int> */
class Suits extends MultiEnum
{
    public static function getEnumClass(): string
    {
        return Suit::class;
    }
}

Enum values mapping

If the single enum values do not match the multi enum values, you can map them to the multi enum values.

<?php declare(strict_types = 1);

namespace Neatous\MultiEnum;

/** @extends MultiEnum<Car, string> */
class Cars extends MultiEnum
{
    public static function getEnumClass(): string
    {
        return Car::class;
    }

    protected static function convertEnumValueToValue(string|int $enumValue): int
    {
        return match ($enumValue) {
            Car::AUDI->value => 1,
            Car::CITROEN->value => 2,
            Car::SKODA->value => 4,
            Car::VOLKSWAGEN->value => 8,
            default => throw new \Exception(
                sprintf('Mapping missing for the single enum value "%s".', $enumValue)
            ),
        };
    }
}

Versions

State Version PHP
stable 1.0.0 >=8.1
dev dev-master >=8.1