morebec/orkestra-enum
Composer 安装命令:
composer require morebec/orkestra-enum
包简介
Orkestra component providing enums
README 文档
README
This Orkestra component provides typed enumerations to PHP.
Installation
composer require morebec/orkestra-orkestra-enum
Usage
Creating an Enum
To create a new Enum, one needs to extend the Enum class.
As an example, lets pretend we want to create a CardinalPoint Class.
Since there are strictly 4 cardinal points, this is a good candidate for an Enum:
class CardinalPoint extends Enum { const NORTH = 'NORTH'; const EAST = 'EAST'; const WEST = 'WEST'; const SOUTH = 'SOUTH'; }
Simply doing this, will allow us to use our class in the following way:
// Instantiate a new CardinalPoint instance $direction = new CardinalPoint(CardinalPoint::NORTH); // Since Enums have builtin validation, // the following line would throw an InvalidArgumentException: $direction = new CardinalPoint('North'); // However the following would work: $direction = new CardinalPoint('NORTH'); // Using in functions or class methods public function changeDirection(CardinalPoint $direction) { // Testing equlity with string if(!$direction->isEqualTo(new CardinalPoint(CardinalPoint::EAST))) { echo 'Not going East!'; } // Since the constants are strings, it is also possible to compare // using loose comparison if($direction == CardinalPoint::NORTH) { echo 'Definitely going North!'; } }
For easier IDE integration we can even go further adding
@methodannotations to the Enum class:/** * @method static self NORTH() * @method static self EAST() * @method static self WEST() * @method static self SOUTH() */ class CardinalPoint extends Enum { const NORTH = 'NORTH'; const EAST = 'EAST'; const WEST = 'WEST'; const SOUTH = 'SOUTH'; }This will allow us to do the following in code:
$direction = CardinalPoint::NORTH();
Getting all possible values
In order to get all the possible values as an array you can use the static method getValues:
CardinalPoint::getValues(); // Returns an array as: // [ 'NORTH', 'EAST', 'WEST', 'SOUTH' ]
morebec/orkestra-enum 适用场景与选型建议
morebec/orkestra-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 534 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 morebec/orkestra-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 morebec/orkestra-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 534
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-04-21