dive-be/php-enum-utils
最新稳定版本:1.4.0
Composer 安装命令:
composer require dive-be/php-enum-utils
包简介
Those utilities you always need when dealing with native enumerations.
README 文档
README
This library provides a collection of native enum utilities (traits) which you almost always need in every PHP project.
Installation
You can install the package via composer:
composer require dive-be/php-enum-utils
Usage
Assume the following string backed enum:
enum Role: string { use \Dive\Enum\Arrayable; use \Dive\Enum\Assertable; use \Dive\Enum\Comparable; use \Dive\Enum\NameListable; use \Dive\Enum\ValueListable; case Administrator = 'admin'; case Auditor = 'audit'; case Moderator = 'mod'; }
Arrayable (backed enums only)
Allows you to retrieve a key-value pair of names and values:
Role::toArray(); // ['Administrator' => 'admin', 'Auditor' => 'audit', 'Moderator' => 'mod']
Assertable
This relies on the enum names being in PascalCase, which follows Larry Garfield's RFC.
Allows you to make assertions on enum instances using predicate functions:
$role = Role::Moderator; $role->isAuditor(); // false $role->isModerator(); // true
Comparable
Allows you to compare enums. Mostly useful when providing backed values:
$role = Role::Administrator; $role->equals('admin'); // true $role->equals(Role::Administrator); // true $role->equals('mod'); // false $role->equals(Role::Moderator); // false $role->equalsAny(['admin', 'mod', 'audit']); // true $role->equalsAny([Role::Administrator, Role::Auditor]); // true $role->equalsAny([Role::Moderator, 'audit']); // false
NameListable
Allows you to retrieve a list of the enum names:
Role::toNames(); // ['Administrator', 'Auditor', 'Moderator']
ValueListable (backed enums only)
Allows you to retrieve a list of the enum values:
Role::toValues(); // ['admin', 'audit', 'mod']
WithUtilities (backed enums only)
Aggregator trait for all of the aforementioned utilities.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email oss@dive.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 278.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-07