52

GitHub - cerbero90/laravel-enum: Enum generator for Laravel.

 4 years ago
source link: https://github.com/cerbero90/laravel-enum
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Laravel Enum

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Laravel package that introduces a new Artisan command to generate Enum classes.

It provides an easy syntax to specify and map constants in Enum classes while adding PHPDoc tags to make IDEs recognise what constants can be invoked as methods to instantiate an Enum class.

Install

Via Composer

$ composer require cerbero/laravel-enum

Usage

Enums can be generated by calling the Artisan command make:enum and specifying the class and constant names. Many enums can be defined at once by separating them with pipes (please note the use of quotes):

$ php artisan make:enum Status 'IN_PROGRESS|COMPLETE|FAILED'

In the previous example no key has been defined, in this case keys are assumed to be equal to their lowercased constant name. This is how the Status enum will look like:

<?php

namespace App\Enums;

use Rexlabs\Enum\Enum;

/**
 * The Status enum.
 *
 * @method static self IN_PROGRESS()
 * @method static self COMPLETE()
 * @method static self FAILED()
 */
class Status extends Enum
{
    const IN_PROGRESS = 'in_progress';
    const COMPLETE = 'complete';
    const FAILED = 'failed';
}

Nonetheless you may need to define your own keys, that is possible by pairing constant names and keys with an = character:

$ php artisan make:enum Status 'IN_PROGRESS=1|COMPLETE=2|FAILED=3'

The command above will generate the following Progress enum:

<?php

namespace App\Enums;

use Rexlabs\Enum\Enum;

/**
 * The Status enum.
 *
 * @method static self IN_PROGRESS()
 * @method static self COMPLETE()
 * @method static self FAILED()
 */
class Status extends Enum
{
    const IN_PROGRESS = 1;
    const COMPLETE = 2;
    const FAILED = 3;
}

Similarly you can specify enum values by pairing keys and values with an = character:

$ php artisan make:enum Status 'IN_PROGRESS=1=In progress|COMPLETE=2=Complete|FAILED=3=Failed'

The above command will generate the following Status enum and implement the map() method:

<?php

namespace App\Enums;

use Rexlabs\Enum\Enum;

/**
 * The Status enum.
 *
 * @method static self IN_PROGRESS()
 * @method static self COMPLETE()
 * @method static self FAILED()
 */
class Status extends Enum
{
    const IN_PROGRESS = 1;
    const COMPLETE = 2;
    const FAILED = 3;

    /**
     * Retrieve a map of enum keys and values.
     *
     * @return array
     */
    public static function map() : array
    {
        return [
            static::IN_PROGRESS => 'In progress',
            static::COMPLETE => 'Complete',
            static::FAILED => 'Failed',
        ];
    }
}

Sometimes you may want to define array of values in your keys or values, you can do that by providing JSON strings:

$ php artisan make:enum Status 'NAMES={"in_progress":"In progress","complete":"Complete"}'

This package will take care of building, indenting and formatting the array for you:

<?php

namespace App\Enums;

use Rexlabs\Enum\Enum;

/**
 * The Status enum.
 *
 * @method static self NAMES()
 */
class Status extends Enum
{
    const NAMES = [
        'in_progress' => 'In progress',
        'complete' => 'Complete',
    ];
}

By default enums are generated in the app/Enums directory. If you prefer a different location, you can set the option --path (or the shortcut -p):

$ php artisan make:enum Status 'IN_PROGRESS|COMPLETE|FAILED' --path=Other/Directory

The above command will generate the Status class in app/Other/Directory.

If you try to generate an enum that already exists, the existing enum won't be overwritten unless you set the option --force (or the shortcut -f):

$ php artisan make:enum Status 'IN_PROGRESS|COMPLETE|FAILED' --force

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK