mediagone/types-enums
Composer 安装命令:
composer require mediagone/types-enums
包简介
Strongly typed enum (classes) to mimic PHP's 8.1 enums.
README 文档
README
Installation
This package requires PHP 7.4+
Add it as Composer dependency:
$ composer require mediagone/types-enums
Introduction
Using constants to represent enumerable values is unsafe unless we provide sufficient checks to prevent any problem:
class Article { public const STATUS_DRAFT = 0; public const STATUS_PUBLISHED = 1; private int $status; public function changeStatus(int $status) : void { // We need need to check if the provided status value is valid. if (! in_array($status, [Article::STATUS_DRAFT, Article::STATUS_PUBLISHED], true)) { throw new LogicException("Invalid status ($status)"); } $this->status = $status; } } $article->changeStatus(Article::STATUS_PUBLISHED); // valid $article->changeStatus(1); // valid but not safe $article->changeStatus(2); // invalid (and risky if there is no validity check in the method) class OtherClass { public const ANOTHER_INT_CONSTANT = 0; } $article->changeStatus(OtherClass::ANOTHER_INT_CONSTANT); // valid but senseless
Using strongly typed Enums instead of PHP primitive types (int, string) allows to safely typehint them everywhere and ensure that your data is valid without adding any check in your code.
Unfortunately, PHP doesn't provide native enums prior to 8.1, but they can be emulated using classes.
Documentation
Simple usage
First of all, create an enum class that defines your enumerable values as private constants:
Note: add static method annotations to your class to enable autocompletion in your favorite IDE.
/** * @method static ArticleStatus DRAFT() * @method static ArticleStatus PUBLISHED() */ final class ArticleStatus extends EnumInt { private const DRAFT = 0; private const PUBLISHED = 1; }
Enum values are now accessible using static methods which return an instance of ArticleStatus:
ArticleStatus::DRAFT(); ArticleStatus::PUBLISHED(); // Strict comparisons are valid since methods always return the same instance ArticleStatus::PUBLISHED() === ArticleStatus::PUBLISHED(); // true ArticleStatus::DRAFT() === ArticleStatus::PUBLISHED(); // false
Now, you can use it in your class as a regular typed property:
class Article { private ArticleStatus $status; public function changeStatus(ArticleStatus $status) : void { // No check needed because $status is always a valid value. $this->status = $status; } } $article->changeStatus(ArticleStatus::PUBLISHED()); // valid $article->changeStatus(1); // invalid
Enum informations
You can access enum underlying value and name by using ->value and ->name properties:
final class ArticleStatus extends EnumInt { private const DRAFT = 0; private const PUBLISHED = 1; } ArticleStatus::PUBLISHED()->value; // 1 ArticleStatus::PUBLISHED()->name; // "PUBLISHED"
Serialization
Because the PHP serialization mechanism doesn't allow to define which instance is restored, it totally breaks how the library works and strict comparison:
$a = unserialize(serialize(ArticleStatus::PUBLISHED())); $b = ArticleStatus::PUBLISHED(); $a === $b; // false
That's why serialize() and unserialize() are blocked for all enums classes.
If you need to deal with serialization, you'll have to store the enum's value and restore it afterward manually:
$serializedValue = ArticleStatus::PUBLISHED()->value; // restore the enum $enum = ArticleStatus::from($serializedValue);
Note: using enum's name for serialization is not recommended since code refactoring might break it anytime.
License
Types Enums is licensed under MIT license. See LICENSE file.
mediagone/types-enums 适用场景与选型建议
mediagone/types-enums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.98k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「enum」 「enums」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mediagone/types-enums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mediagone/types-enums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mediagone/types-enums 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enums for Laravel Enso
Enum type behavior for Yii2 based on class constants
Shared backed enums classifying HTML elements by category (block, inline, list, table, void, metadata, root) for the UI Awesome ecosystem.
A PHP Abstract Enum Class
Helpers for handling enums and enum translations in Laravel
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
统计信息
- 总下载量: 1.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-24