cuyz/magic-constant
Composer 安装命令:
composer require cuyz/magic-constant
包简介
PHP Magic Constants, even more powerful than an Enum
README 文档
README
This library allows you to create enum-like classes that support multiple formats for each key.
It helps represent magic numbers and strings in code.
Example
Let's say your code has to interact with two services about some contracts.
To represent an active contract:
- Service A uses
active - Service B uses
10
Using a magic constant you declare the following class:
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'; }
You can then use it like this:
// Instead of doing this: if ($status === 'active' || $status === 10) { // } // You can do this: if ($status->equals(ContractStatus::ACTIVE())) { // }
Installation
$ composer require cuyz/magic-constant
Usage
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'; }
You can then use the class everywhere:
// 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());
Methods
Get an instance value
echo (new Example('foo'))->getValue(); // 'foo' // You can specify the desired output format echo (new Example('FIZ'))->getValue(Example::FORMAT_LOWER); // 'fiz'
Get an instance key
$constant = new Example('b'); echo $constant->getKey(); // 'BAR'
Get instances with all possible formats
$constant = new Example('fiz'); echo $constant->getAllFormats(); // [new Example('fiz'), new Example('FIZ')]
Get all possible values for an instance
$constant = new Example('BAR'); echo $constant->getAllValues(); // ['bar', 'BAR', 'b']
Returns a new instance where the value is from the first format
$constant = new Example('BAR'); echo $constant->normalize(); // new Example('bar')
Compares instances
(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
Returns true if at least one element is equal
$constant = new Example('foo'); $constant->in([new Exemple('bar'), null, 'foo']); // false $constant->in([new Exemple('foo'), null, 'foo']); // true
Get all keys for a magic constant class
Example::keys(); // ['FOO', 'BAR', 'FIZ']
Get an associative array of possible values
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'), ];
Get all keys and associated values
Example::toArray(); [ 'FOO' => ['foo'], 'BAR' => ['bar', 'BAR', 'b'], 'FIZ' => ['fiz', 'FIZ'], ];
Check if a value is valid
Example::isValidValue('foo'); // true Example::isValidValue('hello'); // false
Check if a key is valid
Example::isValidKey('BAR'); // true Example::isValidKey('HELLO'); // false
Returns the key of any value
Example::search('foo'); // 'FOO' Example::search('b'); // 'BAR' Example::search('hello'); // false
cuyz/magic-constant 适用场景与选型建议
cuyz/magic-constant 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 61.99k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2020 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「enum」 「Magic Number」 「magic constant」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cuyz/magic-constant 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cuyz/magic-constant 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cuyz/magic-constant 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fast-prototyping magic accessors trait for Doctrine entities
Enum type behavior for Yii2 based on class constants
Eloquent extension providing ability to use a lot of subquery functions like fromSubquery or leftJoinSubquery
A PHP Abstract Enum Class
A package for working with arbitrary precision numbers in Laravel.
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
统计信息
- 总下载量: 61.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-30