fab2s/enumerate
Composer 安装命令:
composer require fab2s/enumerate
包简介
Enumerate, a nice boost to your enums
README 文档
README
A lightweight PHP library that makes native enums practical for real-world applications.
The Problem
PHP 8.1 enums are powerful but have limitations that create friction in production code:
- No
Stringablesupport - Can't use enums directly in string contexts - Strict type restrictions - Can't safely instantiate from mixed input (HTTP requests, databases)
- No inheritance - Can't share behavior across related enums
- Type mismatch pain -
IntBackedEnum::tryFrom('1')throws instead of returning null
The Solution
Enumerate provides a unified API to work with any enum type consistently:
// Safely instantiate from any input type $status = Status::tryFromAny($request->get('status')); // string, int, null, or enum instance // Get a serializable value from any enum $value = $status->toValue(); // Works for Unit, String, and Int backed enums // Compare across enum boundaries $status->compares(LegacyStatus::Active); // true if values match
Installation
composer require fab2s/enumerate
Requirements: PHP 8.1+
Quick Start
Add the trait to your enum:
use fab2s\Enumerate\EnumerateTrait; use fab2s\Enumerate\EnumerateInterface; enum Status: string implements EnumerateInterface { use EnumerateTrait; case Pending = 'pending'; case Active = 'active'; case Closed = 'closed'; }
Now you can:
// From HTTP request (could be string, int, or null) $status = Status::tryFromAny($request->input('status')); // From database or JSON $status = Status::fromAny($row['status']); // throws if invalid // To database or JSON $value = $status->toValue(); // 'active' json_encode($status); // '"active"' // Validation $status->equals('active', 'pending'); // true if matches any
API Reference
Instantiation Methods
| Method | Returns | Throws | Description |
|---|---|---|---|
tryFromAny($value, $strict = true) |
?static |
- | Safe instantiation from any type |
fromAny($value, $strict = true) |
static |
InvalidArgumentException |
Strict instantiation |
tryFromName($name) |
?static |
- | Match by case name |
fromName($name) |
static |
InvalidArgumentException |
Strict match by case name |
Value Methods
| Method | Returns | Description |
|---|---|---|
toValue() |
int|string |
Get the backed value (or case name for UnitEnum) |
jsonSerialize() |
int|string |
Same as toValue(), implements JsonSerializable |
Comparison Methods
| Method | Returns | Description |
|---|---|---|
equals(...$values) |
bool |
Strict match against provided values |
compares(...$values) |
bool |
Loose match, allows cross-enum comparison by value |
Type Inspection (Static Helper Only)
| Method | Returns | Description |
|---|---|---|
Enumerate::getType($enum) |
?string |
Returns 'string', 'int', or null |
Enumerate::isStringBacked($enum) |
bool |
Check if string-backed |
Enumerate::isIntBacked($enum) |
bool |
Check if int-backed |
Enumerate::isBacked($enum) |
bool |
Check if backed (not UnitEnum) |
Detailed Usage
Safe Instantiation with tryFromAny
The native tryFrom() only accepts the exact backing type. tryFromAny() handles real-world input:
enum Priority: int implements EnumerateInterface { use EnumerateTrait; case Low = 1; case Medium = 2; case High = 3; } // All return Priority::Medium Priority::tryFromAny(2); Priority::tryFromAny(Priority::Medium); // All return null (no exception) Priority::tryFromAny('2'); // string on int-backed Priority::tryFromAny(null); Priority::tryFromAny('invalid');
Cross-Enum Matching
When migrating between enum versions or comparing related enums, use non-strict mode:
enum LegacyRole: string { case Admin = 'admin'; case User = 'user'; } enum Role: string implements EnumerateInterface { use EnumerateTrait; case Admin = 'admin'; case User = 'user'; case Moderator = 'moderator'; } // Strict mode (default): only accepts same enum type Role::tryFromAny(LegacyRole::Admin); // null Role::tryFromAny(LegacyRole::Admin, strict: true); // null // Non-strict mode: matches by value across enums Role::tryFromAny(LegacyRole::Admin, strict: false); // Role::Admin
Working with UnitEnums
UnitEnums (no backing value) are matched by case name:
enum Color implements EnumerateInterface { use EnumerateTrait; case Red; case Green; case Blue; } Color::tryFromAny('Red'); // Color::Red Color::tryFromName('Red'); // Color::Red Color::Red->toValue(); // 'Red' (case name as string) json_encode(Color::Red); // '"Red"'
Comparison Methods
enum Status: string implements EnumerateInterface { use EnumerateTrait; case Draft = 'draft'; case Published = 'published'; case Archived = 'archived'; } $status = Status::Published; // equals: strict comparison, multiple values allowed $status->equals('published'); // true $status->equals('draft', 'published'); // true (matches any) $status->equals(Status::Published); // true // compares: allows cross-enum matching by value $status->compares(OtherEnum::Published); // true if values match
Using the Static Helper
All methods are also available via the Enumerate static class, useful when you can't modify the enum:
use fab2s\Enumerate\Enumerate; // Works with any enum, even without the trait Enumerate::tryFromAny(SomeEnum::class, $value); Enumerate::toValue(SomeEnum::Case); Enumerate::equals(SomeEnum::Case, 'value'); Enumerate::getType(SomeEnum::class); // 'string', 'int', or null
Real-World Examples
Form Request Handling
class UpdateOrderRequest extends FormRequest { public function validated(): array { return [ 'status' => OrderStatus::fromAny($this->input('status')), 'priority' => Priority::tryFromAny($this->input('priority')) ?? Priority::Medium, ]; } }
Database Model Casting
class Order extends Model { protected $casts = [ 'status' => OrderStatus::class, ]; public function scopeActive($query) { return $query->whereIn('status', [ OrderStatus::Pending->toValue(), OrderStatus::Processing->toValue(), ]); } }
API Response
return response()->json([ 'status' => $order->status, // Automatically serialized via JsonSerializable ]);
Why This Exists
PHP enums prioritize type safety over practicality. While philosophically sound, this creates boilerplate in real applications where enums must interoperate with HTTP requests (always strings), databases, and JSON APIs.
Enumerate handles this complexity internally so you don't have to write defensive code everywhere enums cross system boundaries.
Contributing
Contributions are welcome. Please open issues and submit pull requests.
License
Enumerate is open-source software licensed under the MIT license.
fab2s/enumerate 适用场景与选型建议
fab2s/enumerate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.31k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「enum」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fab2s/enumerate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fab2s/enumerate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fab2s/enumerate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Enum type behavior and helper for Yii2, for PostgreSQL only
Enum libraries used by Zimbra Api
Bundle for Doctrine enumerations extension for Postgres
统计信息
- 总下载量: 2.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-19