neatous / multi-enum
Multi enum support for the PHP 8.1+ enums.
Installs: 1 912
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^8.6
- squizlabs/php_codesniffer: ^3.7
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 |