kdabrow/enum-settings
最新稳定版本:1.0.0
Composer 安装命令:
composer require kdabrow/enum-settings
包简介
Extend your enums by settings provided by attributes
README 文档
README
Extend your enums by settings provided by attributes.
Installation
composer require kdabrow/enum-settings
How to use it
Prepare enum
Add trait UseAttributeSettings to your enum and implement attribute to enum's cases. It's possible to use multiple attributes.
use \Kdabrow\EnumSettings\UseAttributeSettings; enum Countries { use UseAttributeSettings; #[CountrySettings('polish', 38)] case poland; #[CountrySettings('spanish', 47)] case spain; }
Your attribute class might look like this. Name should end with 'Settings' but it is customizable.
#[\Attribute] class CountrySettings { public function __construct( public readonly string $language, public readonly int $population, ) {} }
Usage
Get all available settings
Countries::poland->getSettings(); // return array with all settings [ 'language' => 'polish', 'population' => 38, ]
Get one setting
Countries::poland->getSetting('population'); // return value of population setting 38
Customisation
It's possible to set up attribute name. For example, you have attributes: Details and Cities
use \Kdabrow\EnumSettings\UseAttributeSettings; enum Countries { use UseAttributeSettings; public function settingsAttributeName() { return [Details::class, Cities::class]; } #[Details('polish', 38)] #[Cities('Warsaw', 'Cracow', 'Gdansk')] case poland; #[Details('spanish', 47)] #[Cities('Madrid', 'Barcelona', 'Valencia')] case spain; }
Testing
vendor/bin/phpunit
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-13