bensampo/laravel-enum

This package adds support for creating enums in PHP and includes a generator for Laravel.

1.10.1 2018-08-10 20:52 UTC

README

This package adds support for creating enums in PHP and includes a generator for Laravel.

Build Status Packagist Stable Version Packagist downloads MIT Software License

Guide

I wrote a blog post about using laravel-enum: https://sampo.co.uk/blog/using-enums-in-laravel

Install

Via Composer

$ composer require bensampo/laravel-enum

If you're using Laravel < 5.5 you'll need to add the service provider to config/app.php

'BenSampo\Enum\EnumServiceProvider'

Generating enums

php artisan make:enum UserType

Usage

Given the following enum:

<?php

namespace App\Enums;

use BenSampo\Enum\Enum;

final class UserType extends Enum
{
    const Administrator = 0;
    const Moderator = 1;
    const Subscriber = 2;
    const SuperAdministrator = 3;
}

Values can now be accessed like so:

UserType::Moderator // Returns 1

Methods

getKeys(): array

Returns an array of the keys for an enum.

UserType::getKeys(); // Returns ['Administrator', 'Moderator', 'Subscriber', 'SuperAdministrator']

getValues(): array

Returns an array of the values for an enum.

UserType::getValues(); // Returns [0, 1, 2, 3]

getKey(int $value): string

Returns the key for the given enum value.

UserType::getKey(1); // Returns 'Moderator'
UserType::getKey(UserType::Moderator); // Returns 'Moderator'

getValue(string $key): int

Returns the value for the given enum key.

UserType::getValue('Moderator'); // Returns 1

getDescription(int $value): string

Returns the key in sentence case for the enum value. It's possible to override the getDescription method to return custom descriptions.

UserType::getDescription(3); // Returns 'Super administrator'
UserType::getDescription(UserType::SuperAdministrator); // Returns 'Super administrator'

getRandomKey(): string

Returns a random key from the enum. Useful for factories.

UserType::getRandomKey(); // Returns 'Administrator', 'Moderator', 'Subscriber' or 'SuperAdministrator'

getRandomValue(): int

Returns a random value from the enum. Useful for factories.

UserType::getRandomValue(); // Returns 0, 1, 2 or 3

toArray(): array

Returns the enum key value pairs as an associative array.

UserType::toArray(); // Returns ['Administrator' => 0, 'Moderator' => 1, 'Subscriber' => 2, 'SuperAdministrator' => 3]

toSelectArray(): array

Returns the enum for use in a select as value => description.

UserType::toSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'Super administrator']

Validation

You may validate that an enum value passed to a controller is a valid value for a given enum by using the EnumValue rule.

public function store(Request $request)
{
    $this->validate($request, [
        'user_type' => ['required', new EnumValue(UserType::class)],
    ]);
}

By default type checking is set to strict, but you can bypass this passing false to the optional second parameter of the EnumValue class.

new EnumValue(UserType::class, false) // Turn off strict type checking.

You can also validate on keys using the EnumKey rule. This is useful if you're taking the enum key as a URL parameter for sorting or filtering for example.

public function store(Request $request)
{
    $this->validate($request, [
        'user_type' => ['required', new EnumKey(UserType::class)],
    ]);
}

Of course, both of these work on form request classes too.

Make sure to include BenSampo\Enum\Rules\EnumValue and/or BenSampo\Enum\Rules\EnumKey and your enum class in the usings.

Localization

You can translate the strings returned by the getDescription method using Laravel's built in localization features.

Set up your translation keys files. In this example there is one for English and one for Spanish.

// resources/lang/en/enums.php
<?php

use App\Enums\UserType;

return [

    'user-type' => [
        UserType::Administrator => 'Administrator',
        UserType::SuperAdministrator => 'Super administrator',
    ],

];
// resources/lang/es/enums.php
<?php

use App\Enums\UserType;

return [

    'user-type' => [
        UserType::Administrator => 'Administrador',
        UserType::SuperAdministrator => 'Súper administrador',
    ],

];

On your enum, change/add the getDescription method as follows:

public static function getDescription(int $value): string
{
    $localizedStringKey = 'enums.user-type.' . $value;

    if (strpos(__($localizedStringKey), 'enums.') !== 0) {
        return __($localizedStringKey);
    }

    return parent::getDescription($value);
}

Remember to change user-type in the $localizedStringKey in the example to the name of your enum.

The getDescription method will now look for the value in your localization files. If a value doesn't exist for a given key, the key name is returned instead.

Overriding the getDescription method

If you'd like to return a custom value from the getDescription method, you may do so by overriding the method on your enum:

public static function getDescription($value): string
{
    if ($value === self::SuperAdministrator) {
        return 'Super admin';
    }

    return parent::getDescription($value);
}

Calling UserType::getDescription(3); now returns Super admin instead of Super administator.

Extending the enum base class

The Enum base class implements the Laravel Macroable trait, meaning it's easy to extend it with your own functions. If you have a function that you often add to each of your enums, you can use a macro.

Let's say we want to be able to get a flipped version of the enum toArray method, we can do this using:

Enum::macro('toFlippedArray', function() {
    return array_flip(self::toArray());
});

Now, on each of my enums, I can call it using UserType::toFlippedArray().

It's best to register the macro inside of a service providers' boot method.