mle86/enum
Composer 安装命令:
composer require mle86/enum
包简介
A base class for enum functionality in PHP.
README 文档
README
This PHP library
provides enum functionality for PHP 7.1+
with an Enum interface,
an AbstractEnum base class,
and an AbstractAutoEnum base class.
It is released under the MIT License.
Installation and Dependencies:
Via Composer: $ composer require mle86/enum
Or insert this into your project's composer.json file:
"require": { "mle86/enum": "^0" }
Its only dependency is the mle86/value library.
Minimum PHP Version:
PHP 7.1
Usage:
To implement a custom Enum class, simply extend the AbstractAutoEnum base class and add a few public constants:
<?php class TriState extends \mle86\Enum\AbstractAutoEnum { public const HIGH = 'H'; public const LOW = 'L'; public const UNDEF = 'Z'; }
For more fine control
over which values are considered valid by your class,
override the isValid() class method or
extend the more general AbstractEnum base class instead.
Keep in mind though that enum classes
are fundamentally based on
a hardcoded list of accepted values known at compile-time;
if you want to accept values based on a pattern or on complex validation logic,
consider using a value object class
such as Value
instead.
There's three ways to use enum classes:
-
Just use the class constants.
You don't really need this library to do that as you can simply write a standalone class for that, but it's definitely a possibility. -
Build instances and use typehints in your methods.
The AbstractEnum and AbstractAutoEnum base classes have a default constructor and wraps a single valid enum value in the instance. The wrapped value can then be retrieved with thevalue()getter but it's also returned by the default__toString()andjsonSerialize()methods. -
Use the
validate()andvalidateOptional()methods to enfore correct types in your methods.
If you don't want to build instances of your enum classes to avoid the object overhead or simply because you receive non-object input values (e.g. from a JSON API), you can also check raw input values with the twovalidatemethods. They will accept both raw values and instances, return void, and throw an EnumValueException if the input is invalid. ThevalidateOptional()method also accepts NULL values.
An example with instances and enum typehints:
<?php $state = new TriState(TriState::HIGH); var_dump($state->value()); // H var_dump(json_encode($state)); // "H" var_dump($state->equals(TriState::HIGH)); // true var_dump($state->equals(TriState::LOW)); // false function (TriState $state) { // $state is definitely a TriState instance here, // so it definitely wraps a valid TriState constant. }
Another example without typehints and more explicit validation:
<?php function ($state, $optionalState = null) { TriState::validate($state); // Now we can be sure that $state contains a valid TriState value // (or maybe it's even a TriState instance). TriState::validateOptional($optionalState); // Now we know that $optionalState contains either // a valid TriState value, // a valid TriState instance, // or NULL. }
Classes and Interfaces:
- Enum base interface:
- AbstractEnum base class.
- AbstractAutoEnum base class.
- AbstractEnum base class.
- AutoEnumTrait trait.
- Exception classes.
More Documentation:
- Enum Inheritance – how to extend enum classes.
- Serializability – how Enum instances can be serialized.
- Type Safety – notes about enum value types and typecasting.
mle86/enum 适用场景与选型建议
mle86/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.35k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mle86/enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mle86/enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-13