Download the PHP package datomatic/laravel-enum-helper without Composer
On this page you can find all versions of the php package datomatic/laravel-enum-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download datomatic/laravel-enum-helper
More information about datomatic/laravel-enum-helper
Files in datomatic/laravel-enum-helper
Package laravel-enum-helper
Short Description Simple opinionated framework agnostic PHP 8.1 enum helper for Laravel
License MIT
Informations about the package laravel-enum-helper
Laravel Enum Helper
This is an extension of the datomatic/enum-helper package based on Laravel Framework.
The package consists on a LaravelEnumHelper
trait that extends EnumInvokable
, EnumFroms
, EnumNames
,
EnumValues
, EnumEquality
, EnumDescription
and add dynamic methods to return a translation or "property" method and
relative helper methods.
You can think about it as an EnumDescription
trait but completely dynamic.
So you can define a custom method()
and have these functions available: [method]s()
, [method]sByName()
,
[method]sByValue()
. For example with color()
you obtain colors()
, colorsByName()
, colorsByValue()
methods.
The cool thing is you can also avoid writing the method and write only the translations.
For example, you can define the property excerpt
by writing only the translations on enums.php (see below for explanation)
and obtain excerpt()
, excerpts()
, excerptsByName()
, excerptsByValue()
methods.
The package use the Laravel Pluralizer
component to get the singular method to call or to translate.
Related Packages
- datomatic/laravel-enum-collections if you need to save and interact many enum values on one field.
- datomatic/nova-enum-field if you are using Laravel Nova, this package adds the PHP 8.1
enum
,EnumDescription
andLaravelEnumHelper
traits support, with a Nova Select and Nova Boolean filters ready out of the box.
The enum-helper
base package summary
You should see the datomatic/enum-helper
details on his repository, this is a summary:
- Invokable cases: get the value of enum "invoking" it statically
- Construct enum by name or value:
from()
,tryFrom()
,fromName()
,tryFromName()
for all enums - Enums Inspection:
isPure()
,isBacked()
,has()
,hasName()
,hasValue()
methods - Enums Equality:
is()
,isNot()
,in()
,notIn()
methods - Names: methods to have a list of case names (
names()
,namesByValue()
) - Values: methods to have a list of case values (
values()
,valuesByName()
) - Unique ID: get an unique identifier from instance or instance from identifier (
uniqueId()
,fromUniqueId()
) - Descriptions & Translations: add description to enum with optional translation (
description()
,descriptions()
,descriptionsByName()
,descriptionsByValue()
)
Installation
Laravel 8 or above and PHP 8.1+ are required.
Usage
You can use this functionality simply using the LaravelEnumHelper
trait.
After that you can define a custom "property" method like color()
or color(?string $lang = null)
or define the translations instead.
Annotation command
A convenient command is available to generate DocBlock annotations for enum classes.
This command generate the phpDoc annotations for invokable case methods.
Translations
Using this trait there is 2 way to manage translation strings.
Using Short Keys
Language strings may be stored in enums.php
files within the lang
directory. Within this directory, there may be subdirectories for each language supported by the application.
All language files return an array of keyed strings. The array has 3 levels:
- first level key: the complete class name of the enum
- second level key: the name of the property to translate
- third level key: the name/value of the enum case.
The default property is description
so if you use only that property you can use just a 2 level array.
Using Translation Strings As Keys
Language strings are stored as JSON files in the lang directory (e.g. lang/it.json
).
In this example the description
property is translated:
But if you want to use this way, you can simply define the method on Enum class like this description
method.
[property]()
method
This dynamic method try to resolve property()
method on the enum.
If the method not exist try to translate the instance value with the framework translation function
__("enums.Namespace\EnumClass.property.CASE_NAME")
.
Because the default property is description
, if there isn't a translation to that property there is a fallback that takes the case name humanized.
With the other properties, if the translation does not exist, the package throws a TranslationMissing
exception.
static [property]s()
method
This dynamic method gets a list of case property()
returns of the enum.
The name of the method is the plural of the property so if you are using property
it will be properties()
.
static [property]sByName()
method
This dynamic method returns an associative array of [case name => property()
result].
The name of the method is the plural of the property so if you are using property
it will be propertiesByName()
.
static [property]sByValue()
method
This dynamic method returns an associative array of [case value => property()
result] on BackedEnum
, [case name => property()
result] otherwise.
The name of the method is the plural of the property so if you are using property
it will be propertiesByValue()
.
All versions of laravel-enum-helper with dependencies
composer/class-map-generator Version ^1.0
datomatic/enum-helper Version ^2.0
illuminate/translation Version ^8.0|^9.0|^10.0|^11.0
illuminate/support Version ^8.0|^9.0|^10.0|^11.0
laminas/laminas-code Version ^4.0
jawira/case-converter Version ^3.5