承接 cuyz/magic-constant 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cuyz/magic-constant

Composer 安装命令:

composer require cuyz/magic-constant

包简介

PHP Magic Constants, even more powerful than an Enum

README 文档

README

Latest Version on Packagist Total Downloads Software License

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 cuyz/magic-constant 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 61.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-30