matthewpageuk/laravel-bitty-enums 问题修复 & 功能扩展

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

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

matthewpageuk/laravel-bitty-enums

Composer 安装命令:

composer require matthewpageuk/laravel-bitty-enums

包简介

This package helps you use bitwise enums in PHP and Laravel.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads GitHub Issues

This package helps you use bitwise enums in your Laravel application if you choose to, think before you do. It provides an Enum Container, a trait for your model query scopes and a model attribute cast.

You can think of this as a hasMany relationship but using a single integer column to store the data.

bitty1

Installation

You can install the package into your Laravel project via composer:

composer require matthewpageuk/laravel-bitty-enums

Usage

Create an Enum

You can create a new enum using the bitty-enum:make Artisan command. This command will create a new enum class in the app/Enums directory with the cases you supply. It will ensure the values and names are suitable for use with the package.

php artisan bitty-enum:make Colours

To use your own enums with this package they must :

  • Implement the MatthewPageUK\BittyEnums\Contracts\BittyEnum interface.
  • Return type int
  • Values be a power of 2 starting from 1 in order

Invalid enums will throw an BittyEnumException when used with the container.

There is a current limit of 16 cases (bits) per enum. This can be overiding in your config files.

Example of a Colour enum:

use MatthewPageUK\BittyEnums\Contracts\BittyEnum;

enum Colour: int implements BittyEnum
{
    case Red = 1;
    case Green = 2;
    case Blue = 4;
    case White = 8;
    case Black = 16;
    case Pink = 32;
}

Using the Bitty Enum Container

The container is used to store the selected enum values. It is a wrapper around the integer value and provides methods to manage and check the values. It also performs validation on the values you set to prevent accidental misuse.

Creating a container

You can create a new container using the Contract binding in the Laravel app.

use App\Enums\Colour;
use MatthewPageUK\BittyEnums\Contracts\BittyContainer;

$container = app()->make(BittyContainer::class)->setClass(Colour::class);

This will create a container suitable for the Colour enum.

You can also use the MatthewPageUK\BittyEnums\Support\Container directly.

use MatthewPageUK\BittyEnums\Support\Container as BittyContainer;

$favouriteColours = (new BittyContainer(Colour::class))
    ->set(Colour::Red)
    ->set(Colour::Green)
    ->set(Colour::Blue);

Example usage

// Set values
$favouriteColours = app()->make(BittyContainer::class)
    ->setClass(Colour::class)
    ->set(Colour::Red)
    ->set(Colour::Green)
    ->set(Colour::Blue);

// Passing an array of values
$favouriteColours = app()->make(BittyContainer::class)
    ->setClass(Colour::class)
    ->set([Colour::Red, Colour::Green, Colour::Blue]);

// Unset a value
$favouriteColours->unset(Colour::Red);

// Check if the container has a value
if ($favouriteColours->has(Colour::Red)) {
    echo 'Red is one of your favourite colours';
}

// Check if the container has any of the values
if ($favouriteColours->hasAny([Colour::Red, Colour::Green])) {
    echo 'You like red or green';
}

// Check if the container has all of the values
if ($favouriteColours->hasAll([Colour::Red, Colour::Green])) {
    echo 'You like red and green';
}

// Pass another container to check if any of the values exist
if ($product->colours->hasAny($favouriteColours)) {
    echo 'This product is available in one of your favourite colours';
}

Container public methods

public function __construct(string $class, int $selected = 0);

public function clear(): BittyContainer;

public function getChoices(): array;

public function getValue(): int;

public function has(BittyEnum $choice): bool;

public function hasAll(array|BittyContainer $choices): bool;

public function hasAny(array|BittyContainer $choices): bool;

public function set(array|BittyContainer|BittyEnum $choice): BittyContainer;

public function setAll(): BittyContainer;

public function unset(array|BittyContainer|BittyEnum $choice): BittyContainer;

Validation

The container also perfoms validation on the values you set, throwing a BittyEnumException if you try to set an invalid value or have a malformed enum.

Model Attribute Cast

You can cast the integer column on your models to a BittyEnumContainer using the MatthewPageUK\BittyEnums\Casts\BittyEnumCast cast.

You must pass the enum class you intend to use as the second parameter.

Your database column should be a BIGINT.

@todo see also bit limit on enum, how many can we have?

Example Laravel Model

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->unsignedBigInteger('colours');
    $table->unsignedInteger('price');
    $table->timestamps();
});

use App\Enums\Colour;
use MatthewPageUK\BittyEnums\Casts\BittyEnumCast;

class Product extends Model
{
    protected $casts = [
        'colours' => BittyEnumCast::class . ':' . Colour:class,
    ];
}

You can now use the container methods to update and retrieve the enum values direct from your model attribute.

Example Usage

$product = Product::find(1);
$product->colours->set(Colour::Blue)->unset(Colours::Red);
$product->save();
// Check if value exists
$product = Product::find(1);
if ($product->colours->has(Colour::Blue)) {
    echo 'This product is available in blue';
}
// Check if any of the values exist
$customerPreferences = app()->make(BittyContainer::class)
    ->setClass(Colour::class)
    ->set(Colour::Blue)
    ->set(Colour::Red)
    ->set(Colour::Green);

$product = Product::find(1);
if ($product->colours->hasAny($customerPreferences)) {
    echo 'This product is available in one of the customers preferred colours';
}

Scoped Queries

To access the scoped queries in your model you need to use the MatthewPageUK\BittyEnums\Traits\WithBittyEnumQueryScope trait.

You should also ensure your model has the BittyEnumCast set on the column you want to query.

Example Model

use App\Enums\Colours;
use MatthewPageUK\BittyEnums\Traits\WithBittyEnumQueryScope;

class Product extends Model
{
    use WithBittyEnumQueryScope;

    ...
}

Example Queries

// Products with the colour blue
Product::whereBittyEnumHas('colours', Colour::Blue)->get();

// Products with the colour blue or red
Product::whereBittyEnumHasAny('colours', [Colour::Blue, Colour::Red])->get();

// Products with the colour blue and red
Product::whereBittyEnumHasAll('colours', [Colour::Blue, Colour::Red])->get();

// Products without the colour blue
Product::whereBittyEnumDoesntHave('colours', Colour::Blue)->get();

// Products without the colour blue or red
$customerPreferences = new BittyEnumContainer(Colour::class)
    ->set(Colour::Blue)
    ->set(Colour::Red);

Product::whereBittyEnumDoesntHaveAny('colours', $customerPreferences)->get();

Methods accepting multiple choices can be an array of BittyEnum or a BittyEnumContainer.

A BittyEnumException will be thrown if you pass the incorrect type or invalid enum to the query scope.

Config settings

You can set the maximum number of bits for the container in the config file.

return [
    'max_bits' => 16,
];

Package Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

matthewpageuk/laravel-bitty-enums 适用场景与选型建议

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

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

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

围绕 matthewpageuk/laravel-bitty-enums 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 13.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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