The Laravel Enum package enriches Laravel's enum support, integrating advanced features like attribute handling, select array transformation, and facade access for streamlined enum operations. Designed for Laravel applications, it offers a suite of utilities for both backed enums and attribute-enhanced enums, including descriptive annotations, ID management, label generation, and metadata association. This package streamlines working with enums in Laravel by providing intuitive, fluent interfaces for common tasks, enhancing enum usability in forms, API responses, and more. Whether you're defining select options, querying enum attributes, or integrating enums tightly with Laravel features, Laravel Enum simplifies these processes, making enum management in Laravel applications both powerful and efficient. Offered by iteks, Developed by jeramyhing.
Requires PHP 8.1+
Install Laravel Enum via the Composer package manager:
composer require iteks/laravel-enum
- EnumExample class
- Enum Helpers (BackedEnum)
- Enum Helpers (HasAttributes)
- Enum Traits (BackedEnum)
- Enum Traits (HasAttributes)
- String Helper Macros
The Laravel Enum methods are designed for PHP 8 Backed Enumeration classes.
Laravel Enum helper and trait methods extend an existing backed enum class for more versatile enum handling. Additionally, Laravel Enum offers a fluent way to add and manage PHP 8 Attributes on backed enum cases. This package comes with four available attributes to readily assign to your enum cases: Description, Id, Label, and Metadata. The ExampleEnum class below demonstrates how you can apply these attributes to you enums. You may pick and choose which attributes you wish to take advantage of.
use Iteks\Attributes\Description;
use Iteks\Attributes\Id;
use Iteks\Attributes\Label;
use Iteks\Attributes\Metadata;
use Iteks\Traits\BackedEnum;
use Iteks\Traits\HasAttributes;
enum ExampleEnum: int
{
use BackedEnum;
use HasAttributes;
#[Description('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')]
#[Id(0)]
#[Label('First-Example')]
#[Metadata(['key' => 'value'])]
case FirstExample = 1;
#[Description('Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.')]
#[Id(1)]
#[Label('SecondExample (Example)')]
#[Metadata('{"key": "value"}')]
case SecondExample = 2;
#[Description('Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.')]
#[Id(2)]
#[Label('3rd Eg.')]
#[Metadata([])]
case ThirdExample = 3;
}
First, import the helper class:
use Iteks\Support\Facades\Enum;
Note: This group of helpers does NOT require any trait to be applied to the target enum class. You may immediately use the the following methods:
Get a backed enum class as an array to populate a select element. The array will consist of a text
key column containing values of the case name in display format, and a value
keys column containing values using the original simpler values.
Note: This method will first check for Label and Id attributes applied to the target enum class. If they are present, the method will prioritize those values. If not present, the method will return a mutated Headline value from the case name.
Enum::asSelectArray(ExampleEnum::class);
array:3 [▼
0 => array:2 [▼
"text" => "First-Example"
"value" => 1
]
1 => array:2 [▼
"text" => "SecondExample (Example)"
"value" => 2
]
2 => array:2 [▼
"text" => "3rd Eg."
"value" => 3
]
]
Create a label from the case name.
Enum::toLabel(ExampleEnum::FirstExample);
"First Example"
Create and compile an array of labels from the case names.
Enum::toLabels(ExampleEnum::class);
array:3 [▼
0 => "First Example"
1 => "Second Example"
2 => "Third Example"
]
First, ensure that the target enum class has the HasAttributes
trait applied, as shown in the ExampleEnum class above.
Then, import the helper class:
use Iteks\Support\Facades\Enum;
You may then use the following methods:
Retrieve all of the attributes for all cases.
Enum::attributes(ExampleEnum::FirstExample);
array:5 [▼
"simpleValue" => 1
"description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"id" => 0
"label" => "First-Example"
"metadata" => array:1 [▼
"key" => "value"
]
]
Enum::attributes(ExampleEnum::FirstExample, ['id', 'label']);
array:2 [▼
"id" => 0
"label" => "First-Example"
]
Enum::attributes(ExampleEnum::class);
array:3 [▼
"FirstExample" => array:5 [▼
"simpleValue" => 1
"description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"id" => 0
"label" => "First-Example"
"metadata" => array:1 [▼
"key" => "value"
]
]
"SecondExample" => array:5 [▼
"simpleValue" => 2
"description" => "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
"id" => 1
"label" => "SecondExample (Example)"
"metadata" => "{"key": "value"}"
]
"ThirdExample" => array:5 [▼
"simpleValue" => 3
"description" => "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"id" => 2
"label" => "3rd Eg."
"metadata" => []
]
]
Enum::attributes(ExampleEnum::class, ['description', 'metadata']);
array:3 [▼
"FirstExample" => array:2 [▼
"description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"metadata" => array:1 [▼
"key" => "value"
]
]
"SecondExample" => array:2 [▼
"description" => "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
"metadata" => "{"key": "value"}"
]
"ThirdExample" => array:2 [▼
"description" => "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"metadata" => []
]
]
Retrieve the description attribute.
Enum::description(ExampleEnum::FirstExample);
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
Retrieve the description attribute for all cases.
Enum::descriptions(ExampleEnum::class);
array:3 [▼
0 => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
1 => "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2 => "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
]
Retrieve the id attribute.
Enum::id(ExampleEnum::FirstExample);
0
Retrieve the id attribute for all cases.
Enum::ids(ExampleEnum::class);
array:3 [▼
0 => 0
1 => 1
2 => 2
]
Retrieve the label attribute.
Enum::label(ExampleEnum::FirstExample);
"First-Example"
Retrieve the label attribute for all cases.
Enum::labels(ExampleEnum::class);
array:3 [▼
0 => "First-Example"
1 => "SecondExample (Example)"
2 => "3rd Eg."
]
Retrieve the metadata attribute.
Enum::metadata(ExampleEnum::FirstExample);
array:1 [▼
"key" => "value"
]
Retrieve the metadata attribute for all cases.
Enum::metadatum(ExampleEnum::class);
array:3 [▼
0 => array:1 [▼
"key" => "value"
]
1 => "{"key": "value"}"
2 => []
]
First, ensure that the target enum class has the BackedEnum
trait applied, as shown in the ExampleEnum class above.
Then, you may then use the following methods:
Get a backed enum class as an array to populate a select element. The array will consist of a text
key column containing values of the case name in display format, and a value
keys column containing values using the original simpler values.
Note: This method will first check for Label and Id attributes applied to the target enum class. If they are present, the method will prioritize those values. If not present, the method will return a mutated Headline value from the case name.
ExampleEnum::asSelectArray();
array:3 [▼
0 => array:2 [▼
"text" => "First-Example"
"value" => 0
]
1 => array:2 [▼
"text" => "SecondExample (Example)"
"value" => 1
]
2 => array:2 [▼
"text" => "3rd Eg."
"value" => 2
]
]
Maps a scalar to an enum instance.
ExampleEnum::fromName('FirstExample');
App\Enums\ExampleEnum {#297 ▼
+name: "FirstExample"
+value: 1
}
Retrieve the case name for the given simpler value.
ExampleEnum::name(1);
"FirstExample"
Retrieve an array containing all of the case names.
ExampleEnum::names();
array:3 [▼
0 => "FirstExample"
1 => "SecondExample"
2 => "ThirdExample"
]
Create a label from the case name.
ExampleEnum::toLabel(1);
"First Example"
Create and compile an array of labels from the case names.
ExampleEnum::toLabels();
array:3 [▼
0 => "First Example"
1 => "Second Example"
2 => "Third Example"
]
Maps a scalar to an enum instance or null.
ExampleEnum::tryFromName('FirstExample');
App\Enums\ExampleEnum {#297 ▼
+name: "FirstExample"
+value: 1
}
Retrieve the simpler value for the given case name.
ExampleEnum::value('FirstExample');
1
Retrieve an array containing all of the simpler values.
ExampleEnum::values();
array:3 [▼
0 => 1
1 => 2
2 => 3
]
First, ensure that the target enum class has the HasAttributes
trait applied, as shown in the ExampleEnum class above.
Then, you may then use the following methods:
Retrieve all of the attributes for all cases.
ExampleEnum::attributes('FirstExample');
array:5 [▼
"simpleValue" => 1
"description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"id" => 0
"label" => "First-Example"
"metadata" => array:1 [▼
"key" => "value"
]
]
ExampleEnum::attributes('FirstExample', ['id', 'label']);
array:2 [▼
"id" => 0
"label" => "First-Example"
]
ExampleEnum::attributes();
array:3 [▼
"FirstExample" => array:5 [▼
"simpleValue" => 1
"description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"id" => 0
"label" => "First-Example"
"metadata" => array:1 [▼
"key" => "value"
]
]
"SecondExample" => array:5 [▼
"simpleValue" => 2
"description" => "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
"id" => 1
"label" => "SecondExample (Example)"
"metadata" => "{"key": "value"}"
]
"ThirdExample" => array:5 [▼
"simpleValue" => 3
"description" => "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"id" => 2
"label" => "3rd Eg."
"metadata" => []
]
]
ExampleEnum::attributes(null, ['description', 'metadata']);
array:3 [▼
"FirstExample" => array:2 [▼
"description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"metadata" => array:1 [▼
"key" => "value"
]
]
"SecondExample" => array:2 [▼
"description" => "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
"metadata" => "{"key": "value"}"
]
"ThirdExample" => array:2 [▼
"description" => "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"metadata" => []
]
]
Retrieve the description attribute.
ExampleEnum::description('FirstExample');
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
Retrieve the description attribute for all cases.
ExampleEnum::descriptions();
array:3 [▼
0 => "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
1 => "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2 => "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
]
Retrieve the id attribute.
ExampleEnum::id('FirstExample');
0
Retrieve the id attribute for all cases.
ExampleEnum::ids();
array:3 [▼
0 => 0
1 => 1
2 => 2
]
Retrieve the label attribute.
ExampleEnum::label('FirstExample');
"First-Example"
Retrieve the label attribute for all cases.
ExampleEnum::labels();
array:3 [▼
0 => "First-Example"
1 => "SecondExample (Example)"
2 => "3rd Eg."
]
Retrieve the metadata attribute.
ExampleEnum::metadata('FirstExample');
array:1 [▼
"key" => "value"
]
Retrieve the metadata attribute for all cases.
ExampleEnum::metadatum();
array:3 [▼
0 => array:1 [▼
"key" => "value"
]
1 => "{"key": "value"}"
2 => []
]
These helperas are booted when installing the package and are immediately available for use.
Splits a "CONSTANT_CASE" string into words separated by whitespace.
Str::splitConstantCase('FIRST_EXAMPLE');
"FIRST EXAMPLE"
Splits a "EnumCase" string into words separated by whitespace.
Str::splitEnumCase('FirstExample');
"First Example"