commerceguys / enum
PHP 5.4+ 枚举库。
v1.0
2015-02-27 21:36 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-08-25 06:28:45 UTC
README
PHP 5.4+ 枚举库。
类常量通常用于表示允许值的集合。通过将它们组合在枚举类中,我们可以获得添加辅助方法、列出所有可能值以及验证值的能力。
namespace CommerceGuys\Addressing\Enum; use CommerceGuys\Enum\AbstractEnum; /** * Enumerates available locality types. */ final class LocalityType extends AbstractEnum { const CITY = 'city'; const DISTRICT = 'district'; // We can provide a getDefault() method here, or anything else. } LocalityType::getAll(); // ['CITY' => 'city', 'DISTRICT' => 'district'] LocalityType::getKey('city'); // 'CITY' LocalityType::exists('city'); // true LocalityType::assertExists('invalid value'); // InvalidArgumentException LocalityType::assertAllExist(['district', 'invalid value']); // InvalidArgumentException
同时,在 AddressFormat
// The AddressFormatInterface is now free of LOCALITY_TYPE_ constants. class AdressFormat implements AddressFormatInterface { public function setLocalityType($localityType) { LocalityType::assertExists($localityType); $this->localityType = $localityType; } }
创建这个库而不是重用 myclabs/php-enum 的原因是我们不想允许枚举被实例化。