PHPackages                             tourze/enum-extra - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tourze/enum-extra

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tourze/enum-extra
=================

PHP枚举增强

1.0.1(6mo ago)034.4k20MITPHPPHP ^8.2CI passing

Since Mar 24Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/tourze/enum-extra)[ Packagist](https://packagist.org/packages/tourze/enum-extra)[ RSS](/packages/tourze-enum-extra/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (9)Used By (20)

PHP Enum Extra
==============

[](#php-enum-extra)

[English](README.md) | [中文](README.zh-CN.md)

[![Latest Version](https://camo.githubusercontent.com/8f2c23605f5e16b2969d0759f73a092b67a5d124ae6f82cfb3d1064aa0748c45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f656e756d2d65787472612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/enum-extra)[![Total Downloads](https://camo.githubusercontent.com/3c85e55aed6c149347dfaa4431189c6ef5f67765f96170cb17ec73d141a1896f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f75727a652f656e756d2d65787472612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/enum-extra)[![License](https://camo.githubusercontent.com/ccfab104f4f9f9e493ea97ca69f79296717032641d4bf3439b276df16af9846f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f746f75727a652f656e756d2d65787472612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/enum-extra)

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
--------

[](#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
- Badge interface for EasyAdminBundle integration

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Installation
------------

[](#installation)

```
composer require tourze/enum-extra
```

Quick Start
-----------

[](#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
--------------------

[](#available-interfaces)

### Labelable

[](#labelable)

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

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

### Itemable

[](#itemable)

This interface allows converting enum cases to select option items.

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

### Selectable

[](#selectable)

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

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

### SelectDataFetcher

[](#selectdatafetcher)

This interface standardizes data fetching for select components.

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

### TreeDataFetcher

[](#treedatafetcher)

This interface provides methods to generate hierarchical tree data.

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

### BadgeInterface

[](#badgeinterface)

This interface provides badge constants for EasyAdminBundle integration.

```
interface BadgeInterface
{
    public const SUCCESS = 'success';
    public const WARNING = 'warning';
    public const DANGER = 'danger';
    public const INFO = 'info';
    public const PRIMARY = 'primary';
    public const SECONDARY = 'secondary';
    public const LIGHT = 'light';
    public const DARK = 'dark';
    public const OUTLINE = 'outline';

    public function getBadge(): string;
}
```

Features in Detail
------------------

[](#features-in-detail)

### Select Options Generation

[](#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

[](#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

[](#array-conversion)

Convert enum cases to array format for easy serialization:

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

### Badge Integration

[](#badge-integration)

Use badge interface for EasyAdminBundle styling:

```
use Tourze\EnumExtra\BadgeInterface;

enum UserStatus: string implements BadgeInterface, Labelable
{
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';
    case BANNED = 'banned';

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

    public function getBadge(): string
    {
        return match($this) {
            self::ACTIVE => self::SUCCESS,
            self::INACTIVE => self::WARNING,
            self::BANNED => self::DANGER,
        };
    }
}
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

License
-------

[](#license)

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

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance68

Regular maintenance activity

Popularity23

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~54 days

Total

8

Last Release

185d ago

Major Versions

0.1.0 → 1.0.02025-10-31

PHP version history (2 changes)0.0.1PHP ^8.1

1.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-enum-extra/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-enum-extra/health.svg)](https://phpackages.com/packages/tourze-enum-extra)
```

###  Alternatives

[vinicius73/seotools

A package containing SEO helpers.

245.2k](/packages/vinicius73-seotools)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
