承接 josezenem/php-enums-extended 相关项目开发

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

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

josezenem/php-enums-extended

Composer 安装命令:

composer require josezenem/php-enums-extended

包简介

PHP 8.1 Enums Extended adds additional functionality when working with PHP 8.1+ Enums.

README 文档

README

Latest Stable Version Tests Codacy Badge Total Downloads PHP Version Require

PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.

enum StatusEnum:int
{
    case Closed = 0;
    case Open = 1;
    case PENDING_APPROVAL = 2;
}

// Given a new Blog() that uses the enum trait, you can do things like:
$blog->status->isOpen() // Will return boolean
$blog->status->equals(StatusEnum::Open, StatusEnum::Closed)

// Normalization happens in the background allowing these scenarios 
$blog->status->isPendingApproval();
$blog->status->isPENDING_APPROVAL();

StatusEnum::Open() // Will return ->value, vs doing StatusEnum::Open->value
StatusEnum::PendingApproval()
StatusEnum::PENDING_APPROVAL()

Installation

You can install the package via composer:

composer require josezenem/php-enums-extended

Usage

Available Methods

Available Static Methods

equals()

Pass one or multiple Enum cases, will return boolean if one matches.

$blog->status->equals(StatusEnum::Closed, StatusEnum::Draft);

doesNotEqual()

Pass one or multiple Enum cases, will return boolean if it does not match.

$blog->status->doesNotEqual(StatusEnum::Closed, StatusEnum::Draft);

isCall**()

Returns boolean if the current value matches the desired case. Methods with underscores can be accessed via camel case, as well as their regular name.

$blog->status->isDraft();

// Given StatusEnum::OPEN_ISSUE = 4;
// the following is acceptable.
$blog->status->isOpenIssue();
$blog->status->isOPEN_ISSUE();

options()

Will return an array of $val => $key.

$options = self::options()

// returns
$options = [
    'open' => 'Open',
    'closed' => 'Closed',
    'draft' => 'Draft',
]

optionsFlipped()

Will return an array of $key => $val.

$options = self::optionsFlipped()

// returns
$options = [
    'Open' => 'open',
    'Closed' => 'closed',
    'Draft' => 'draft',
]

names()

Will return an array of only names

$options = self::names()

// returns
$options = [
    'Open' => 'Open',
    'Closed' => 'Closed',
    'Draft' => 'Draft',
]

hasName()

Pass variable and confirm if the name is valid for the Enum

App\MyEnums\Type::hasName('Closed');
// Returns true

App\MyEnums\Type::hasName('close');
// Returns false

values()

Will return an array of only values

$options = self::values()

// returns
$options = [
    'open' => 'open',
    'closed' => 'closed',
    'draft' => 'draft',
]

hasValue()

Pass variable and confirm if the value is valid for the Enum

App\MyEnums\Type::hasValue('closed');
// Returns true

App\MyEnums\Type::hasValue('not a valid value for the enum');
// Returns false

call**()

Will allow you to grab the value of a field by calling it statically.

// Consider the following scenario, to get the value you would do:
// StatusEnum::Open->value
enum StatusEnum:int
{
    case Closed = 0;
    case Open = 1;
    case Draft = 2;
}

// You can instead get value directy by calling it statically
// Case Insensitive
StatusEnum::OPEN()
StatusEnum::Open()

Exception Handler

When using the magic methods, if the method calls do not exist, the system will throw

Josezenem\PhpEnumsExtended\Exceptions\EnumsExtendedException
// StatusEnum.php
// StatusEnum:int is used for the example, but supports :string and default of just StatusEnum
use Josezenem\PhpEnumsExtended\Traits\PhpEnumsExtendedTrait;

enum StatusEnum:int
{
    use PhpEnumsExtendedTrait;

    case Closed = 0;
    case Open = 1;
    case Draft = 2;
}

// Blog.php
class Blog
{
    public function __construct(
        public StatusEnum $status = StatusEnum::Open,
    ) {
    }
}



// Usage
$blog = new Blog();


// ->equals()
$blog->status->equals(StatusEnum::Open); // will return true if it matches
$blog->status->equals(StatusEnum::Closed, StatusEnum::Open); // Pass any number of params, will return true if it matches any of the parameters

// ->doesNotEqual()
$blog->status->doesNotEqual(StatusEnum::Closed); // will return true if it does not match
$blog->status->doesNotEqual(StatusEnum::Closed, StatusEnum::Draft)  // Pass any number of params, will return true if it does not match any of the parameters

// ->is** magic method
// the magic method takes camelCase allowing you to do boolean check against any field.
$blog->status->isOpen() // will return true or false

// ::options()
$options = StatusEnum::options();

// will output
//$options = [
//    0 => 'Closed',
//    1 => 'Open',
//    2 => 'Closed',
//];

// ::optionsFlipped()
$options = StatusEnum::optionsFlipped();

// will output
//$options = [
//    'Closed' => 0,
//    'Open' => 1,
//    'Closed' => 2,
//];

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

josezenem/php-enums-extended 适用场景与选型建议

josezenem/php-enums-extended 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70.5k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「enums」 「josezenem」 「php-enums-extended」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 josezenem/php-enums-extended 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 josezenem/php-enums-extended 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 70.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-02