joskolenberg / enum
PHP的枚举实现
v1.5.0
2022-12-02 10:15 UTC
Requires
- php: ^7.0||^8.0
- illuminate/support: ^5.3||^6.0||^7.0||^8.0||^9.0
Requires (Dev)
- phpunit/phpunit: ^6.0||^7.0||^8.0||^9.0
README
枚举
PHP的枚举实现
安装
使用composer来拉取此包。
使用Composer
$ composer require joskolenberg/enum
用法
设置枚举
可以通过扩展JosKolenberg\Enum\Enum类来创建枚举。
use JosKolenberg\Enum\Enum; class UserType extends Enum {}
这个创建的类既充当了仓库,通过静态方法,也充当了枚举本身。
有两个抽象方法需要实现。使用seed方法用枚举填充类,并通过identifierAttribute方法返回该属性的名称,以告诉类哪个属性标识枚举。
一个示例实现可能如下所示
use JosKolenberg\Enum\Enum; class UserType extends Enum { protected $id; protected $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } /** * Seed the class with Enum instances * * @return array */ protected static function seed() { return [ new static('dev', 'Developer'), new static('admin', 'Administrator'), new static('user', 'User'), ]; } /** * Return the name of the attribute which stores the identifier * * @return string */ protected function identifierAttribute() { return 'id'; } }
接下来,这个类可以像其他任何类一样设计。
仓库
该类提供了以下静态“仓库类似”方法
get()
返回具有给定标识符的枚举。random()
返回一个随机标识符(例如,用于播种)。collection()
返回包含所有枚举的Illuminate\Support\Collection。identifiers()
返回只包含枚举标识符的Illuminate\Support\Collection。exists()
告诉是否具有给定标识符的枚举存在。
枚举也可以使用与标识符相同名称的魔法方法访问。例如,UserType::get('dev')
等于UserType::dev()
枚举
枚举本身有以下方法
identifier()
返回实例的标识符值(例如,前面的示例中的'dev'或'admin')。equals(Enum $enum)
检查给定的枚举是否与当前枚举匹配。
所有定义的属性默认都是只读的
$value = $userType->value
通过。$userType->value = 'changed'
失败。
自定义类
如果同一类型的不同枚举应该有不同的行为,则可以扩展基本枚举到更具体的枚举。
class UserType extends Enum { protected $id; protected $name; protected static function seed() { return [ new UserTypeDev(), new UserTypeAdmin(), ]; } protected function identifierAttribute() { return 'id'; } } class UserTypeDev extends UserType{ public function __construct() { $this->id = 'dev'; $this->name = 'Developer'; } public function whatCanYouDo() { return 'I can code'; } } class UserTypeAdmin extends UserType{ public function __construct() { $this->id = 'admin'; $this->name = 'Administrator'; } public function whatCanYouDo() { return 'I can do some important stuff'; } }
如果您想扩展集合的功能,则可以使用静态newCollection方法将您自己的自定义集合分配给枚举。
protected static function newCollection(array $enums = []) { return new MyCustomEnumCollection($enums); }
预定义枚举
由于枚举通常只是标识符和可能显示值,因此有两个预定义枚举可以扩展。
扩展EnumWithId以获取只具有id属性的枚举
class UserStatus extends \JosKolenberg\Enum\EnumWithId { protected static function seed() { return [ new static('new'), new static('active'), new static('deleted'), ]; } }
或者扩展EnumWithIdAndName以获取具有id和name属性的枚举
class ExtendedUserType extends \JosKolenberg\Enum\EnumWithIdAndName { protected static function seed() { return [ new static('dev', 'Developer'), new static('admin', 'Administrator'), new static('user', 'User'), ]; } }
或者如果您希望所有枚举都具有不同的属性,则可以创建自己的基类。
祝您编码愉快!
Jos Kolenberg joskolenberg@gmail.com