azimkordpour/power-enum
Composer 安装命令:
composer require azimkordpour/power-enum
包简介
This light package provides some methods to your Enum classes to make the most of them.
README 文档
README
This lightweight package provides a Trait that allows you to fully utilize Enum classes in your PHP projects, particularly in modern PHP frameworks like Laravel.
Installation
NOTE: As Enum was introduced in PHP 8.1, this package requires a minimum PHP version of 8.1.
You can install the package via composer:
composer require azimkordpour/power-enum
Usage Instructions
To use the PowerEnum trait in your Enum class, simply import it like this:
<?php use AzimKordpour\PowerEnum\Traits\PowerEnum; enum PostStatus: string { use PowerEnum; case Active = 'active'; case Inactive = 'inactive'; }
Now, let's take a closer look at the methods.
In Laravel
Eloquent allows you to cast your attribute values to PHP Enums.
<?php namespace App\Models; use App\Enums\PostStatus; use Illuminate\Database\Eloquent\Model; class Post extends Model { /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'status' => PostStatus::class, ]; }
Then, you can use it like the below examples.
Check if the status of the model is active:
$post = Post::find(1); // The status is active. $post->status->isActive();
Returns boolean:
true
Check if the status of the model equals the given value:
$post = Post::find(1); // The status is active. $post->status->equals(PostStatus::Active);
Returns boolean:
false
This method works like equals:
$post = Post::find(1); // The status is active. $post->status->is(PostStatus::Active);
Returns boolean:
false
Get the label of the status:
$post = Post::find(1); // The status is active. $post->status->label();
Returns the value of the case if you have not set labels:
"active"
For setting custom labels and Seeing all methods in PHP projects, take a look at the next section.
All Methods
Get the values of PostStatus statically:
PostStatus::values();
Returns an array:
[
'active',
'inactive'
]
Get the names of PostStatus statically:
PostStatus::names();
Returns an array:
[
'Active',
'Inactive'
]
Get the names and values of PostStatus statically:
PostStatus::list();
Returns an array:
[
'Active' => 'active',
'Inactive' => 'inactive'
]
Check if the case is the active one:
PostStatus::from('active')->isActive();
Returns boolean:
true
Check if the case equals the given value:
PostStatus::Active->equals(AnotherEnum::Example);
Returns boolean:
false
This method works like equals:
PostStatus::Active->is(AnotherEnum::Example);
Returns boolean:
false
Initiate the class from name:
PostStatus::fromName('Active');
Returns the Enum object:
PostStatus::Active
Get the label of the case:
PostStatus::Active->label();
Returns the value of the case if you have not set labels:
"active"
Get the labels of the cases:
PostStatus::Active->getLabels();
Returns the values of the cases if you have not set labels:
[
'active' => 'active',
'inactive' => 'inactive'
]
You can write custom label for the cases in your Enum class:
/** * Set the labels of all the cases. */ public static function setLabels(): array { return [ self::Active->value => 'published post', self::Inactive->value => 'draft post', ]; }
Then, the method of label:
PostStatus::Active->label();
Returns:
"published post"
And the method of getLabels:
PostStatus::Active->getLables();
Returns:
[
'active' => 'published post',
'inactive' => 'draft post'
]
Testing
composer test
azimkordpour/power-enum 适用场景与选型建议
azimkordpour/power-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.85k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2023 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「enum」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 azimkordpour/power-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 azimkordpour/power-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 azimkordpour/power-enum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Enum type behavior and helper for Yii2, for PostgreSQL only
Enum libraries used by Zimbra Api
Bundle for Doctrine enumerations extension for Postgres
统计信息
- 总下载量: 18.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-07