This package holds a simple class that may be used as an ancestor for your enum classes.
# composer require werkspot/enum
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'