outerweb / enum-helpers
Traits to make working with Enums a breeze in Laravel.
Fund package maintenance!
Outerweb
Installs: 112
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 1
pkg:composer/outerweb/enum-helpers
Requires
- php: ^8.4
- illuminate/contracts: ^11.0||^12.0
- illuminate/support: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
Enum helpers
This package provides a set of traits you can use to extend the functionality of your enums in Laravel.
Table of Contents
Installation
You can install the package via composer:
composer require outerweb/enum-helpers
Usage
Collection support
Add the following trait to your backed enum to get collection support:
use Outerweb\EnumHelpers\HasCollectionSupport; enum MyEnum: string { use HasCollectionSupport; case Foo = 'foo'; case Bar = 'bar'; public function getLabel(): string { return match ($this) { self::Foo => 'Foo label', self::Bar => 'Bar label', }; } }
This allows you to collect enum cases easily into a Laravel collection:
$collection = MyEnum::collect(); // Collection{'foo' => MyEnum::Foo, 'bar' => MyEnum::Bar}
Getting a collection of values
You can get a collection of enum values using the collect('value')
method:
$values = MyEnum::collect('value'); // Collection{'foo' => 'foo', 'bar' => 'bar'}
Getting a collection of return values of a function
You can get a collection of return values of a function by passing the function name to the collect()
method:
$mapped = MyEnum::collect('getLabel'); // Collection{'foo' => 'Foo label', 'bar' => 'Bar label'}
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.