adam-lutka / php-enum
PHP implementation of enum type
Installs: 6 068
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires (Dev)
- phpunit/phpunit: ^7.1
This package is auto-updated.
Last update: 2025-03-29 00:59:08 UTC
README
Enum types are represented by classes. Values of specific enum type are represented by @method annotations that enables code completion and doesn't cause duplicity.
<?php require_once('vendor/autoload.php'); /** * @method static TypeEnum TYPE_1() * @method static $this TYPE_2() */ final class TypeEnum extends \AL\PhpEnum\Enum {} var_dump((string)TypeEnum::TYPE_2()); // string(6) "TYPE_2" var_dump(TypeEnum::TYPE_2()->getValue()); // string(6) "TYPE_2" var_dump(TypeEnum::TYPE_1()->getOrder()); // int(0) var_dump(TypeEnum::TYPE_2()->getOrder()); // int(1) var_dump(TypeEnum::TYPE_2() === TypeEnum::TYPE_2()); // bool(true) var_dump(TypeEnum::TYPE_2() == TypeEnum::TYPE_2()); // bool(true) var_dump(TypeEnum::TYPE_1() === TypeEnum::TYPE_2()); // bool(false) var_dump(TypeEnum::TYPE_1() == TypeEnum::TYPE_2()); // bool(false) var_dump(TypeEnum::parse('TYPE_2') === TypeEnum::TYPE_2()); // bool(true) var_dump(TypeEnum::tryParse('NOT_EXIST')); // NULL var_dump(TypeEnum::inOrder(0) === TypeEnum::TYPE_1()); // bool(true) var_dump(TypeEnum::tryInOrder(1000)); // NULL
Getting Started
Prerequisites
The code needs PHP 7.1 or greater.
Installing
composer require adam-lutka/php-enum
Running the tests
composer install
vendor/bin/phpunit
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.