kwaadpepper/enum
Composer 安装命令:
composer require kwaadpepper/enum
包简介
Enums to use for any project
README 文档
README
This package goal is to provide a complete solution to lack a long time php weakness : it do not have any enum support.
This will change with php 8 enums proposition. In the mean time we still a support for this allowing us to generate generic and extendable applications (instead of have singletons sql tables..).
You will be able to use enums in any project using a simple definition, take a look at example Basic or a more sophisticated Example
Installation
Via Composer
$ composer require kwaadpepper/enum
Usage
All enum have two properties : label and value, only value has to be unique. Each enum can have multiple options all written on class comment as static methods, these have to be unique !
- On Any project, take a look on examples to create an enum class.
-
Invoke an enum value
Days::mon() -
Compare enums
Days::mon()->equals(Days::tue()) // false Days::mon()->equals(Days::mon()) // true Days::mon()->value === Days::tue()->value // false Days::mon()->value === Days::mon()->value // true
-
Print an enum
echo Days::mon(); // 2 echo Days::tue(); // 4 echo Days::mon()->label; // Monday
As you can see enum implements the __toString method which you can override to display the label instead of the value. This default behavior is set like this for a better behavior in laravel.
-
Serialise an enum
echo json_encode(Days::mon()); // {"label":"Monday","value":2}
Enums implement the JsonSerializable interface
- On a Laravel project you can use enums in multiple ways
-
As a property
// Add theses to your model use CastsEnums; protected $enumCasts = [ 'day' => Days::class ];
This will allow you model to store an enum in database using its value, and then cast the property to an enum when accessing it
-
As a route parameter
Define a route like the following
Route::get('/days/{day}', function (Days $day) { return response()->json([$day]); })->middleware('bindings'); // OR Route::get('/days/{day}', [DayController::class, 'getDay']);
Then on your controller for the
DayControllerexamplepublic function getDay(Request $request, Day $day) { return response()->json([$day]); }
-
As a request parameter using validation
Take a look at this request example.
new EnumIsValidRule(Days::class)
It use EnumIsValidRule to validate the parameter as being a valid enum value.
Note that you may still have to cast your parameter to int if your enum value is an int, as enum check is strict
-
As a model primary Key
Take a look at unit test using the Report enum class from tests
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email github@jeremydev.ovh instead of using the issue tracker.
Credits
License
MIT. Please see the license file for more information.
kwaadpepper/enum 适用场景与选型建议
kwaadpepper/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「enum」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kwaadpepper/enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kwaadpepper/enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kwaadpepper/enum 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 4.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-01