werkspot/enum

Werkspot's Enum Library

v3.0 2020-05-22 11:48 UTC

This package is auto-updated.

Last update: 2024-02-29 02:58:40 UTC


README

This package holds a simple class that may be used as an ancestor for your enum classes.

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version License

Install

# composer require werkspot/enum

Usage

Extend the Werkspot\Enum\AbstractEnum, define your enum key values as constants.

namespace YourAwesomeOrganisation\Project;

use Werkspot\Enum\AbstractEnum;

/**
 * @method static FooEnum foo()
 * @method bool isFoo()
 * @method static FooEnum bar()
 * @method bool isBar()
 * @method static FooEnum baz()
 * @method bool isBaz()
 */
final class FooEnum extends AbstractEnum
{
    const FOO = 'foo';
    const BAR = 'bar';
    const BAZ = 'baz';   
}

Now you can use the enum in a class:

namespace YourAwesomeOrganisation\Project;

final class Bar
{
    /** @var FooEnum */
    private $enum;
    
    punblic function __construct(FooEnum $enum)
    {
        $this->enum = $enum;
    }
    
    public function getEnum(): FooEnum
    {
        return $this->enum;    
    }
}

Implementation of that class

$fooEnum = FooEnum::baz();
$bar = new Bar($fooEnum);

$enum = $bar->getEnum();
$value = $enum->getValue(); // will return 'baz'