Download the PHP package iteks/laravel-enum without Composer
On this page you can find all versions of the php package iteks/laravel-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iteks/laravel-enum
More information about iteks/laravel-enum
Files in iteks/laravel-enum
Package laravel-enum
Short Description A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.
License MIT
Informations about the package laravel-enum
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.
Get Started
Requires PHP 8.1+
Install Laravel Enum via the Composer package manager:
Usage
- ExampleBackedEnum class
- Attributes
- Enum Helpers (BackedEnum)
- Enum::asSelectArray()
- Enum::toLabel()
- Enum::toLabels()
- Enum Helpers (HasAttributes)
- Enum::attributes()
- Enum::description()
- Enum::descriptions()
- Enum::id()
- Enum::ids()
- Enum::label()
- Enum::labels()
- Enum::metadata()
- Enum::metadatum()
- Enum Traits (BackedEnum)
- asSelectArray()
- fromName()
- name()
- names()
- toLabel()
- toLabels()
- tryFromName()
- value()
- values()
- Enum Traits (HasAttributes)
- attributes()
- description()
- descriptions()
- id()
- ids()
- label()
- labels()
- metadata()
- metadatum()
- String Helper Macros
- Str::splitConstantCase()
- Str::splitEnumCase()
ExampleBackedEnum class
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.
top
Attributes
The package provides four attributes to enhance your enum classes and cases:
Description Attribute
Provides descriptive text for enum classes and cases.
Id Attribute
Provides unique identifiers for enum cases.
Label Attribute
Provides human-readable labels for enum cases.
Metadata Attribute
Provides additional metadata for enum classes and cases. Supports multiple metadata attributes and can be used on both the enum class and its cases.
Attribute Capabilities Summary
- Description: Class + Cases
- Id: Cases Only
- Label: Cases Only
- Metadata: Class + Cases
top
Enum Helpers (BackedEnum)
First, import the helper class:
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:
Enum::asSelectArray()
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::toLabel()
Create a label from the case name.
Enum::toLabels()
Create and compile an array of labels from the case names.
top
Enum Helpers (HasAttributes)
First, ensure that the target enum class has the HasAttributes
trait applied, as shown in the ExampleBackedEnum class above.
Then, import the helper class:
You may then use the following methods:
Enum::attributes()
Accessing attributes.
Retrieve all attributes for a given case.
Retrieve a subset of the attributes for a given case.
Retrieve all attributes for all cases.
Retrieve a subset of the attributes for all cases.
Enum::description()
Retrieve the description attribute.
Enum::descriptions()
Retrieve the description attribute for all cases.
Enum::id()
Retrieve the id attribute.
Enum::ids()
Retrieve the id attribute for all cases.
Enum::label()
Retrieve the label attribute.
Enum::labels()
Retrieve the label attribute for all cases.
Enum::metadata()
Retrieve the metadata attribute.
Enum::metadatum()
Retrieve the metadata attribute for all cases.
top
Enum Traits (BackedEnum)
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:
asSelectArray()
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.
fromName()
Maps a scalar to an enum instance.
name()
Retrieve the case name for the given simpler value.
names()
Retrieve an array containing all of the case names.
toLabel()
Create a label from the case name.
toLabels()
Create and compile an array of labels from the case names.
tryFromName()
Maps a scalar to an enum instance or null.
value()
Retrieve the simpler value for the given case name.
values()
Retrieve an array containing all of the simpler values.
top
Enum Traits (HasAttributes)
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:
attributes()
Retrieve the attributes for a given case.
Retrieve a subset of the attributes for a given case.
Retrieve the attributes for all cases.
Retrieve a subset of the attributes for all cases/
description()
Retrieve the description attribute.
descriptions()
Retrieve the description attribute for all cases.
id()
Retrieve the id attribute.
ids()
Retrieve the id attribute for all cases.
label()
Retrieve the label attribute.
labels()
Retrieve the label attribute for all cases.
metadata()
Retrieve the metadata attribute.
metadatum()
Retrieve the metadata attribute for all cases.
top
String Helper Macros
These helperas are booted when installing the package and are immediately available for use.
Str::splitConstantCase()
Splits a "CONSTANT_CASE" string into words separated by whitespace.
Str::splitEnumCase()
Splits a "EnumCase" string into words separated by whitespace.
top