brunojunior/simple-enum
Composer 安装命令:
composer require brunojunior/simple-enum
包简介
Abstract class to simulate enum with label in PHP
README 文档
README
Abstract class to simulate enum with label in PHP
How to install
composer require brunojunior/simple-enum
How to use
You have to create a class which extends SimpleEnum\Enum.
The simpliest enum
This enum will automatically generate labels thanks to the constants name. Here, you'll have those labels «First value», «Second value» and «Third value».
<?php class SimpleEnum extends \SimpleEnum\Enum { const FIRST_VALUE = 0; const SECOND_VALUE = 1; const THIRD_VALUE = 2; }
A simple enum with defined labels
In this enum, the user want to change default labels.
<?php class LabeledEnum extends \SimpleEnum\Enum { const FIRST_VALUE = 0; const SECOND_VALUE = 1; const THIRD_VALUE = 2; /** * Specify the labels */ protected static function defineList(): void { static::addEnum(static::FIRST_VALUE, 'My first value'); static::addEnum(static::SECOND_VALUE, 'This is the second value'); static::addEnum(static::THIRD_VALUE, 'Hey! 3rd value!'); } }
A more complex enum with extra fonctionnality
In this enum, the user want to change default label and add some extra features.
<?php class ComplexEnum extends \SimpleEnum\Enum { const FIRST_VALUE = 0; const SECOND_VALUE = 1; const THIRD_VALUE = 2; /** * A property */ private $prop; /** * Specify the labels */ protected static function defineList(): void { static::addEnum(static::FIRST_VALUE, 'My first value')->setProp(42); static::addEnum(static::SECOND_VALUE, 'This is the second value')->setProp(0); static::addEnum(static::THIRD_VALUE, 'Hey! 3rd value!')->setProp(-1); } /** * @param int $value * @return ComplexEnum */ private function setProp(int $value):self { $this->prop = $value; return $this; } /** * @return int|null */ public function getProp():?int { return $this->prop; } /** * @return string */ public function doStuffWithProp():string { if ($this->prop === 42) { return "This is the answer to life the universe and everything"; } return "I don't know"; } }
Available methods
Enum::getList()
This method will return an array with constants value as keys and the associated Enum instance as values.
Enum::getLabels()
This method will return an array with constants value as keys and the label as values. It can be useful for HTML select options.
Enum::getLabelById()
This method will return the label for a specific key.
If the key is unknown it will raise SimpleEnum\UnknownEumException.
Enum::getInstance()
This method will return an Enum instance for a specific key.
If the key is unknown it will raise SimpleEnum\UnknownEumException.
enumInstance->getLabel()
This method will return the label of a specific instance.
enumInstance->setLabel($label)
This method allows you to override the default label.
enumInstance->resetLabel()
This method will reset the default label of the instance.
enumInstance->getKey()
This method will return the key of the instance.
enumInstance->is(int $key)
This method will check if the instance has the same key.
enumInstance->equals(Enum $anotherInstance)
This method will check if the two instances have the same key.
How to compare
Using the ==
This way to compare is really simple. You can do something like this :
$receivedValue = 2; SimpleEnum::getInstance(SimpleEnum::THIRD_VALUE) == $receivedValue;
Using the is method
$receivedValue = 2; SimpleEnum::getInstance(SimpleEnum::THIRD_VALUE)->is($receivedValue);
brunojunior/simple-enum 适用场景与选型建议
brunojunior/simple-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 174 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 brunojunior/simple-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brunojunior/simple-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 174
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-22