tourze/enum-extra

PHP枚举增强

0.0.2 2025-03-27 07:20 UTC

This package is auto-updated.

Last update: 2025-03-28 18:50:43 UTC


README

English | 中文

Latest Version Total Downloads License

A PHP package that enhances PHP 8.1+ enums with additional functionality, providing commonly used enum extensions.

Features

  • Convert enum cases to select options with custom labels
  • Environment-based option filtering with enum-display:{enum}-{value}
  • Array conversion utilities for easy data transformation
  • Interface support for flexible implementation
  • Type-safe enum operations

Requirements

  • PHP 8.1 or higher

Installation

composer require tourze/enum-extra

Quick Start

use Tourze\EnumExtra\Itemable;
use Tourze\EnumExtra\ItemTrait;
use Tourze\EnumExtra\Labelable;
use Tourze\EnumExtra\Selectable;
use Tourze\EnumExtra\SelectTrait;

enum Status: string implements Labelable, Itemable, Selectable
{
    use ItemTrait;
    use SelectTrait;

    case ACTIVE = 'active';
    case INACTIVE = 'inactive';

    public function getLabel(): string
    {
        return match($this) {
            self::ACTIVE => 'Active',
            self::INACTIVE => 'Inactive',
        };
    }
}

// Generate select options
$options = Status::genOptions();

// Convert single case to array
$array = Status::ACTIVE->toArray();

Available Interfaces

  • Labelable: For implementing custom labels
  • Itemable: For select item conversion
  • Selectable: For options generation

Features in Detail

Select Options Generation

  • Convert enum cases to select options format
  • Support for custom labels
  • Environment-based filtering using enum-display:{enum}-{value}

Array Conversion

  • Convert enum cases to array format
  • Includes both value and label

Contributing

Please see CONTRIBUTING for details.

License

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