Download the PHP package biiiiiigmonster/laravel-enum without Composer
On this page you can find all versions of the php package biiiiiigmonster/laravel-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download biiiiiigmonster/laravel-enum
More information about biiiiiigmonster/laravel-enum
Files in biiiiiigmonster/laravel-enum
Package laravel-enum
Short Description laravel enum helper base php version 8.1
License MIT
Homepage https://github.com/biiiiiigmonster/laravel-enum
Informations about the package laravel-enum
laravel enum helper base php version 8.1
Enum helper for laravel10 based on the enum feature of php 8.1.
Installation
You can install the package via composer:
Usage
To get started, enums typically live in the app\Enums
directory. You may use the make:enum
Artisan command to generate a new enum:
if you want to generate a backed enum, you may use the make:enum
Artisan command with --type
options:
and also you can apply the trait on your exists enum:
Invokable
This helper lets you get the primitive value of a backed enum, or the name of a pure enum, by "invoking" it — either statically (MyEnum::FOO()
instead of MyEnum::FOO
), or as an instance ($enum()
).
That way, you can use enums as array keys:
Or access the underlying primitives for any other use cases:
The main point: this is all without having to append ->value
to everything:
Use static calls to get the primitive value
Invoke instances to get the primitive value
Enhancement
Helper provide many static methods for you to enhance experience with enums.
Names
This helper returns a list of case names in the enum.
Values
This helper returns a list of case values for backed enums, or a list of case names for pure enums (making this functionally equivalent to ::names()
for pure Enums)
Options
This helper returns an array, that key is each instance invoke ()
return, and value is instance ->label()
returns.
Tables
This helper returns a list of case map array that each instance, if instance append attributes that extended Meta
, the map array including more.
From
This helper adds from()
and tryFrom()
to pure enums, and adds fromName()
and tryFromName()
to all enums.
Important Notes:
BackedEnum
instances already implement their ownfrom()
andtryFrom()
methods, which will not be overridden by this trait. Attempting to override those methods in aBackedEnum
causes a fatal error.- Pure enums only have named cases and not values, so the
from()
andtryFrom()
methods are functionally equivalent tofromName()
andtryFromName()
Use the from()
method
Use the tryFrom()
method
Use the fromName()
method
Use the tryFromName()
method
Random
This helper returns an instance of case by random.
Default Case
Sometimes you may need to specify default case for your enum, which is easy as below: simply append the #[DefaultCase]
attribute to the case:
Then use the ::default()
static method to get this case instance:
Meta
This feature lets you add metadata to enum cases, it's used by way of attributes.
Creating meta attributes
To generate a new meta attributes, you may use the make:enumMeta
Artisan command:
meta attribute needs to exist as an Attribute.
Inside the attribute, you can customize a few things. For instance, you may want to use a different method name than the one derived from the class name (Description
becomes description()
by default). To do that, define the alias
static property on the meta:
With the code above, the ->description()
of a case will be accessible as ->note()
.
Another thing you can customize is the passed value. For instance, to wrap a color name like text-{$color}-500
, you'd add the following transform()
method:
And now the returned color will be correctly transformed:
Access the metadata
By accessing the attribute method name, you can get the meta value:
Also, ::tables()
static method can return all meta attribute maps on each instance.
Use the fromMeta()
method
Similarly, you can also get the enum case instance through the meta instance.
Use the tryFromMeta()
method
Validation
Usually, we need limit your application's incoming data to a specified enums, laravel provides the basic rule, but here we have perfected it.
Array Validation
You can use the 'array' syntax for rules.
Enum
Validate that a parameter is an instance of a given enum, it's similar to Enum Rules
and can support pure enums.
EnumMeta
Additionally, validate that a parameter is an instance of the given meta in the given enum.
EnumMeta
rule takes two parameters, the first is given enum, the second is given meta, if parameter name is same of meta method name, you can omit it:
Pipe Validation
You can also use the 'pipe' syntax for rules.
- enumerate: _enumclass
- enum_meta: _enum_class,[metaattribute]
Validation messages
If needed, you can modify the error message when validated fails.
Run the following command to publish the language files to your lang
folder:
Localization
Labels
The enum instances are descriptive, and we have added translation capabilities for this.
You can translate the strings returned by the enum instance's ->label()
method using Laravel's built-in localization features.
Add a new enums.php
keys file for each of your supported languages. In this example there is one for English and one for Spanish:
Now, you just need to make sure that your enum implements the Localizable
interface as demonstrated below:
Alternatively, when creating with the make:enum
Artisan command, add the --local
option:
The ->label()
method will now look for the value in your localization files:
and the ::options()
static method returned array's value also be localized:
Artisan Command
If you want your IDE to autocomplete the static instantiation helpers, you can generate PHPDoc annotations through an artisan command.
By default, all Enums in app/Enums
will be annotated (you can change the folder by passing a path to --folder
)
Also, you can annotate a single class by specifying the class name
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
References
License
The MIT License (MIT). Please see License File for more information.