Skip to content

Latest commit

 

History

History
142 lines (97 loc) · 3 KB

configure.md

File metadata and controls

142 lines (97 loc) · 3 KB

Configure

Configure is very useful when you want to add enhanced enums to your own package or shared library and allow users to modify behavior of certain aspects.

For example: the LogLevel enum in this package is utilizing this trait, so you can use this enum for your own purposes.

Note: this feature is not added by default to the Enhancers aggregate trait

How to use

You can simply add the aggregate trait Configure to your enum to utilize all features.

use Henzeb\Enumhancer\Concerns\Enhancers;
use Henzeb\Enumhancer\Concerns\Configure;
enum YourConfigurableEnum {
    use Enhancers, Configure;

    // ...
}

If you want to use a selection, you can choose between the following:

Example

use Henzeb\Enumhancer\Concerns\Defaults;
use Henzeb\Enumhancer\Concerns\ConfigureDefaults;
enum YourConfigurableEnum {
    use Defaults, ConfigureDefaults;

    // ...
}

Each configurable option also has a Once method, which can be used in a Service Provider or in the root of your project so you can prevent changes later on if needed.

ConfigureDefaults

Used with Defaults

ConfigureDefaults example

use Henzeb\Enumhancer\Concerns\ConfigureDefaults;
use Henzeb\Enumhancer\Concerns\Enhancers;

enum YourEnum {
    use Enhancers, ConfigureDefaults;

    case AnEnum;
    // ...
}

yourEnum::setDefault(YourEnum::AnEnum);
yourEnum::setDefaultOnce(YourEnum::AnEnum);

ConfigureLabels

Used with Labels

ConfigureLabels example

use Henzeb\Enumhancer\Concerns\ConfigureLabels;
use Henzeb\Enumhancer\Concerns\Enhancers;

enum YourEnum {
    use Enhancers, ConfigureLabels;

    case AnEnum;
    // ...
}

yourEnum::setLabels([YourEnum::AnEnum->name => 'An Enum']);
yourEnum::setLabelsOnce([YourEnum::AnEnum->name => 'An Enum']);

ConfigureMapper

Used with Mappers

ConfigureMapper example

use Henzeb\Enumhancer\Concerns\ConfigureMapper;

use Henzeb\Enumhancer\Concerns\Enhancers;

enum Suit {
    use Enhancers, ConfigureMapper;

    // ...
}

yourEnum::setMapper(new SuitMapper());
yourEnum::setMapper(['H'=>'Hearts']);
yourEnum::setMapper(SuitMapper::class, ['H'=>'Hearts']);

yourEnum::setMapperOnce(new YourMapper());
yourEnum::setMapperOnce(['H'=>'Hearts']);
yourEnum::setMapperOnce(SuitMapper::class, ['H'=>'Hearts']);

ConfigureState

Used with State

ConfigureState example

use Henzeb\Enumhancer\Concerns\ConfigureState;

use Henzeb\Enumhancer\Concerns\Enhancers;

enum YourEnum {
    use Enhancers, ConfigureState;

    case Up;
    case Down;
    case Stop;

    // ...
}

yourEnum::setTransitionHook(new YourTransitionHook());
yourEnum::setTransitionHookOnce(new YourTransitionHook());
yourEnum::setTransitions(['Up'=>'Stop', 'Stop'=> ['Up', 'Down']]);
yourEnum::setTransitionHookOnce(['Up'=>'Stop', 'Stop'=> ['Up', 'Down']]);