markbaker/enumhelper
Composer 安装命令:
composer require markbaker/enumhelper
包简介
A small library that provides Helper Traits for PHP 8.1 Enums
README 文档
README
This package provides a series of traits that allows you to:
-
RestorableFromName Trait
Create/restore a PHP 8.1 enum from a name string.
-
EnumValidatableCase
Validate an enum name from a name string.
-
CasesIndexedByName
Similar to the standard static
cases()method, but returns an associative array, indexed by the case names.
Installation
You can install the package via composer:
composer require markbaker/enumhelper
Usage
RestorableFromName Trait
In PHP 8.1, it is possible to create a backed enum from a value using the enum's from() or tryFrom() methods.
enum Suit: string { case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } $suit = Suit::Diamonds; $value = $suit->value; // Returns 'D' $newSuit = Suit::from($value);
The EnumHelper\EnumRestorableFromName trait provided in this library adds a fromName() method to any enum where you want to create an enum from its name, rather than from its value.
enum Suit: string { use EnumHelper\EnumRestorableFromName; case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } $suit = Suit::Diamonds; $suitName = $suit->name; // Returns 'Diamonds' $newSuit = Suit::fromName($suitName);
An invalid name will throw an exception. Note that names are case-sensitive.
This could be useful if you wanted to store the name in a database for readability (particularly appropriate for unbacked enums); then recreate the enum in the model when you load the database record.
This works with both backed and unbacked enums.
EnumValidatableCase Trait
Useful to validate if a name has been defined in the case set for an enum:
enum Suit: string { use EnumHelper\EnumValidatableCase; case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } $suit = Suit::Diamonds; $validCaseName = Suit::Hearts; $isCaseNameValid = Suit::isValidCase($validCaseName); // Returns boolean true $invalidCaseName = 'HeArTs'; $isCaseNameValid = Suit::isValidCase($invalidCaseName); // Returns boolean false
Note that names are case-sensitive.
This works with both backed and unbacked enums.
CasesIndexedByName Trait
While PHP 8.1+ Enums already provide a standard static cases() method to return a list of all cases defined for that enum:
enum Suit: string { use EnumHelper\EnumValidatableCase; case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } var_dump(Suit::cases());
which returns an enumerated array of the defined cases.
array(4) {
[0]=>
enum(Suit::Hearts)
[1]=>
enum(Suit::Diamonds)
[2]=>
enum(Suit::Clubs)
[3]=>
enum(Suit::Spades)
}
Using the CasesIndexedByName Trait and the related casesIndexedByName() method
enum Suit: string { use EnumHelper\CasesIndexedByName; case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } var_dump(Suit::casesIndexedByName());
which will return an associative array of the defined cases, where the array index is the case name.
array(4) {
["Hearts"]=>
enum(Suit::Hearts)
["Diamonds"]=>
enum(Suit::Diamonds)
["Clubs"]=>
enum(Suit::Clubs)
["Spades"]=>
enum(Suit::Spades)
}
This can be particularly useful if you filter the cases() list to return a subset of cases, and don't like the gaps in numeric sequence in the enumerated array.
enum Suit: string { use EnumHelper\CasesIndexedByName; public const RED = 'Red'; public const BLACK = 'Black'; case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; public function color(): string { return match($this) { self::Hearts, self::Diamonds => self::RED, self::Clubs, self::Spades => self::BLACK, }; } public static function red(): array { return array_filter( self::casesIndexedByName(), fn(self $suit) => $suit->color() === self::RED ); } public static function black(): array { return array_filter( self::casesIndexedByName(), fn(self $suit) => $suit->color() === self::BLACK ); } } var_dump(Suit::black());
will return
array(2) {
["Clubs"]=>
enum(Suit::Clubs)
["Spades"]=>
enum(Suit::Spades)
}
Changelog
Please see the CHANGELOG for more information on what has changed recently.
License
This library is released under the MIT License (MIT). Please see License File for more information.
markbaker/enumhelper 适用场景与选型建议
markbaker/enumhelper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.66k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2021 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「enum」 「trait」 「helper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 markbaker/enumhelper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 markbaker/enumhelper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 markbaker/enumhelper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
Datetime helpers for timezone handling
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Trait providing methods to set class properties with an array.
Library with classes to help you do batch.
统计信息
- 总下载量: 21.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-07