emulgeator / enum
A very simple library allowing you to define enums in PHP
Installs: 3 965
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2024-10-28 02:20:22 UTC
README
A very simple library allowing you to define enums in PHP
Getting Started
Installing
Run composer require emulgeator/enum
to add this library as a dependency to your project
Usage
Just extend the class, define the possible values, and crate your own constructors
:
use Emul\Enum\EnumAbstract; class Status extends EnumAbstract { const ENABLED = 'enabled'; const DISABLED = 'disabled'; const DELETED = 'deleted'; public static function enabled(): self { return new self(self::ENABLED); } public static function disabled(): self { return new self(self::DISABLED); } public static function deleted(): self { return new self(self::DELETED); } protected static function getPossibleValues(): array { return [ self::ENABLED, self::DISABLED, self::DELETED, ]; } } $enabled = Status::enabled(); $disabled = Status::disabled(); $deleted = Status::createFromString('invalid'); // Throws exception $enabled->isEqualToString('disabled'); // false $enabled->isEqualTo($disabled); // false