upward/enumerables
最新稳定版本:v1.0.3
Composer 安装命令:
composer require upward/enumerables
包简介
Utils for enums
README 文档
README
A PHP/Laravel package that enhances Enum usage with various utilities like easy conversion, validation, and additional operations. Designed to streamline working with Enums in Laravel projects, Enumerables optimizes code for clarity and maintainability when handling constants and specific values.
Installation
To install the Enumerables package run the following command in your terminal:
composer require upward/enumerables
Features
Enum Case Attributes with Label
The Enumerables package provides a way to add a Label attribute to your Enum cases, allowing you to define custom text for each case. This can be especially useful for displaying human-readable labels or translating Enum values.
Usage Example
Here's how to use the Label attribute with an Enum case and retrieve the text:
use Upward\Enumerables\Attributes\Label; use Upward\Enumerables\Traits\InteractsWithLabel; enum Status { use InteractsWithLabel; #[Label(text: 'Active')] case ACTIVE; #[Label(text: 'Inactive')] case INACTIVE; } // Accessing the label text $status = Status::ACTIVE; echo $status->getLabel(); // Outputs: Active
Example without Translation
If you want to display the label without translation, set the translate option to false:
use Upward\Enumerables\Attributes\Label; use Upward\Enumerables\Traits\InteractsWithLabel; enum UserRole { use InteractsWithLabel; #[Label(text: 'Administrator', translate: false)] case ADMIN; #[Label(text: 'Editor', translate: false)] case EDITOR; } $role = UserRole::ADMIN; echo $role->getLabel(); // Outputs: Administrator
Note on Translation
Translations are performed using the default Laravel __() function, allowing you to manage language files and translations easily.
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-31