konekt/enum-eloquent 问题修复 & 功能扩展

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

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

konekt/enum-eloquent

Composer 安装命令:

composer require konekt/enum-eloquent

包简介

Enum attribute casting for Eloquent models

README 文档

README

Tests Packagist Stable Version Packagist downloads StyleCI MIT Software License

This package provides support for auto casting konekt enum fields in Eloquent models.

Supported Konekt Enum versions are 2.x, 3.x and 4.x with Eloquent (Laravel) 8 - 13

Changelog

Installation

composer require konekt/enum-eloquent

Usage

  1. Add the CastsEnums trait to your model
  2. Define the attributes to be casted via the protected $enums property on the model

Example

The Enum:

namespace App;

use Konekt\Enum\Enum;

class OrderStatus extends Enum
{
    const __DEFAULT = self::PENDING; 
    // const __default = self::PENDING; // usage of default in v2.x 

    const PENDING   = 'pending';
    const CANCELLED = 'cancelled';
    const COMPLETED = 'completed';

}

The Model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Konekt\Enum\Eloquent\CastsEnums;

class Order extends Model
{
    use CastsEnums;

    protected $enums = [
        'status' => OrderStatus::class
    ];
}

Client code:

$order = Order::create([
    'status' => 'pending'
]);

// The status attribute will be an enum object:
echo get_class($order->status);
// output: App\OrderStatus

echo $order->status->value();
// output: 'pending'

echo $order->status->isPending() ? 'yes' : 'no';
// output: yes

echo $order->status->isCancelled() ? 'yes' : 'no';
// output: no

// You can assign an enum object as attribute value:
$order->status = OrderStatus::COMPLETED();
echo $order->status->value();
// output: 'completed'

// It also works with mass assignment:
$order = Order::create([
    'status' => OrderStatus::COMPLETED()    
]);

echo $order->status->value();
// output 'completed'

// It still accepts scalar values:
$order->status = 'completed';
echo $order->status->isCompleted() ? 'yes' : 'no';
// output: yes

// But it doesn't accept scalar values that aren't in the enum:
$order->status = 'negotiating';
// throws UnexpectedValueException
// Given value (negotiating) is not in enum `App\OrderStatus`

Resolving Enum Class Runtime

It is possible to defer the resolution of an Enum class to runtime.

It happens using the ClassName@method notation known from Laravel.

This is useful for libraries, so you can 'late-bind' the actual enum class and let the user to extend it.

Example

The Model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Konekt\Enum\Eloquent\CastsEnums;

class Order extends Model
{
    use CastsEnums;

    protected $enums = [
        'status' => 'OrderStatusResolver@enumClass'
    ];
}

The Resolver:

namespace App;

class OrderStatusResolver
{
    /**
     * Returns the enum class to use as order status enum
     *
     * @return string
     */
    public static function enumClass()
    {
        return config('app.order.status.class', OrderStatus::class);
    }
}

This way the enum class becomes configurable without the need to modify the Model code.

Laravel Collective Forms Compatibility

Laravel Collective Forms Package provides the Form facade known from Laravel v4.x.

In case you want to use the Forms package with this one, you need to add the EnumsAreCompatibleWithLaravelForms trait to your model, next to CastsEnums.

This will fix a problem where the forms package detects the enum label instead of its actual value as the value of the field.

It is being done by adding the (undocumented) getFormValue() method to the model, that is being used by the forms library to obtain form field value.

Enjoy!

For detailed usage of konekt enums refer to the Konekt Enum Documentation.

konekt/enum-eloquent 适用场景与选型建议

konekt/enum-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.12M 次下载、GitHub Stars 达 63, 最近一次更新时间为 2017 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.12M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 64
  • 点击次数: 30
  • 依赖项目数: 11
  • 推荐数: 0

GitHub 信息

  • Stars: 63
  • Watchers: 5
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-05