cuiz / magic-constant
PHP 魔术常量,比枚举更强大
2.1.0
2024-07-18 14:53 UTC
Requires
- php: 8.2.* || 8.3.*
Requires (Dev)
- phpstan/phpstan: 1.11.7
- phpunit/phpunit: ^11.0
README
此库允许您创建类似枚举的类,每个键都支持多种格式。
它有助于在代码中表示 魔术数字和字符串。
示例
假设您的代码需要与两个关于某些合同的服务进行交互。
表示一个活动合同
- 服务 A 使用
active
- 服务 B 使用
10
使用魔术常量,您可以声明以下类
use CuyZ\MagicConstant\MagicConstant; class ContractStatus extends MagicConstant { protected const ACTIVE = [ self::FORMAT_SERVICE_A => 'active', self::FORMAT_SERVICE_B => 10, ]; // Others status... public const FORMAT_SERVICE_A = 'a'; public const FORMAT_SERVICE_B = 'b'; }
然后您可以像这样使用它
// Instead of doing this: if ($status === 'active' || $status === 10) { // } // You can do this: if ($status->equals(ContractStatus::ACTIVE())) { // }
安装
$ composer require cuyz/magic-constant
用法
use CuyZ\MagicConstant\MagicConstant; /** * You can declare static methods to help with autocompletion: * * @method static Example FOO() * @method static Example BAR() * @method static Example FIZ() */ class Example extends MagicConstant { // Only protected constants are used as keys protected const FOO = 'foo'; // A key can have multiple possible formats for it's value protected const BAR = ['bar', 'BAR', 'b']; // You can use an associative array to declare formats protected const FIZ = [ self::FORMAT_LOWER => 'fiz', self::FORMAT_UPPER => 'FIZ', ]; // Using constants for formats is not mandatory public const FORMAT_LOWER = 'lower'; public const FORMAT_UPPER = 'upper'; }
然后您可以在任何地方使用此类
// As a parameter typehint and/or a return typehint function hello(Example $example): Example { // } hello(new Example('foo')); // You can also use constants keys as a static method hello(Example::BAR());
方法
获取实例值
echo (new Example('foo'))->getValue(); // 'foo' // You can specify the desired output format echo (new Example('FIZ'))->getValue(Example::FORMAT_LOWER); // 'fiz'
获取实例键
$constant = new Example('b'); echo $constant->getKey(); // 'BAR'
获取所有可能的格式实例
$constant = new Example('fiz'); echo $constant->getAllFormats(); // [new Example('fiz'), new Example('FIZ')]
获取实例的所有可能值
$constant = new Example('BAR'); echo $constant->getAllValues(); // ['bar', 'BAR', 'b']
返回第一个格式值的新实例
$constant = new Example('BAR'); echo $constant->normalize(); // new Example('bar')
比较实例
(new Example('foo'))->equals(new Exemple('bar')); // false (new Example('foo'))->equals(null); // false (new Example('fiz'))->equals(new Exemple('FIZ')); // true (new Example('b'))->equals(new Exemple('b')); // true
如果至少有一个元素相等,则返回 true
$constant = new Example('foo'); $constant->in([new Exemple('bar'), null, 'foo']); // false $constant->in([new Exemple('foo'), null, 'foo']); // true
获取魔术常量类的所有键
Example::keys(); // ['FOO', 'BAR', 'FIZ']
获取可能的值关联数组
Example::values(); [ 'FOO' => new Example('foo'), 'BAR' => new Example('bar'), 'FIZ' => new Example('fiz'), ]; // You can specify a regex pattern to match certain keys Example::values('/^F(.+)/'); [ 'FOO' => new Example('foo'), 'FIZ' => new Example('fiz'), ];
获取所有键及其相关值
Example::toArray(); [ 'FOO' => ['foo'], 'BAR' => ['bar', 'BAR', 'b'], 'FIZ' => ['fiz', 'FIZ'], ];
检查值是否有效
Example::isValidValue('foo'); // true Example::isValidValue('hello'); // false
检查键是否有效
Example::isValidKey('BAR'); // true Example::isValidKey('HELLO'); // false
返回任何值的键
Example::search('foo'); // 'FOO' Example::search('b'); // 'BAR' Example::search('hello'); // false