salekur/enum
最新稳定版本:1.0.2
Composer 安装命令:
composer require salekur/enum
包简介
A powerful tool for creating and managing enumerations.
README 文档
README
A robust and flexible package for creating enumerations in Laravel. This package simplifies the creation and management of enumerated types, offering a consistent and extensible approach to handle options and their corresponding labels.
Features
- Automatic Label Generation: Automatically generates human-readable labels from enumeration keys.
- Custom Label Support: Allows for custom labels through a static
valuesmethod. - Option Retrieval: Provides methods to retrieve all options, specific options by key, and check the existence of options.
- Key and Label Extraction: Easily extract all keys or labels from the enumeration.
Installation
To install the package, use Composer:
composer require salekur/enum
Usage
Extend the Enum class and define constants for each enumerated value to create a new enumeration. Optionally, provide a values method to customize labels.
namespace Salekur\Enum\Enum; class UserRole extends Enum { const ADMIN = 'admin'; const EDITOR = 'editor'; const VIEWER = 'viewer'; // optional values function public static function values(): array { return [ self::ADMIN => 'Administrator', self::EDITOR => 'Content Editor', self::VIEWER => 'Content Viewer' ]; } }
Retrieving Options
The Enum class provides several methods to retrieve and interact with enumeration options. These methods allow you to access all enumeration options easily, specify option labels, check for the existence of an option, and extract keys and labels. Below are examples demonstrating how to use these methods with the UserRole enum.
$options = UserRole::options(); // ['admin' => 'Administrator', 'editor' => 'Content Editor', 'viewer' => 'Content Viewer'] $label = UserRole::get('admin'); // 'Administrator' $exists = UserRole::has('editor'); // true $keys = UserRole::keys(); // ['admin', 'editor', 'viewer'] $labels = UserRole::labels(); // ['Administrator', 'Content Editor', 'Content Viewer']
统计信息
- 总下载量: 72
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-25