Skip to content

Type-Strict Enum which works on >= PHP7.1

License

Notifications You must be signed in to change notification settings

gachakra/php-enum

Repository files navigation

php-enum

inspired from this article https://qiita.com/Hiraku/items/71e385b56dcaa37629fe (japanese)

Quick start to run examples

docker-compose up -d --build // build docker images and up containers
docker exec -it php-enum_php bash // login the php container

php examples/BasicMethods/Continent.php // or another example php file

Usage

Class Definition

Private or protected constructor way

/**
 * @method static self ELEMENT1
 * @method static self ELEMENT2
 */
class PrivateEnumChild extends Enum
{
    const ELEMENT1 = 'element1';
    const ELEMENT2 = 'element2';
}

PrivateEnumChild::ELEMENT1(); // Instantiate an enum element using internal cache

Public constructor way

/**
 * @method static self ELEMENT1
 * @method static self ELEMENT2
 */
class PublicEnumChild extends Enum
{
    const ELEMENT1 = 'element1';
    const ELEMENT2 = 'element2';
    
    public function __construct(string $value)
    {
        parent::__construct($value);
    }
}

PublicEnumChild::ELEMENT1(); // Instantiate an enum element using internal cache
new PublicEnumChild(PublicEnumChild::ELEMENT2); // Create new enum element instance not using internal cache

Basic Methods

EnumChild::ELEMENT1()->equals(EnumChild::of('ELEMENT1')); // true
EnumChild::ELEMENT1()->equals(EnumChild::fromValue('element1')); // true

EnumChild::ELEMENT1()->equals(EnumChild::elements()['ELEMENT1']); // true
EnumChild::ELEMENT1()->equals(EnumChild::valueToElement()['element1']); // true

About

Type-Strict Enum which works on >= PHP7.1

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published