mpscholten / typesafe-enum
一个轻量级的、类型安全的枚举库
1.0.0
2015-07-08 14:21 UTC
Requires (Dev)
- phpunit/phpunit: ^4.7
This package is auto-updated.
Last update: 2024-09-22 20:25:58 UTC
README
PHP的一个轻量级、类型安全的枚举库。
使用它来提供具有自动补全支持的枚举。非常适合领域驱动设计应用程序。
开始使用
通过composer安装
composer install mpscholten/typesafe-enum
基本用法
class UserType extends \TypesafeEnum\Enum { public static function PAID() { return new UserType('paid'); } public static function FREE() { return new UserType('free'); } public function isPaid() { return $this->is('paid'); } public function isFree() { return $this->is('free'); } } class User { public function __construct($email, UserType $type) { $this->type = $type; } public function getType() { return $this->type; } } $user = new User('hello@example.com', UserType::PAID()); // Good $user->getType()->isPaid(); // true $user->getType()->isFree(); // false (string) $user->getType(); // "paid" $user = new User('hello@example.com', UserType::FREE()); // Good $user->getType()->isFree(); // true $user->getType()->isPaid(); // false (string) $user->getType(); // "free" $user = new User('hello@example.com', 'some string'); // Type error (PHP Catchable fatal error: Argument 2 passed to User::__construct() must be an instance of UserType, integer given, called in ...) $user = new User('hello@example.com', null); // Type error (PHP Catchable fatal error: Argument 2 passed to User::__construct() must be an instance of UserType, null given, called in ...)
扩展用法
class Temperature extends Enum { private $celsius; protected function __construct($value, $celsius) { parent::__construct($value); $this->celsius = $celsius; } public static function HOT() { return new Temperature('hot', 40); } public static function COLD() { return new Temperature('cold', 10); } public function isHot() { return $this->is('hot'); } public function isCold() { return $this->is('cold'); } /** * @return int */ public function getCelsius() { return $this->celsius; } } $temperature = Temperature::HOT(); $temperature->isHot(); // true $temperature->isCold(); // false $temperature->getCelsius(); // 40 (string) $temperature; // "hot" $temperature = Temperature::COLD(); $temperature->isCold(); // true $temperature->isHot(); // false $temperature->getCelsius(); // 10 (string) $temperature; // "cold"
如上图所示,将属性(在这种情况下为celsius)分配给枚举值集的每个值的方法是在类中添加一个新属性并重写构造函数。不要为此使用switch
语句。
这将确保枚举值集中的每个值始终分配有一个属性,因此分配的属性不会与值集不同步。
与doctrine2的使用
您可以通过使用所谓的内嵌实体来将此库与doctrine2一起使用
/** @Embeddable */ class UserType extends \TypesafeEnum\Enum { /** @Column(type = "smallint", name = "type") */ protected $value; // Override the `$value` property of \TypesafeEnum\Enum and apply mapping public static function PAID() { return new UserType(0); } public static function FREE() { return new UserType(1); } public function isPaid() { return $this->is(0); } public function isFree() { return $this->is(1); } } class User { /** @Embedded(class = "UserType", columnPrefix = false) */ private $type; public function __construct($email, UserType $type) { $this->type = $type; } }
测试
您可以使用make
运行phpunit
套件
make tests
贡献
请随意发送pull请求!