Extend your enums by settings provided by attributes.
Sponsored by Givore and Mistflare
composer require kdabrow/enum-settingsAdd 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
{
publicfunction__construct(
publicreadonlystring$language,
publicreadonlyint$population,
) {}
}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 setting38It's possible to set up attribute name. For example, you have attributes: Details and Cities
use \Kdabrow\EnumSettings\UseAttributeSettings;
enum Countries {
use UseAttributeSettings;
publicfunctionsettingsAttributeName()
{
return [Details::class, Cities::class];
}
#[Details('polish', 38)]
#[Cities('Warsaw', 'Cracow', 'Gdansk')]
case poland;
#[Details('spanish', 47)]
#[Cities('Madrid', 'Barcelona', 'Valencia')]
case spain;
}vendor/bin/phpunit