damianulan/php-enumerable 问题修复 & 功能扩展

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

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

damianulan/php-enumerable

最新稳定版本:1.0.0

Composer 安装命令:

composer require damianulan/php-enumerable

包简介

Extensive enum class support for plain PHP and Laravel projects

README 文档

README

Static Badge   Licence   Static Badge

Description

This package provides extensive enumeration support for PHP and Laravel projects, as a substitute for PHP built-in enums.

Installation

You can install the package via composer in your laravel project:

composer require damianulan/php-enumerable

The package will automatically register itself.

Usage

Use Enumerable\Enum class to create enumeration in plain php, in laravel project use Enumerable\Laravel\Enum class, which provides additional casting support. Class Enumerable\Enum instances mimic implementation of BackedEnum and UnitEnum.

use Enumerable\Enum;

class CampaignStage extends Enum
{
    public const PENDING = 'pending';

    public const IN_PROGRESS = 'in_progress';

    public const COMPLETED = 'completed';
}

Check the examples below:

StageEnum::cases(); // returns an assoc array of all enum cases with enum instance
StageEnum::values(); // returns an array of all enum values
StageEnum::labels(); // should return an assoc array of all enum cases with human-readable label. This method should be declared in Enum child class. Accessible by attribue `label`.
// ['enum_value' => 'Human-readable label']

$stage = StageEnum::fromValue('pending'); // returns enum instance
$stage->label; // returns human-readable label if labels() method is declared in child class
$stage->value; // returns enum value

StageEnum::Pending; // returns enum value, not its instance

In Laravel assign your Enumerable\Laravel\Enum to yout model's casts property:

protected $casts = array(
    'stage' => StageEnum::class,
);

It will return StageEnum instance instead of string when accessing model's stage property.

More examples

$stage->is('pending'); // returns true if enum value is equal to given string
$stage->isNot('pending'); // returns true if enum value is not equal to given string
$stage->equals($otherEnum); // returns true if enum value is equal to given enum instance

Contact & Contributing

Any question You can submit to damian.ulan@protonmail.com.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-30