tourze/enum-extra

PHP枚举增强

0.0.5 2025-04-19 08:34 UTC

This package is auto-updated.

Last update: 2025-04-19 08:35:24 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 for easier data handling and UI integration.

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
  • Boolean enum implementation with useful helpers
  • Tree data structure support for hierarchical data
  • Data fetcher interfaces for standardized data retrieval

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();
// Result: [['label' => 'Active', 'text' => 'Active', 'value' => 'active', 'name' => 'Active'], ...]

// Convert single case to array
$array = Status::ACTIVE->toArray();
// Result: ['value' => 'active', 'label' => 'Active']

Available Interfaces

Labelable

This interface provides a way to add human-readable labels to enum cases.

interface Labelable
{
    public function getLabel(): string;
}

Itemable

This interface allows converting enum cases to select option items.

interface Itemable
{
    public function toSelectItem(): array;
}

Selectable

This interface provides methods to generate select options from enum cases.

interface Selectable
{
    public static function genOptions(): array;
}

SelectDataFetcher

This interface standardizes data fetching for select components.

interface SelectDataFetcher
{
    public function genSelectData(): iterable;
}

TreeDataFetcher

This interface provides methods to generate hierarchical tree data.

interface TreeDataFetcher
{
    public function genTreeData(): array;
}

Features in Detail

Select Options Generation

Convert enum cases to select options format for UI components:

$options = Status::genOptions();

You can filter options using environment variables:

// In your .env file or server configuration
$_ENV['enum-display:App\\Enums\\Status-inactive'] = false;

// Now Status::genOptions() will exclude the INACTIVE option

BoolEnum

A ready-to-use boolean enum implementation:

use Tourze\EnumExtra\BoolEnum;

$value = BoolEnum::YES;
$boolValue = $value->toBool(); // true

// Generate boolean options
$options = BoolEnum::genBoolOptions();
// Result: [['label' => '是', 'text' => '是', 'value' => true, 'name' => '是'], ...]

Array Conversion

Convert enum cases to array format for easy serialization:

$array = Status::ACTIVE->toArray();
// Result: ['value' => 'active', 'label' => 'Active']

Contributing

Please see CONTRIBUTING for details.

License

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