2017 © Pedro Peláez
 

library laravel-enums

Laravel package for Enum implementation.

image

nasyrov/laravel-enums

Laravel package for Enum implementation.

  • Wednesday, April 4, 2018
  • by nasyrov
  • Repository
  • 1 Watchers
  • 24 Stars
  • 6,592 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 11 Versions
  • 32 % Grown

The README.md

Laravel Enums

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-scrutinizer] Quality Score ![Total Downloads][ico-downloads], (*1)

Laravel package for Enum implementation., (*2)

Requirements

Make sure all dependencies have been installed before moving on:, (*3)

Install

Pull the package via Composer:, (*4)

``` bash $ composer require nasyrov/laravel-enums, (*5)


Register the service provider in `config/app.php`: ``` php 'providers' => [ ... Nasyrov\Laravel\Enums\EnumServiceProvider::class, ... ]

Usage

Generate a new enum class via the command:, (*6)

``` bash $ php artisan make:enum UserStatusEnum, (*7)


Define the enum constants: ``` php /** * @method static UserStatusEnum ACTIVE() * @method static UserStatusEnum INACTIVE() */ class UserStatusEnum extends Enum { const ACTIVE = 10; const INACTIVE = 20; }

To use the enum new up the instance or simply call via the static methods:, (*8)

``` php $status = new UserStatusEnum(UserStatusEnum::ACTIVE); $status = UserStatusEnum::ACTIVE();, (*9)


Type-hint the model accessor: ``` php public function getStatusAttribute($attribute) { return new UserStatusEnum($attribute); }

Type-hint the model mutator:, (*10)

``` php public function setStatusAttribute(UserStatusEnum $attribute) { $this->attributes['status'] = $attribute->getValue(); }, (*11)


Validation: ``` php $this->validate($request, [ 'status' => [ 'required', Rule::in(UserStatusEnum::values()), ], ]);

Localization:, (*12)

``` php use Nasyrov\Laravel\Enums\Enum as BaseEnum;, (*13)

abstract class Enum extends BaseEnum { /** * Get the enum labels. * * @return array */ public static function labels() { return static::constants() ->flip() ->map(function ($key) { // Place your translation strings in resources/lang/en/enum.php return trans(sprintf('enum.%s', strtolower($key))); }) ->all(); } }, (*14)


``` blade <select name="status"> @foreach (UserStatusEnum::labels() as $value => $label) <option value="{{ $value }}"> {{ $label }} </option> @endforeach </select>

Testing

bash $ composer lint $ composer test, (*15)

Security

If you discover any security related issues, please email inasyrov@ya.ru instead of using the issue tracker., (*16)

Credits

License

The MIT License (MIT). Please see License File for more information., (*17)

The Versions

04/04 2018

dev-mrhn-feature/json-encode-enum

dev-mrhn-feature/json-encode-enum https://github.com/nasyrov/laravel-enums

Laravel package for Enum implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel enum

26/03 2018

dev-mrhn-feature/#13-upgrade-to-5.6-from-master

dev-mrhn-feature/#13-upgrade-to-5.6-from-master https://github.com/nasyrov/laravel-enums

Laravel package for Enum implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel enum

12/10 2017
17/06 2017
16/06 2017
13/06 2017
12/06 2017
12/06 2017

v1.0.0

1.0.0.0 https://github.com/nasyrov/laravel-enum

Laravel package for Enum implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel enum