Skip to content

Repository files navigation

Enum settings

Extend your enums by settings provided by attributes.

Sponsored by Givore and Mistflare

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
{
publicfunction__construct(
publicreadonlystring$language,
publicreadonlyint$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 setting38

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;
publicfunctionsettingsAttributeName()
{
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

About

Extend your enums by settings provided by attributes

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages