This package supercharges your PHP8 backed enums with superpowers like localization support and fluent comparison methods.
composer require webfox/laravel-backed-enums
The enum you create must implement the BackedEnum
interface and also use the IsBackedEnum
trait.
The interface is required for Laravel to cast your enum correctly and the trait is what gives your enum its superpowers.
use Webfox\LaravelBackedEnums\BackedEnum;
use Webfox\LaravelBackedEnums\IsBackedEnum;
enum VolumeUnitEnum: string implements BackedEnum
{
use IsBackedEnum;
case MILLIGRAMS = "milligrams";
case GRAMS = "grams";
case KILOGRAMS = "kilograms";
case TONNE = "tonne";
}
Create enums.php lang file and create labels for your enum values.
// resources/lang/en/enums.php
return [
VolumeUnitEnum::class => [
VolumeUnitEnum::MILLIGRAMS->value => "mg",
VolumeUnitEnum::GRAMS->value => "g",
VolumeUnitEnum::KILOGRAMS->value => "kg",
VolumeUnitEnum::TONNE->value => "t"
]
];
You may then access these localized values using the ->label()
or ::labelFor()
methods.
Additionally rendering the enum in a blade template will render the label.
VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"
// in blade
{{ VolumeUnitEnum::KILOGRAMS }} // "kg"
If you do not specify a label in the lang file these methods will return the value assigned to the enum inside the enum file. e.g MILLIGRAMS label will be MILLIGRAMS
Adding metadata allows you to return additional values alongside the label and values.
Create a withMeta method on your enum to add metadata.
public function withMeta(): array
{
return match ($this) {
self::MILLIGRAMS => [
'background_color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
self::GRAMS => [
'background_color' => 'bg-red-100',
'text_color' => 'text-red-800',
],
self::KILOGRAMS, self::TONNE => [
'background_color' => 'bg-gray-100',
'text_color' => 'text-gray-800',
],
default => [
'background_color' => 'bg-blue-100',
'text_color' => 'text-blue-800',
],
};
}
If you do not specify a withMeta
method, meta will be an empty array.
Returns an array of all enum values with their labels and metadata.
VolumeUnitEnum::options();
returns
[
[
'name' => 'MILLIGRAMS'
'value' => 'milligrams',
'label' => 'mg',
'meta' => [
'background_color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
],
[
'name' => 'GRAMS',
'value' => 'grams',
'label' => 'g',
'meta' => [
'background_color' => 'bg-red-100',
'text_color' => 'text-red-800',
],
...
]
]
Returns an array of all enum values.
VolumeUnitEnum::names();
returns
[
'MILLIGRAMS',
'GRAMS',
'KILOGRAMS',
'TONNE',
]
Returns an array of all enum values.
VolumeUnitEnum::values();
returns
[
'milligrams',
'grams',
'killograms',
'tonne',
]
Returns an array of all enum labels.
VolumeUnitEnum::labels();
returns
[
'mg',
'g',
'kg',
't',
]
Returns an array of all enum values mapping to their label.
VolumeUnitEnum::map();
returns
[
'MILLIGRAMS' => 'mg',
'GRAMS' => 'g',
'KILOGRAMS' => 'kg',
'TONNE' => 't',
]
Returns an array of a single enum value with its label and metadata.
VolumeUnitEnum::MILLIGRAMS->toArray();
returns
[
'name' => 'MILLIGRAMS'
'value' => 'milligrams',
'label' => 'mg',
'meta' => [
'color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
]
An alias of ::label(). Used to satisfy Laravel's Htmlable interface.
VolumeUnitEnum::MILLIGRAMS->toHtml();
returns
mg
Returns a json string represention of the toArray return value.
Allows you to check if an enum is a given value. Returns a boolean.
Note
isA
,isAn
are just aliases foris
.
VolumeUnitEnum::MILLIGRAMS->is(VolumeUnitEnum::MILLIGRAMS); //true
VolumeUnitEnum::MILLIGRAMS->is('MILLIGRAMS'); //true
VolumeUnitEnum::MILLIGRAMS->is('invalid'); //exception
Allows you to check if an enum is not a given value. Returns a boolean.
Note
isNotA
andisNotAn
are just aliases forisNot
.
VolumeUnitEnum::MILLIGRAMS->isNot(VolumeUnitEnum::GRAMS); //true
VolumeUnitEnum::MILLIGRAMS->isNot('GRAMS'); //true
VolumeUnitEnum::MILLIGRAMS->isNot('invalid'); //exception
Allows you to check if an enum is contained in an array. Returns a boolean.
VolumeUnitEnum::MILLIGRAMS->isAny(['GRAMS', VolumeUnitEnum::TONNE]); // false
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // true
Allows you to check if an enum is not contained in an array. Returns a boolean.
VolumeUnitEnum::MILLIGRAMS->isNotAny(['GRAMS', VolumeUnitEnum::TONNE]); // true
VolumeUnitEnum::MILLIGRAMS->isNotAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
The backed enums may be validated using Laravel's standard Enum validation rule - new Illuminate\Validation\Rules\Enum(VolumeUnitEnum::class)
.
This method a shortcut for the validation rule.
public function rules(): array
{
return [
'volume_unit' => [VolumeUnitEnum::rule()],
];
}
This cast is similar to the Laravel built in AsEnumCollection
cast but unlike the built-in will maintain the full toArray
structure
when converting to json.
E.g. the Laravel built in AsEnumCollection
cast will return the following json:
["MILLIGRAMS", "GRAMS"]
This cast will return
[
{
"name": "MILLIGRAMS",
"value": "MILLIGRAMS",
"label": "mg",
"meta": {
"background_color": "bg-green-100",
"text_color": "text-green-800"
}
},
{
"name": "GRAMS",
"value": "GRAMS",
"label": "g",
"meta": {
"background_color": "bg-red-100",
"text_color": "text-red-800"
}
}
]
Please see CHANGELOG for more information on what has changed recently.
We welcome all contributors to the project.
The MIT License (MIT). Please see License File for more information.