PHP code example of neatous / multi-enum

1. Go to this page and download the library: Download neatous/multi-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neatous / multi-enum example snippets


 declare(strict_types = 1);

namespace Neatous\MultiEnum;

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

 declare(strict_types = 1);

namespace Neatous\MultiEnum;

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

 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)
            ),
        };
    }
}