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
- ExampleBackedEnum class
- Attributes
- 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 ExampleBackedEnum 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 ExampleBackedEnum: int
{
use BackedEnum;
use HasAttributes;
#[Description('Active status indicating the resource is currently in use')]
#[Id(101)]
#[Label('Active')]
#[Metadata(['status_type' => 'positive', 'display_color' => 'green'])]
case CurrentlyActive = 1;
#[Description('Pending status indicating the resource is awaiting processing or approval')]
#[Id(102)]
#[Label('Pending')]
#[Metadata('{"status_type": "neutral", "display_color": "yellow"}')]
case PendingReview = 2;
#[Description('Temporarily suspended status indicating the resource is on hold')]
#[Id(103)]
#[Label('Suspended (!)')]
#[Metadata([])]
case TemporarilySuspended = 3;
}
The package provides four attributes to enhance your enum classes and cases:
Provides descriptive text for enum classes and cases.
#[Description('A collection of status codes')] // Class-level description
enum Status: int
{
#[Description('Operation completed successfully')] // Case-level description
case Success = 200;
}
Provides unique identifiers for enum cases.
enum Status: int
{
#[Id(1)]
case Draft = 0;
}
Provides human-readable labels for enum cases.
enum Status: int
{
#[Label('In Progress')]
case Processing = 1;
}
Provides additional metadata for enum classes and cases. Supports multiple metadata attributes and can be used on both the enum class and its cases.
#[Metadata(['version' => '1.0'])] // Class-level metadata
enum Status: int
{
#[Metadata(['severity' => 'error'])] // Case-level metadata
case Error = 500;
}
- Description: Class + Cases
- Id: Cases Only
- Label: Cases Only
- Metadata: Class + Cases
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.
$selectArray = Enum::asSelectArray(ExampleBackedEnum::class);
# Result:
array:3 [▼
0 => array:2 [▼
"text" => "Active"
"value" => 101
]
1 => array:2 [▼
"text" => "Pending"
"value" => 102
]
2 => array:2 [▼
"text" => "Suspended (!)"
"value" => 103
]
]
Create a label from the case name.
$label = Enum::toLabel(ExampleBackedEnum::CurrentlyActive); // 'Currently Active'
Create and compile an array of labels from the case names.
$labels = Enum::toLabels(ExampleBackedEnum::class);
# Result:
array:3 [▼
0 => "Currently Active"
1 => "Pending Review"
2 => "Temporarily Suspended"
]
First, ensure that the target enum class has the HasAttributes
trait applied, as shown in the ExampleBackedEnum class above.
Then, import the helper class:
use Iteks\Support\Facades\Enum;
You may then use the following methods:
Accessing attributes.
Retrieve all attributes for a given case.
$attributes = Enum::attributes(ExampleBackedEnum::CurrentlyActive);
# Result:
array:5 [▼
"simpleValue" => 1
"description" => "Active status indicating the resource is currently in use"
"id" => 101
"label" => "Active"
"metadata" => array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
]
Retrieve a subset of the attributes for a given case.
$attributes = Enum::attributes(ExampleBackedEnum::CurrentlyActive, ['id', 'label']);
# Result:
array:2 [▼
"id" => 101
"label" => "Active"
]
Retrieve all attributes for all cases.
$attributes = Enum::attributes(ExampleBackedEnum::class);
# Result:
array:3 [▼
"CurrentlyActive" => array:5 [▼
"simpleValue" => 1
"description" => "Active status indicating the resource is currently in use"
"id" => 101
"label" => "Active"
"metadata" => array:2 [▶]
]
"PendingReview" => array:5 [▼
"simpleValue" => 2
"description" => "Pending status indicating the resource is awaiting processing or approval"
"id" => 102
"label" => "Pending"
"metadata" => "{"status_type": "neutral", "display_color": "yellow"}"
]
"TemporarilySuspended" => array:5 [▼
"simpleValue" => 3
"description" => "Temporarily suspended status indicating the resource is on hold"
"id" => 103
"label" => "Suspended (!)"
"metadata" => []
]
]
Retrieve a subset of the attributes for all cases.
$attributes = Enum::attributes(ExampleBackedEnum::class, ['description', 'metadata']);
# Result:
array:3 [▼
"CurrentlyActive" => array:2 [▼
"description" => "Active status indicating the resource is currently in use"
"metadata" => array:2 [▶]
]
"PendingReview" => array:2 [▼
"description" => "Pending status indicating the resource is awaiting processing or approval"
"metadata" => "{"status_type": "neutral", "display_color": "yellow"}"
]
"TemporarilySuspended" => array:2 [▼
"description" => "Temporarily suspended status indicating the resource is on hold"
"metadata" => []
]
]
Retrieve the description attribute.
$description = Enum::description(ExampleBackedEnum::CurrentlyActive);
# Result:
"Active status indicating the resource is currently in use"
Retrieve the description attribute for all cases.
$descriptions = Enum::descriptions(ExampleBackedEnum::class);
# Result:
array:3 [▼
0 => "Active status indicating the resource is currently in use"
1 => "Pending status indicating the resource is awaiting processing or approval"
2 => "Temporarily suspended status indicating the resource is on hold"
]
Retrieve the id attribute.
$id = Enum::id(ExampleBackedEnum::CurrentlyActive); //101
Retrieve the id attribute for all cases.
$ids = Enum::ids(ExampleBackedEnum::class);
# Result:
array:3 [▼
0 => 101
1 => 102
2 => 103
]
Retrieve the label attribute.
$label = Enum::label(ExampleBackedEnum::TemporarilySuspended); // 'Suspended (!)'
Retrieve the label attribute for all cases.
$labels = Enum::labels(ExampleBackedEnum::class);
# Result:
array:3 [▼
0 => "Active"
1 => "Pending"
2 => "Suspended (!)"
]
Retrieve the metadata attribute.
$metadata = Enum::metadata(ExampleBackedEnum::CurrentlyActive);
# Result:
array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
Retrieve the metadata attribute for all cases.
$metadatum = Enum::metadatum(ExampleBackedEnum::class);
# Result:
array:3 [▼
0 => array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
1 => "{"status_type": "neutral", "display_color": "yellow"}"
2 => []
]
First, ensure that the target enum class has the BackedEnum
trait applied, as shown in the ExampleBackedEnum 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.
$selectArray = ExampleBackedEnum::asSelectArray();
# Result:
array:3 [▼
0 => array:2 [▼
"text" => "Active"
"value" => 101
]
1 => array:2 [▼
"text" => "Pending"
"value" => 102
]
2 => array:2 [▼
"text" => "Suspended (!)"
"value" => 103
]
]
Maps a scalar to an enum instance.
$enum = ExampleBackedEnum::fromName('CurrentlyActive');
# Result:
App\Enums\ExampleBackedEnum { ▼
+name: "CurrentlyActive"
+value: 1
}
Retrieve the case name for the given simpler value.
$caseName = ExampleBackedEnum::name(1); // 'CurrentlyActive'
Retrieve an array containing all of the case names.
$caseNames = ExampleBackedEnum::names();
# Result:
array:3 [▼
0 => "CurrentlyActive"
1 => "PendingReview"
2 => "TemporarilySuspended"
]
Create a label from the case name.
$label = ExampleBackedEnum::toLabel(1); // 'Currently Active'
Create and compile an array of labels from the case names.
$labels = ExampleBackedEnum::toLabels();
# Result:
array:3 [▼
0 => "Currently Active"
1 => "Pending Review"
2 => "Temporarily Suspended"
]
Maps a scalar to an enum instance or null.
$enum = ExampleBackedEnum::tryFromName('CurrentlyActive');
# Result:
App\Enums\ExampleBackedEnum { ▼
+name: "CurrentlyActive"
+value: 1
}
Retrieve the simpler value for the given case name.
$simplerValue = ExampleBackedEnum::value('CurrentlyActive'); // 1
Retrieve an array containing all of the simpler values.
$simplerValues = ExampleBackedEnum::values();
# Result:
array:3 [▼
0 => 1
1 => 2
2 => 3
]
First, ensure that the target enum class has the HasAttributes
trait applied, as shown in the ExampleBackedEnum class above.
Then, you may then use the following methods:
Retrieve the attributes for a given case.
$attributes = ExampleBackedEnum::attributes('CurrentlyActive');
# Result:
array:5 [▼
"simpleValue" => 1
"description" => "Active status indicating the resource is currently in use"
"id" => 101
"label" => "Active"
"metadata" => array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
]
Retrieve a subset of the attributes for a given case.
$attributes = ExampleBackedEnum::attributes('CurrentlyActive', ['id', 'label']);
# Result:
array:2 [▼
"id" => 101
"label" => "Active"
]
Retrieve the attributes for all cases.
$attributes = ExampleBackedEnum::attributes();
# Result:
array:3 [▼
"CurrentlyActive" => array:5 [▼
"simpleValue" => 1
"description" => "Active status indicating the resource is currently in use"
"id" => 101
"label" => "Active"
"metadata" => array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
]
"PendingReview" => array:5 [▼
"simpleValue" => 2
"description" => "Pending status indicating the resource is awaiting processing or approval"
"id" => 102
"label" => "Pending"
"metadata" => "{"status_type": "neutral", "display_color": "yellow"}"
]
"TemporarilySuspended" => array:5 [▼
"simpleValue" => 3
"description" => "Temporarily suspended status indicating the resource is on hold"
"id" => 103
"label" => "Suspended (!)"
"metadata" => []
]
]
Retrieve a subset of the attributes for all cases/
$attributes = ExampleBackedEnum::attributes(null, ['description', 'metadata']);
# Result:
array:3 [▼
"CurrentlyActive" => array:2 [▼
"description" => "Active status indicating the resource is currently in use"
"metadata" => array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
]
"PendingReview" => array:2 [▼
"description" => "Pending status indicating the resource is awaiting processing or approval"
"metadata" => "{"status_type": "neutral", "display_color": "yellow"}"
]
"TemporarilySuspended" => array:2 [▼
"description" => "Temporarily suspended status indicating the resource is on hold"
"metadata" => []
]
]
Retrieve the description attribute.
$description = ExampleBackedEnum::description('CurrentlyActive');
# Result:
"Active status indicating the resource is currently in use"
Retrieve the description attribute for all cases.
$descriptions = ExampleBackedEnum::descriptions();
# Result:
array:3 [▼
0 => "Active status indicating the resource is currently in use"
1 => "Pending status indicating the resource is awaiting processing or approval"
2 => "Temporarily suspended status indicating the resource is on hold"
]
Retrieve the id attribute.
$id = ExampleBackedEnum::id('CurrentlyActive'); // 101
Retrieve the id attribute for all cases.
$ids = ExampleBackedEnum::ids();
# Result:
array:3 [▼
0 => 101
1 => 102
2 => 103
]
Retrieve the label attribute.
$label = ExampleBackedEnum::label('CurrentlyActive'); // 'Active'
Retrieve the label attribute for all cases.
$labels = ExampleBackedEnum::labels();
# Result:
array:3 [▼
0 => "Active"
1 => "Pending"
2 => "Suspended (!)"
]
Retrieve the metadata attribute.
$metadata = ExampleBackedEnum::metadata('CurrentlyActive');
# Result:
array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
Retrieve the metadata attribute for all cases.
$metadatum = ExampleBackedEnum::metadatum();
# Result:
array:3 [▼
0 => array:2 [▼
"status_type" => "positive"
"display_color" => "green"
]
1 => "{"status_type": "neutral", "display_color": "yellow"}"
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.
$string = Str::splitConstantCase('CONSTANT_CASE'); // 'CONSTANT CASE'
Splits a "EnumCase" string into words separated by whitespace.
$string = Str::splitEnumCase('EnumCase'); // 'Enum Case'