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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-enum

laravel enum helper base php version 8.1

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Coverage Status Scrutinizer Code Quality Total Downloads

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:

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.

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.


All versions of laravel-enum with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version >=10
laminas/laminas-code Version ^4.10
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package biiiiiigmonster/laravel-enum contains the following files

Loading the files please wait ....