病毒15 / 枚举
用于创建枚举对象的简单PHP库。
0.5.1
2018-02-01 14:28 UTC
Requires
- php: >=7
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-09-24 17:11:37 UTC
README
用于创建枚举对象的简单PHP库。
示例
定义新的枚举类
use Enum\Enum; // defining our Enum class final class Example extends Enum { const FOO = 1; const BAR = 2; const YOLO = 3; }
基本用法
$foo = Example::get(Example::FOO); $bar = Example::get('bar'); $yolo = Example::YOLO(); function test(Example $enum) { if ($enum->is(Example::FOO)) { echo 'foo'; } else if ($enum->is(Example::BAR)) { echo 'bar'; } else { echo $enum; } } test($foo); // foo test($bar); // bar test($yolo); // Yolo
获取枚举实例,列出键和值
Example::get(Example::FOO); // instance of Example Example::FOO(); // shortcut Example::all(); // array of Example instances // keys and values Example::keys(); // array of keys ['FOO', 'BAR', 'YOLO'] Example::values(); // array of values [1, 2, 3] // checking if value exists Example::has(3); // true Example::has(4); // false
获取单个枚举属性
$foo = Example::FOO(); $foo->key(); // 'FOO' $foo->value(); // 1
比较枚举
$foo = Example::FOO(); $foo->is(1); // true $foo->is(Example::FOO); // true $foo->is($foo); // true // comparing with strict option (type comparison) $foo->is($foo, true); // true $foo->is(1, true); // false // searching in array $foo->in([1,2,3]); // true $foo->in([2,3]); // false
您还可以进行静态调用并指定枚举类
Enum::get(Example::FOO, Example::class); Enum::all(Example::class); Enum::keys(Example::class); Enum::values(Example::class); Enum::has(1, Example::class);
开发和测试
为了开发这个库,我们使用 docker 和 docker-compose。安装这些工具后,您应该运行
bash
docker-compose run enum bash
然后在docker控制台运行
composer install
composer test
许可证
此库根据 MIT许可证 发布。