werkspot / enum
Werkspot's Enum Library
Installs: 143 216
Dependents: 7
Suggesters: 0
Security: 0
Stars: 2
Watchers: 32
Forks: 1
Open Issues: 0
Requires
- php: >=7.3
- doctrine/inflector: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.1
README
This package holds a simple class that may be used as an ancestor for your enum classes.
Install
# composer require werkspot/enum
Usage
Extend the Werkspot\Enum\AbstractEnum, define your enum key values as constants.
namespace YourAwesomeOrganisation\Project; use Werkspot\Enum\AbstractEnum; /** * @method static FooEnum foo() * @method bool isFoo() * @method static FooEnum bar() * @method bool isBar() * @method static FooEnum baz() * @method bool isBaz() */ final class FooEnum extends AbstractEnum { const FOO = 'foo'; const BAR = 'bar'; const BAZ = 'baz'; }
Now you can use the enum in a class:
namespace YourAwesomeOrganisation\Project; final class Bar { /** @var FooEnum */ private $enum; punblic function __construct(FooEnum $enum) { $this->enum = $enum; } public function getEnum(): FooEnum { return $this->enum; } }
Implementation of that class
$fooEnum = FooEnum::baz(); $bar = new Bar($fooEnum); $enum = $bar->getEnum(); $value = $enum->getValue(); // will return 'baz'