fuwasegu/php-enum-util
Composer 安装命令:
composer require fuwasegu/php-enum-util
包简介
The extension library for PHP native enum.
README 文档
README
The extension library for PHP native enum.
📦 Installation
composer require fuwasegu/php-enum-util
✏️ Usage
This library provides traits to make Enum, implemented since PHP8.1, easier to use.
HasDescription trait
This trait provides an interface to the description() method and an implementation of the descriptions() method.
enum Status: string { use HasDescription; case ACTIVE = 'active'; case INACTIVE = 'inactive'; case RETIED = 'retired'; public function description(): string { return match ($this) { self::ACTIVE => 'State in which the employee is enrolled.', self::INACTIVE => 'State in which the employee is on administrative leave.', self::RETIED => 'State in which employee is retiring.', }; } }
// Getter for Enum description Status::ACTIVE->description(); // Getter for Enum value and description maps Status::descriptions();
HasLabel trait
This trait provides an interface to the label() method and an implementation of the labels() method.
enum Capital: int { use HasLabel; case TOKYO = 1; case BEIJING = 2; case WASHINGTON = 3; case LONDON = 4; public function label(): string { return match ($this) { self::TOKYO => 'Tokyo', self::BEIJING => 'Beijing', self::WASHINGTON => 'Washington', self::LONDON => 'London', }; } }
// Getter for Enum label Status::ACTIVE->label(); // Getter for Enum value and label maps Status::labels();
📌 NOTICE:
There is no implementation difference between the HasLabel and HasDescription traits. The difference is the method name.
Choose the former if you want a short label for an enum, and choose the latter if you want a long description.
HasValues trait
This trait is the value version of the cases() method originally implemented in Enum, and provides auxiliary methods.
enum Status: string { use HasValues; case ACTIVE = 'active'; case INACTIVE = 'inactive'; case RETIED = 'retired'; }
// Getter for Enum values Status::values(); // Join Enum values with a string Status::implodeValues();
HasNames trait
This trait is the name version of the cases() method originally implemented in Enum, and provides auxiliary methods.
enum Status: string { use HasNames; case ACTIVE = 'active'; case INACTIVE = 'inactive'; case RETIED = 'retired'; }
// Getter for Enum names Status::names(); // Join Enum names with a string Status::implodeNames();
Comparable trait
This trait provides a method for easy comparison of Bakced Enums
enum Status: string { use Comparable; case ACTIVE = 'active'; case INACTIVE = 'inactive'; case RETIED = 'retired'; }
// Compare Enum peer to peer $maybeActive = Status::ACTIVE; Status::ACTIVE->is($maybeActive); // true Status::INACTIVE->isNot($maybeActive); // false // Attempts to convert the compared int or string to an Enum with Enum::tryFrom before comparing // If tryFrom is null, `isFrom()` returns false and `isNotFrom()` returns true. $value = 'active'; Status::ACTIVE->isFrom($value); // true Status::INACTIVE->isNotFrom($value); // false Status::ACTIVE->isFrom('foo'); // false Status::ACTIVE->isNotFrom('foo') // true
fuwasegu/php-enum-util 适用场景与选型建议
fuwasegu/php-enum-util 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.58k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2023 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「utility」 「enum」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fuwasegu/php-enum-util 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fuwasegu/php-enum-util 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fuwasegu/php-enum-util 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
A collection of enhancements and utilities for the Silverstripe UserForms module
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Extends TYPO3 EXT:seo hreflang functionality
Enum type behavior and helper for Yii2, for PostgreSQL only
统计信息
- 总下载量: 9.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-28