utilitte/enum

There is no license information available for the latest version (v2.0.0) of this package.

v2.0.0 2020-10-13 10:03 UTC

This package is auto-updated.

Last update: 2024-12-13 19:15:57 UTC


README

Type safe enums

Getting started

Create enum class

/**
 * @method static self ENGLAND() 
 * @method static self USA() 
 */
class CountryEnum extends Enum
{
    protected static function getEnums(): array
    {
        return ['england', 'usa'];
    }

}

Usage

static method must be always uppercase

assert(CountryEnum::USA() === CountryEnum::USA());

function country(CountryEnum $country): string {
    return $country->value();
}

assert(country(CountryEnum::USA()) === 'usa');

convert enum value to object:

assert(CountryEnum::get('usa') === CountryEnum::USA());