werkspot / enum
Werkspot 的 Enum 库
v3.0
2020-05-22 11:48 UTC
Requires
- php: >=7.3
- doctrine/inflector: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.1
README
此包包含一个简单的类,可以作为您枚举类的基类使用。
安装
# composer require werkspot/enum
用法
扩展 Werkspot\Enum\AbstractEnum,将您的枚举键值定义为常量。
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'; }
现在您可以在类中使用枚举
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; } }
该类的实现
$fooEnum = FooEnum::baz(); $bar = new Bar($fooEnum); $enum = $bar->getEnum(); $value = $enum->getValue(); // will return 'baz'