rexlabs / enum
PHP 的枚举(enum)实现
Requires
- php: >=7.0 <8.3
Requires (Dev)
- phpunit/phpunit: ^6.5.7|^7.5|^8.5
This package is auto-updated.
Last update: 2024-09-20 03:33:29 UTC
README
警告 如果您正在使用 PHP 8.1 或更高版本,我们建议您使用 原生 PHP 枚举 代替此包。
我们可能会发布一些维护补丁,但对此包的支持将不再继续。
请随意分支我们的代码,并按您的需求进行修改。
Enum PHP 库
概述
此库为 PHP 提供了枚举(Enum)/枚举(Enumeration)的实现。
为什么使用此库
- 非常简单易用。
- 复杂度可以通过提供
map()
方法进行可选映射。 - 允许在方法和方法、类之间传递枚举值时进行类型提示。
用法
首先创建一个新的类,该类扩展 \Rexlabs\Enum\Enum
并执行以下操作;
- 声明您的常量
- 可选:提供
map()
方法
示例
<?php namespace Rexlabs\Enum\Readme; use Rexlabs\Enum\Enum; /** * Class City * * @package Rexlabs\Enum\Readme * * @method static static BRISBANE() * @method static static MELBOURNE() * @method static static SYDNEY() */ class City extends Enum { const BRISBANE = 'Brisbane'; const MELBOURNE = 'Melbourne'; const SYDNEY = 'Sydney'; // OPTIONAL - Provide a map() method if you would like to // map additional data, which will be available from the ->value() method public static function map(): array { return [ self::BRISBANE => ["state"=>"QLD", "population"=>""], self::MELBOURNE => ["state"=>"VIC", "population"=>"5m"], self::SYDNEY => ["state"=>"NSW", "population"=>"5m"], ]; } } // Static access echo City::BRISBANE; // "Brisbane" echo City::MELBOURNE; // "Melbourne" City::names(); // (array)["BRISBANE", "BRISBANE", "SYDNEY"] City::keys(); // (array)["Brisbane", "Melbourne", "Sydney"] City::keyForName('BRISBANE'); // "Brisbane" City::nameForKey('Melbourne'); // "MELBOURNE" City::isValidKey('Sydney'); // (boolean)true City::isValidKey('Paris'); // (boolean)false // Getting an instance - all return a City instance. $city = City::BRISBANE(); $city = City::instanceFromName('BRISBANE'); $city = City::instanceFromKey('Brisbane'); // Working with an instance $city->name(); // "BRISBANE" $city->key(); // "Brisbane" $city->value()['population']; // null - no value mapped $city->is(City::BRISBANE); // (boolean)true $city->is(City::BRISBANE()); // (boolean)true $city->is(City::SYDNEY()); // (boolean)false $city->isNot(City::SYDNEY()); // (boolean)true $city->isAnyOf([City::BRISBANE()]); // (boolean)true $city->isNoneOf([City::BRISBANE()]); // (boolean)false // Or ... City::SYDNEY()->key(); // "Sydney" City::SYDNEY()->value(); // (array)["state"=>"NSW", "population"=>"5m"]
依赖项
- PHP 7.0 或更高。
安装
在您的项目中安装
composer require rexlabs/enum
类型提示
现在您可以将 Enum
对象作为依赖项进行类型提示
<?php function announceCity(City $city) { echo "{$city->key()} is located in {$city->value()["state"]}, population: {$city->value()["population"]}\n"; } // Get a new instance announceCity(City::SYDNEY()); // "Sydney is located in NSW, population: 5m"
实例方法
Enum
的每个实例都提供了以下方法
name()
返回常量名称。
$enum->name();
key()
返回在 const MY_CONST = 'key'
声明中分配给常量的值/键。
$enum->key();
value()
返回映射(如果有)的值(在 map()
返回的数组中)。如果没有映射值,则此方法返回 null
。
$enum->value();
is(Enum|string $compare)
如果此实例与给定的常量键或枚举实例相同,则返回 true。
$enum->is(City::SYDNEY); // Compare to constant key $enum->is(City::SYDNEY()); // Compare to instance
__toString()
定义 __toString()
方法以返回常量名称。
(string)City::SYDNEY(); // "SYDNEY"
静态方法
map()
返回一个将常量键映射到值的数组。此方法可以可选地在子类中实现。默认实现返回一个将键映射到 null
的数组。
instances()
返回一个枚举实例的数组。
keys()
返回常量键的数组。
values()
返回在 map()
中定义的值的数组。如果没有实现 map()
,则返回一个包含 null
的数组。
names()
返回一个包含类中声明的所有常量名称的数组。
namesAndKeys()
返回一个关联数组,其中 CONSTANT_NAME => key,用于类中声明的所有常量名称。
keyForName(string $name)
返回给定常量名称的键。
nameForKey(string $key)
返回给定键的常量名称(keyForName
的逆操作)。
valueForKey(string $key)
返回给定键的值(或未映射则为 null
)(在 map()
方法中声明)。
keyForValue(mixed $value)
返回给定值的键(在 map()
方法中声明)。
注意:如果
map()
方法中包含重复的值,则只返回第一个键。
instanceFromName($name)
从给定的常量名称创建此 Enum 的实例。
instanceFromKey($key)
从给定的键创建此 Enum 的实例。
isValidKey(string $key)
如果给定的键存在,则返回 true。
isValidName(string $name)
如果给定的常量名称(区分大小写)在类中已声明,则返回 true。
requireValidKey(string $key)
如果给定的键不存在,则抛出 \Rexlabs\Enum\Exceptions\InvalidKeyException
。
测试
运行测试
composer tests
运行覆盖率报告
composer coverage
覆盖率报告输出到 ./tests/report/index.html
贡献
欢迎贡献,请提交拉取请求或创建问题。您提交的代码应使用 PSR-1/PSR-2 标准格式化。
关于
- 作者: Jodie Dunlop
- 许可证: MIT
- 版权 (c) 2018 Rex Software Pty Ltd