PHPackages                             frameck/awesome-enums - 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. frameck/awesome-enums

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

frameck/awesome-enums
=====================

This package provides a set of traits that extend the functionality of enums

v1.2.7(1y ago)243[3 PRs](https://github.com/Frameck/awesome-enums/pulls)MITPHPPHP ^8.1CI passing

Since Apr 23Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/Frameck/awesome-enums)[ Packagist](https://packagist.org/packages/frameck/awesome-enums)[ Docs](https://github.com/frameck/awesome-enums)[ RSS](/packages/frameck-awesome-enums/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (21)Used By (0)

Awesome Enums
=============

[](#awesome-enums)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8b7f7603dc9a47a8b9c3869421eb1b056bde655230d27c366ff92b21cfe34c24/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6672616d65636b2f617765736f6d652d656e756d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/frameck/awesome-enums)[![GitHub Tests Action Status](https://camo.githubusercontent.com/6a97bcb408dc7b936df127832662f03d178ee7a03e3286ea79f4c3c53623333e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6672616d65636b2f617765736f6d652d656e756d732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/frameck/awesome-enums/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/a539c482cdaa0459ce4248c898415679422fbda9ef158d6f07acea3c29365e84/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6672616d65636b2f617765736f6d652d656e756d732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/frameck/awesome-enums/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/7c5c352d1dc07fe53223f7451b0662124f75f04d2aaf440db0b066b5cbb9ceea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6672616d65636b2f617765736f6d652d656e756d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/frameck/awesome-enums)

This package provides a `make:enum` command and a set of traits that extend the functionality of enums

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

[](#installation)

You can install the package via composer:

```
composer require frameck/awesome-enums
```

Usage
-----

[](#usage)

```
php artisan make:enum DeclineCode
```

Possible options:

- `--type`: creates a backed enum, possible values are: `int` or `string`
- `--force`: overwrites if the enum already exists

If no `--type` option is passed the command will prompt you to make a choice

This command generates the following code:

```
namespace App\Enums;

use Frameck\AwesomeEnums\Traits\Comparable;
use Frameck\AwesomeEnums\Traits\HasDetails;
use Frameck\AwesomeEnums\Traits\HasHelpers;

enum DeclineCode: string
{
    use Comparable;
    use HasDetails;
    use HasHelpers;
}
```

Then we add the cases

```
enum DeclineCode: string
{
    use Comparable;
    use HasDetails;
    use HasHelpers;

    case DEFAULT = 'default';
    case CARD_NOT_SUPPORTED = 'card_not_supported';
    case DO_NOT_HONOR = 'do_not_honor';
    case EXPIRED_CARD = 'expired_card';
    case GENERIC_DECLINE = 'generic_decline';
}
```

This is already enough to use this package:

The `all()` method provides a collection of all cases

```
DeclineCode::all();

// result
Illuminate\Support\Collection {
    all: [
        App\Enums\DeclineCode {
            +name: "DEFAULT",
            +value: "default",
        },
        App\Enums\DeclineCode {
            +name: "CARD_NOT_SUPPORTED",
            +value: "card_not_supported",
        },
        App\Enums\DeclineCode {
            +name: "DO_NOT_HONOR",
            +value: "do_not_honor",
        },
        App\Enums\DeclineCode {
            +name: "EXPIRED_CARD",
            +value: "expired_card",
        },
        App\Enums\DeclineCode {
            +name: "GENERIC_DECLINE",
            +value: "generic_decline",
        },
    ],
}
```

The `details()` method provides a collection of all cases with their details

```
DeclineCode::details();

// result
Illuminate\Support\Collection {
    all: [
        [
            "name" => "Default",
            "value" => "default",
        ],
        [
            "name" => "Card Not Supported",
            "value" => "card_not_supported",
        ],
        [
            "name" => "Do Not Honor",
            "value" => "do_not_honor",
        ],
        [
            "name" => "Expired Card",
            "value" => "expired_card",
        ],
        [
            "name" => "Generic Decline",
            "value" => "generic_decline",
        ],
    ],
}
```

The `toArray()` method provides the array representation of the `details()` method

```
DeclineCode::toArray();

// result
[
    [
        "name" => "Default",
        "value" => "default",
    ],
    [
        "name" => "Card Not Supported",
        "value" => "card_not_supported",
    ],
    [
        "name" => "Do Not Honor",
        "value" => "do_not_honor",
    ],
    [
        "name" => "Expired Card",
        "value" => "expired_card",
    ],
    [
        "name" => "Generic Decline",
        "value" => "generic_decline",
    ],
]
```

The `fromName()` method matches the case name and returns you the enum instance

```
DeclineCode::fromName('expired card')

// result
App\Enums\DeclineCode {
    name: "EXPIRED_CARD",
    value: "expired_card",
}
```

You can call the enum case as a static function

```
DeclineCode::EXPIRED_CARD() // equivalent to DeclineCode::EXPIRED_CARD->value
DeclineCode::EXPIRED_CARD('name') // equivalent to DeclineCode::EXPIRED_CARD->name
DeclineCode::EXPIRED_CARD('value') // equivalent to DeclineCode::EXPIRED_CARD->value
```

or from an instance

```
$declineCode = DeclineCode::EXPIRED_CARD;

$declineCode() // equivalent to $declineCode->value
$declineCode('name') // equivalent to $declineCode->name
$declineCode('value') // equivalent to $declineCode->value
```

The `details()` method gives you the the array of details for that specific case

```
DeclineCode::EXPIRED_CARD->getDetails();

// result
[
    "name" => "Expired Card",
    "value" => "expired_card",
]
```

Additionally you can create a function with the camel cased version of the case name (+ Details) that returns an array of details that will be used instead of the default one:

```
private function defaultDetails(): array
{
    return [
        'name' => 'Call Issuer',
        'select' => 'Call issuer',
        'description' => 'The card was declined for an unknown reason.',
        'next_steps' => 'The customer needs to contact their card issuer for more information.',
    ];
}

private function cardNotSupportedDetails(): array
{
    return [
        'name' => 'Card Not Supported',
        'description' => 'The card was declined for an unknown reason.',
        'next_steps' => 'The customer needs to contact their card issuer for more information.',
    ];
}

private function doNotHonorDetails(): array
{
    return [
        'name' => 'Do Not Honor',
        'description' => 'The card was declined for an unknown reason.',
        'next_steps' => 'The customer needs to contact their card issuer for more information.',
    ];
}

private function expiredCardDetails(): array
{
    return [
        'name' => 'Expired Card',
        'description' => 'The card was declined for an unknown reason.',
        'next_steps' => 'The customer needs to contact their card issuer for more information.',
    ];
}

private function genericDeclineDetails(): array
{
    return [
        'name' => 'Generic Decline',
        'description' => 'The card was declined for an unknown reason.',
        'next_steps' => 'The customer needs to contact their card issuer for more information.',
    ];
}
```

You can also pass an optional key to retrieve only that value

```
DeclineCode::EXPIRED_CARD->getDetails('description');

// result
// The card was declined for an unknown reason.
```

The `toSelect()` method provides an array of key value pair useful in html selects

```
DeclineCode::toSelect();

[
    "default" => "Call issuer",
    "card_not_supported" => "Card Not Supported",
    "do_not_honor" => "Do Not Honor",
    "expired_card" => "Expired Card",
    "generic_decline" => "Generic Decline",
]

// the toSelect() method is based on the details() array so you can specify a custom label for the select
// in order the package searches for a 'select' key then 'label' and 'name'
// so you can have a different value for 'name' and 'select'
public static function toSelect(): array
{
    return collect(self::cases())
        ->mapWithKeys(function (self $case) {
            $caseDetails = $case->getDetails();
            $selectLabel = $caseDetails['select']
                ?? $caseDetails['label']
                ?? $caseDetails['name'];

            return [
                $case->value => $selectLabel,
            ];
        })
        ->toArray();
}
```

The `toJson()` method provides the json representation of the `toSelect()` method useful when you have to share data with a frontend in vue, react ecc... or an api:

```
DeclineCode::toJson();

// "{"default":"Call issuer","card_not_supported":"Card Not Supported","do_not_honor":"Do Not Honor","expired_card":"Expired Card","generic_decline":"Generic Decline"}"
```

The `is()` and `isNot()` methods provide a fluent way to check if an enum instance is equal to another:

```
DeclineCode::CARD_NOT_SUPPORTED->is(DeclineCode::EXPIRED_CARD); // false
DeclineCode::CARD_NOT_SUPPORTED->isNot(DeclineCode::EXPIRED_CARD); // true
```

The `in()` and `notIn()` methods provide a fluent way to check if an enum instance is present in an array of enums:

```
DeclineCode::CARD_NOT_SUPPORTED->in([DeclineCode::EXPIRED_CARD, DeclineCode::GENERIC_DECLINE]); // false
DeclineCode::CARD_NOT_SUPPORTED->notIn([DeclineCode::EXPIRED_CARD, DeclineCode::GENERIC_DECLINE]); // true
```

To better integrate the enum in a laravel ecosystem you can add it inside the `$casts` property of the model

```
class Payment extends Model
{
    protected $casts = [
        'decline_code' => DeclineCode::class
    ];
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

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

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Frameck](https://github.com/Frameck)
- [All Contributors](../../contributors)
- [Steve Barbera post on medium](https://stevebarbera.medium.com/extending-php8-1-enums-10ea22aa15c4)

License
-------

[](#license)

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

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance66

Regular maintenance activity

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 62% 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 ~27 days

Recently: every ~83 days

Total

17

Last Release

676d ago

Major Versions

v0.0.3 → v1.0.02023-04-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/64c526acdab45bab0bd3c7d1c29ea0d585d398e5c7855763619aca2c9f91b5e6?d=identicon)[Frameck](/maintainers/Frameck)

---

Top Contributors

[![Frameck](https://avatars.githubusercontent.com/u/77396783?v=4)](https://github.com/Frameck "Frameck (44 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (15 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (12 commits)")

---

Tags

enumslaravelphplaravelFrameckawesome-enums

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/frameck-awesome-enums/health.svg)

```
[![Health](https://phpackages.com/badges/frameck-awesome-enums/health.svg)](https://phpackages.com/packages/frameck-awesome-enums)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

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