百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

enum

> 编程语言
开源

零依赖包,用于增强枚举功能。

360 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

零依赖包,用于增强枚举功能。

Enum

[![Author][ico-author]][link-author] [![PHP Version][ico-php]][link-php] [![Latest Version][ico-version]][link-packagist] ![Software License][ico-license] [![Build Status][ico-actions]][link-actions] [![Code Quality][ico-code-quality]][link-code-quality] [![Coverage][ico-coverage]][link-coverage] [![PHPStan Level][ico-phpstan]][link-phpstan] [![Total Downloads][ico-downloads]][link-downloads]

Zero-dependencies package to supercharge enum functionalities.

[!TIP] Need to supercharge enums in a Laravel application?

Consider using Laravel Enum instead.

Install

Via Composer:

bash
composer require cerbero/enum

Usage

  • ⚖️ Comparison
  • ️ Meta
  • Hydration
  • Enum operations
  • Cases collection
  • Magic
  • Self-awareness
  • Console commands
    • ️ annotate
    • ️ make
    • ts

To supercharge our enums with all the features provided by this package, we can let our enums use the Enumerates trait:

php
use Cerbero\Enum\Concerns\Enumerates;

enum PureEnum
{
    use Enumerates;

    case One;
    case Two;
    case Three;
}

enum BackedEnum: int
{
    use Enumerates;

    case One = 1;
    case Two = 2;
    case Three = 3;
}

⚖️ Comparison

We can check whether an enum includes some names or values. Pure enums check for names and backed enums check for values:

php
PureEnum::has('One'); // true
PureEnum::has('four'); // false
PureEnum::doesntHave('One'); // false
PureEnum::doesntHave('four'); // true

BackedEnum::has(1); // true
BackedEnum::has(4); // false
BackedEnum::doesntHave(1); // false
BackedEnum::doesntHave(4); // true

Otherwise we can check whether cases match a given name or value:

php
PureEnum::One->is('One'); // true
PureEnum::One->is(1); // false
PureEnum::One->is('four'); // false
PureEnum::One->isNot('One'); // false
PureEnum::One->isNot(1); // true
PureEnum::One->isNot('four'); // true

BackedEnum::One->is(1); // true
BackedEnum::One->is('1'); // false
BackedEnum::One->is(4); // false
BackedEnum::One->isNot(1); // false
BackedEnum::One->isNot('1'); // true
BackedEnum::One->isNot(4); // true

Comparisons can also be performed against arrays:

php
PureEnum::One->in(['One', 'four']); // true
PureEnum::One->in([1, 4]); // false
PureEnum::One->notIn(['One', 'four']); // false
PureEnum::One->notIn([1, 4]); // true

BackedEnum::One->in([1, 4]); // true
BackedEnum::One->in(['One', 'four']); // false
BackedEnum::One->notIn([1, 4]); // false
BackedEnum::One->notIn(['One', 'four']); // true

️ Meta

Meta add extra information to a case. Meta can be added by implementing a public non-static method and/or by attaching #[Meta] attributes to cases:

php
enum BackedEnum: int
{
    use Enumerates;

    #[Meta(color: 'red', shape: 'triangle')]
    case One = 1;

    #[Meta(color: 'green', shape: 'square')]
    case Two = 2;

    #[Meta(color: 'blue', shape: 'circle')]
    case Three = 3;

    public function isOdd(): bool
    {
        return $this->value % 2 != 0;
    }
}

The above enum defines 3 meta for each case: color, shape and isOdd. The #[Meta] attributes are ideal to declare static information, whilst public non-static methods are ideal to declare dynamic information.

To access a case meta, we can simply call the method having the same name of the wanted meta:

php
BackedEnum::Two->color(); // green

[!TIP] Our IDE can autocomplete meta methods thanks to the annotate command.

#[Meta] attributes can also be attached to the enum itself to provide default values when a case does not declare its own meta values:

php
#[Meta(color: 'red', shape: 'triangle')]
enum BackedEnum: int
{
    use Enumerates;

    case One = 1;

    #[Meta(color: 'green', shape: 'square')]
    case Two = 2;

    case Three = 3;
}

In the above example all cases have a red color and a triangle shape, except the case Two that overrides the default meta values.

Meta can also be leveraged for the hydration, elaboration and collection of cases.

Hydration

An enum case can be instantiated from its own name, value (if backed) or meta:

…

Hydrating from meta can return multiple cases. To facilitate further processing, such cases are collected into a CasesCollection.

Enum operations

A number of operations can be performed against an enum to affect all its cases:

…

Cases collection

When an enum operation can return multiple cases, they are collected into a CasesCollection which provides a fluent API to perform further operations on the set of cases:

php
PureEnum::filter('isOdd')->sortBy('color')->pluck('color', 'name'); // ['Three' => 'blue', 'One' => 'red']

Cases can be collected by calling collect() or any other enum operation returning a CasesCollection:

php
PureEnum::collect(); // CasesCollection[PureEnum::One, PureEnum::Two, PureEnum::Three]

BackedEnum::only('One', 'Two'); // CasesCollection[BackedEnum::One, BackedEnum::Two]

We can iterate a cases collection within any loop:

php
foreach (PureEnum::collect() as $case) {
    echo $case->name;
}

All the enum operations listed above are also available when dealing with a collection of cases.

Magic

Enums can implement magic methods to be invoked or to handle calls to inaccessible methods. By default when calling an inaccessible static method, the name or value of the case matching the missing method is returned:

php
PureEnum::One(); // 'One'

BackedEnum::One(); // 1

[!TIP] Our IDE can autocomplete cases static methods thanks to the annotate command.

We can also obtain the name or value of a case by simply invoking it:

php
$case = PureEnum::One;
$case(); // 'One'

$case = BackedEnum::One;
$case(); // 1

When calling an inaccessible method of a case, by default the value of the meta matching the missing method is returned:

php
PureEnum::One->color(); // 'red'

BackedEnum::One->shape(); // 'triangle'

[!TIP] Our IDE can autocomplete meta methods thanks to the annotate command.

Depending on our needs, we can customize the default behavior of all enums in our application when invoking a case or calling inaccessible methods:

…

Self-awareness

Some internal methods are also available and can be useful for inspecting enums or auto-generating code:

…

Console commands

This package provides a handy binary, built to automate different tasks. To learn how to use it, we can simply run it:

bash
./vendor/bin/enum

For the console commands to work properly, the application base path is automatically guessed. However, in case of issues, we can manually set it by creating an enums.php file in the root of our app:

php
<?php

use Cerbero\Enum\Enums;

Enums::setBasePath(__DIR__);

Some commands support the option --all to reference all the enums of our application. We can set the paths where enums live in our app in the enums.php configuration file as well:

php
Enums::setPaths('app/Enums', 'domain/*/Enums');

In the above example, enums are discovered in the app/Enums directory and in all Enums sub-folders belonging to domain, e.g. domain/Posts/Enums, domain/Users/Enums, etc.

️ annotate

The annotate command automatically adds method annotations to enums, making IDEs autocompletion possible:

bash
./vendor/bin/enum annotate App/Enums/Enum

./vendor/bin/enum annotate "App\Enums\Enum"

We can provide more than one enum to annotate, if needed:

bash
./vendor/bin/enum annotate App/Enums/Enum1 App/Enums/Enum2

./vendor/bin/enum annotate "App\Enums\Enum1" "App\Enums\Enum2"

Otherwise we can annotate all our enums at once by enabling the option --all:

bash
./vendor/bin/enum annotate --all

./vendor/bin/enum annotate -a

If we want to overwrite method annotations already annotated on enums, we can add the option --force:

bash
./vendor/bin/enum annotate App/Enums/Enum --force

./vendor/bin/enum annotate App/Enums/Enum -f

️ make

The make command creates a new - automatically annotated - enum with the cases that we provide:

bash
./vendor/bin/enum make App/Enums/Enum CaseOne CaseTwo

./vendor/bin/enum make "App\Enums\Enum" CaseOne CaseTwo

If we need to create backed enums, we can specify a custom value for each case:

bash
./vendor/bin/enum make App/Enums/Enum CaseOne=value1 CaseTwo=value2

Otherwise we can automatically assign values to cases by setting the --backed option:

bash
./vendor/bin/enum make App/Enums/Enum CaseOne CaseTwo --backed=int0

The option --backed supports the following values:

  • int0: assign an incremental integer starting from 0 (0, 1, 2...)
  • int1: assign an incremental integer starting from 1 (1, 2, 3...)
  • bitwise: assign an incremental bitwise value (1, 2, 4...)
  • snake: assign the case name in snake case (case_one, case_two...)
  • kebab: assign the case name in kebab case (case-one, case-two...)
  • camel: assign the case name in camel case (caseOne, caseTwo...)
  • lower: assign the case name in lower case (caseone, casetwo...)
  • upper: assign the case name in upper case (CASEONE, CASETWO...)

If we want to overwrite an existing enum, we can add the option --force:

bash
php artisan enum:make App/Enums/Enum CaseOne CaseTwo --force

php artisan enum:make App/Enums/Enum CaseOne CaseTwo -f

We can generate the TypeScript counterpart of the newly created enum by adding the --typescript option:

bash
php artisan enum:make App/Enums/Enum CaseOne CaseTwo --typescript

php artisan enum:make App/Enums/Enum CaseOne CaseTwo -t

ts

The ts command turns enums into their TypeScript counterpart, synchronizing backend with frontend:

bash
./vendor/bin/enum ts App/Enums/Enum

./vendor/bin/enum ts "App\Enums\Enum"

We can provide more than one enum to synchronize in TypeScript, if needed:

bash
./vendor/bin/enum ts App/Enums/Enum1 App/Enums/Enum2

./vendor/bin/enum ts "App\Enums\Enum1" "App\Enums\Enum2"

Otherwise we can synchronize all our enums at once by enabling the option --all:

bash
./vendor/bin/enum ts --all

./vendor/bin/enum ts -a

By default enums are synchronized in resources/js/enums/index.ts, however we can customize it in our enums.php configuration file:

php
<?php

use Cerbero\Enum\Enums;

// custom static path
Enums::setTypeScript('frontend/enums/index.ts');

// custom dynamic path
Enums::setTypeScript(function (string $enum) {
    $domain = explode('\\', $enum)[1];

    return "resources/js/modules/{$domain}/enums.ts";
});

As seen above, we can either set a static path for our TypeScript enums or dynamically set the TypeScript path of an enum depending on its namespace.

If we want to update previously synchronized enums, we can add the option --force:

bash
./vendor/bin/enum ts App/Enums/Enum --force

./vendor/bin/enum ts App/Enums/Enum -f

Change log

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

Testing

bash
composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

  • [Andrea Marco Sartori][link-author]
  • [All Contributors][link-contributors]

⚖️ License

The MIT License

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPenumenumerationphp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言