jac/enums
最新稳定版本:1.1.0
Composer 安装命令:
composer require jac/enums
包简介
Php implementation of Enums
关键字:
README 文档
README
Enum implementation for PHP
Why
To avoid to have to install the SplEnum, yet another extension...
Enums have a lot of benefits (in my own opinion):
- Allow type hinting
- Don't need to search in endless array definitions
- Avoid misspelling of values
- Can be enrich with methods
In this implementation the constructor is made private to avoid duplicating instance of the enum
Installation
composer require jac/php-enum
How to use
For more complex example look at the example directory
Create an Enum
use Jac\Enum\AbstractEnum; /** * SendingStatus Enum * @method SendingStatus PROCESSING() * @method SendingStatus SENT() */ final class SendingStatus extends AbstractEnum { private const PROCESSING = '1 - Processing'; private const SENT = '2 - Sent'; }
Usage
function notifyUser(SendingStatus $status) { if ($status == SendingStatus::SENT()) { // your package have been sent } } $key = 'PROCESSING'; // Validate first the enum by inEnum if (SendingStatus::inEnum($key)) { notifyUser(SendingStatus::$key()); }
Enum having multiple keys with the same value[Multiple]
Instantiate an Enum from its value is possible calling the Devise::EUR() for example,
and having multiple keys with the same value won't be harmful in that case. However, it
is different when we try to instantiate an Enum from its value. Let's take the following
example:
namespace App\Enums; use Jac\Enum\AbstractEnum; final class Devise extends AbstractEnum { /** * @default */ private const RMB = 'RMB'; // will be used due to the default tag private const CNY = 'RMB'; private const EUR = 'EUR'; // will be used as the other key is deprecated /** * @deprecated */ private const FRA = 'EUR'; private const US_DOLLAR = 'USD'; // will be used because of the __DEFAULT__ configuration private const USD = 'USD'; private const __DEFAULT__ = array( 'USD' => 'US_DOLLAR' ); }
There is multiple ways a developer could use to help the builder decide which key should be preferred.
- Use the
__DEFAULT__constant which is a reverted key => value array. This method will first be checked before the following two - Use the
@defaulttag in the php doc associated to the constant, in the above example, for the chinese devise (RMB, CNY), in case we useDevise::from('RMB'), the key RMB is going to be choose. - Use the
@deprecatedto exclude values. When a constant is set as deprecated, then its priority is lowered and it will be returned if and only if no other options is found.
In case there is no configuration set for the default value to choose or if there is still multiple value available, a warning is triggered and one of the found options is returned.
API
Comparison of enums works with == and ===
__toString()To display the current enum : EnumName::EnumKey::EnumValue (can be override)getValue()Return the value of the Enum (the value of the constant associated with the current key)getKey()Return the key of the the current Enumequals(AbstractEnum|null):boolCompare both enums using their values without taking the key into account.
Static method
enum(string)Create an instance of the enum giving its keyfrom(mixed)Create an enum from its value, see [Multiple] to understand more about its behaviortoArray()return the list of key => value of the enuminEnum()check if the parameter is either a key or a valuesearch(mixed): arraySearch for all the keys having the given valuekeyExists(string): boolCheck if the key exists in the enumvalueExists(mixed): boolCheck if at leas one of the keys have the given valuekeys(): array: The list of available keysvalues(): array: The list of available none unique values
Serialization
The enum implements the JsonSerializable interface. By default it will return the value as a string
A formatter is available: Jac\Enum\EnumJsonFormat with several option implemented.
use Jac\Enum\EnumJsonFormat; echo json_decode(EnumJsonFormat::asKeyValue()->format(Devise::USD_DOLLAR()));
Will output
{
"USD_DOLLAR":"USD"
}
use Jac\Enum\EnumJsonFormat; echo json_decode(EnumJsonFormat::keyAndValueAsValues()->format(Devise::USD_DOLLAR()));
Will output
{
"App\\Enums\\Devise": {
"key":"USD_DOLLAR",
"value":"USD"
}
}
统计信息
- 总下载量: 5.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-08