neutronstars/enum
Composer 安装命令:
composer require neutronstars/enum
包简介
Added an enumeration system as close as possible to PHP 8.1 for earlier versions.
README 文档
README
Installation:
Only Enum PHP whiteout Doctrine and Symfony:
composer require neutronstars/enum
For Doctrine
composer require neutronstars/doctrine-enum-php-type
For Symfony
composer require neutronstars/symfony-normalizer-enum-php
Docs: https://github.com/Neutron-Pro/symfony-normalizer-enum-php
For Symfony and Doctrine
composer require neutronstars/doctrine-enum-php-type
composer require neutronstars/symfony-normalizer-enum-php
How create an Enum class:
Method 1
/** * @method static self ONE() * @method static self TWO() * @method static self THREE() */ class MyEnum extends \NeutronStars\Enum\Enum { public const ONE = null; public const TWO = null; public const THREE = null; }
This enum does not require a constructor. We simply create a list of constants that have no value other than their name.
Method 2
/** * @method static self ONE() * @method static self TWO() * @method static self THREE() */ class MyStringEnum extends \NeutronStars\Enum\Enum { public const ONE = 'One'; public const TWO = 'Two'; public const THREE = 'Three'; }
/** * @method static self ONE() * @method static self TWO() * @method static self THREE() */ class MyIntEnum extends \NeutronStars\Enum\Enum { public const ONE = 1; public const TWO = 2; public const THREE = 3; }
Use an Enum class:
Get instance of an enum
$two = MyStringEnum::TWO(); echo $two; // return TWO echo $two->key; // return TWO echo $two->value; // return Two
Get instance of an enum from a string
$three = MyStringEnum::from('THREE'); echo $three; // THREE echo $three->key; // return THREE echo $three->value; // return Three
Compare the instances of an enum
$value = MyStringEnum::tryFrom($_GET['number']); if ($value === MyStringEnum::ONE()) { echo 'The number is ONE !'; }
Result:
if $_GET['number] is ONE or One or 1 then 'The number is ONE !'
else nothing.
Difference between from and tryFrom.
If you use "tryFrom" and the parameter value is not found, the method returns null.
If you use "from" and the value of the parameter is not found, an error of type ValueError is raised.
Retrieve all keys of the enum.
$cases = MyIntEnum::cases();
Result:
[
MyIntEnum::ONE(),
MyIntEnum::TWO(),
MyIntEnum::THREE()
]
foreach (MyIntEnum::cases() as $case) { echo '- ' . $case->value; }
Result:
- 1
- 2
- 3
Serialization and Deserialization
You can serialize an enum with the serialize function of PHP.
$serialized = serialize(MyEnum::THREE());
But for deserialization, there will be a small difference, You must use the from or tryFrom method:
$deserialized = MyEnum::from(unserialize($deserialized)); // $deserialized = MyEnum::THREE()
neutronstars/enum 适用场景与选型建议
neutronstars/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 neutronstars/enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 neutronstars/enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-06-05