Extend your enums by settings provided by attributes.
composer require kdabrow/enum-settings
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,
) {}
}
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
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;
}
vendor/bin/phpunit