kwaadpepper/enum 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kwaadpepper/enum

Composer 安装命令:

composer require kwaadpepper/enum

包简介

Enums to use for any project

关键字:

README 文档

README

GitHub Latest Version on Packagist Total Downloads GitHub Workflow Status

This package goal is to provide a complete solution to lack a long time php weakness : it do not have any enum support. This will change with php 8 enums proposition. In the mean time we still a support for this allowing us to generate generic and extendable applications (instead of have singletons sql tables..).

You will be able to use enums in any project using a simple definition, take a look at example Basic or a more sophisticated Example

Installation

Via Composer

$ composer require kwaadpepper/enum

Usage

All enum have two properties : label and value, only value has to be unique. Each enum can have multiple options all written on class comment as static methods, these have to be unique !

  • On Any project, take a look on examples to create an enum class.
  1. Invoke an enum value

    Days::mon()
  2. Compare enums

    Days::mon()->equals(Days::tue()) // false
    Days::mon()->equals(Days::mon()) // true
    Days::mon()->value === Days::tue()->value // false
    Days::mon()->value === Days::mon()->value // true
  3. Print an enum

    echo Days::mon(); // 2
    echo Days::tue(); // 4
    echo Days::mon()->label; // Monday

    As you can see enum implements the __toString method which you can override to display the label instead of the value. This default behavior is set like this for a better behavior in laravel.

  4. Serialise an enum

    echo json_encode(Days::mon()); // {"label":"Monday","value":2}

    Enums implement the JsonSerializable interface

  • On a Laravel project you can use enums in multiple ways
  1. As a property

    // Add theses to your model
     use CastsEnums;
     protected $enumCasts = [
         'day' => Days::class
     ];

    This will allow you model to store an enum in database using its value, and then cast the property to an enum when accessing it

  2. As a route parameter

    Define a route like the following

    Route::get('/days/{day}', function (Days $day) {
             return response()->json([$day]);
         })->middleware('bindings');
     // OR
     Route::get('/days/{day}', [DayController::class, 'getDay']);

    Then on your controller for the DayController example

    public function getDay(Request $request, Day $day) {
        return response()->json([$day]);
    }
  3. As a request parameter using validation

    Take a look at this request example.

    new EnumIsValidRule(Days::class)

    It use EnumIsValidRule to validate the parameter as being a valid enum value.

    Note that you may still have to cast your parameter to int if your enum value is an int, as enum check is strict

  4. As a model primary Key

    Take a look at unit test using the Report enum class from tests

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email github@jeremydev.ovh instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.

kwaadpepper/enum 适用场景与选型建议

kwaadpepper/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 kwaadpepper/enum 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-01