tkaratug/powered-enum
Composer 安装命令:
composer require tkaratug/powered-enum
包简介
Some cool add-ons to PHP native enums.
README 文档
README
This package offers a trait that contains some cool features for native PHP enums.
Requirements
- PHP
8.1or higher.
Installation
composer require tkaratug/powered-enum
Declaration
All you need to do is use the PoweredEnum trait in your native PHP enums.
use Tkaratug\PoweredEnum\PoweredEnum; enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; }
Jump To
Methods
is(), isNot()
- You can check the equality of a case against any name by passing it to the
is()andisNot()methods.
$myEnum = MyEnum::ONE; $myEnum->is(MyEnum::ONE); // true $myEnum->is(MyEnum::TWO); // false $myEnum->isNot(MyEnum::ONE); // false $myEnum->isNot(MyEnum::TWO); // true
Static Methods
hasName()
- You can check whether an enum has a case by given name via the
hasName()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::hasName('ONE'); // true MyEnum::hasName('THREE'); // false
hasValue()
- You can check whether an enum has a case by given value via the
hasValue()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::hasValue(1); // true MyEnum::hasValue(3); // false
getNames()
- You can get enum case names as an array by using the
getNames()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::getNames(); // ['ONE', 'TWO']
getValues()
- You can get enum case values as an array by using the
getValues()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::getValues(); // [1, 2]
toArray()
- You can get a combined array of the enum cases as
value => nameby using thetoArray()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::toArray(); // [1 => 'ONE', 2 => 'TWO']
getNamesExcept()
- You can get names of enum cases as an array except for given ones by using the
getNamesExcept()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; } MyEnum::getNamesExcept([MyEnum::ONE]); // ['TWO', 'THREE']
getValuesExcept()
- You can get values of enum cases as an array except for given ones by using the
getValuesExcept()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; } MyEnum::getValuesExcept([MyEnum::ONE]); // [2, 3]
toArrayExcept()
- You can get a combined array of the enum cases as
value => nameexcept for given ones by using thetoArrayExcept()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; } MyEnum::toArrayExcept([MyEnum::ONE]); // [2 => 'TWO', 3 => 'THREE]
getRandomName()
- You can get a random name of the enum cases by using the
getRandomName()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; } MyEnum::getRandomName(); // ['ONE']
getRandomValue()
- You can get a random value of the enum cases by using the
getRandomValue()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; } MyEnum::getRandomValue(); // [1]
getRandomCase()
- You can get a random case of the enum by using the
getRandomCase()method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; } MyEnum::getRandomCase(); // MyEnum::ONE
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you've found a bug regarding security please mail tkaratug@hotmail.com.tr instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
tkaratug/powered-enum 适用场景与选型建议
tkaratug/powered-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91 次下载、GitHub Stars 达 21, 最近一次更新时间为 2022 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tkaratug/powered-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tkaratug/powered-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 91
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-12