Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 610 Bytes

README.md

File metadata and controls

37 lines (29 loc) · 610 Bytes

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