outerweb/enum-helpers

Laravel traits to enhance php enums

v1.0.1 2024-03-12 20:41 UTC

This package is auto-updated.

Last update: 2024-11-04 11:34:27 UTC


README

Enum Helpers banner

Enum Helpers

Latest Version on Packagist Total Downloads

This package provides Laravel specific helpers for working with enums.

  • \Illuminate\Support\Collection support via the HasCollectionSupport trait.

Installation

You can install the package via composer:

composer require outerweb/enum-helpers

Usage

Collection support

use Outerweb\EnumHelpers\HasCollectionSupport;

enum Status: string
{
    use HasCollectionSupport;

    const Open = 'open';
    const Closed = 'closed';

    public function label(): string
    {
        return match ($this->value) {
            self::Open => 'Open',
            self::Closed => 'Closed',
        };
    }
}

You can now get a collection of all th cases by calling:

Status::collect();

You can also get a collection of all the labels by calling:

Status::collect('label');

Suggestions?

If you have any suggestions for extra enum helpers, please create an issue or a pull request.

Laravel support

Changelog

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

Credits

License

MIT License (MIT). Read the License File for more information.