kdabrow/enum-settings

Extend your enums by settings provided by attributes

1.0.0 2023-12-13 14:56 UTC

This package is auto-updated.

Last update: 2024-11-13 17:22:19 UTC


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