neatous/multi-enum
最新稳定版本:1.0.0
Composer 安装命令:
composer require neatous/multi-enum
包简介
Multi enum support for the PHP 8.1+ enums.
README 文档
README
PHP multi enum
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 |
统计信息
- 总下载量: 4.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-09