|
| 1 | +# laravel-enumeration |
| 2 | + |
| 3 | +Strongly typed enum implementation for Laravel. Based on [eloquent/enumeration](https://github.com/eloquent/enumeration) and inspired by [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* Strongly typed |
| 8 | +* Key/value-definition via class constants |
| 9 | +* Full featured suite of methods |
| 10 | +* Enum artisan generator |
| 11 | +* Validation rules for passing enum values as input parameters |
| 12 | +* Localization support |
| 13 | +* Extendible |
| 14 | + |
| 15 | +[](https://travis-ci.org/sourceboat/laravel-enumeration) |
| 16 | +[](https://packagist.org/packages/sourceboat/laravel-enumeration) |
| 17 | +[](https://packagist.org/packages/sourceboat/laravel-enumeration) |
| 18 | +[](LICENSE.md) |
| 19 | + |
| 20 | +* [Guide](#guide) |
| 21 | +* [Install](#install) |
| 22 | +* [Generating enums](#generating-enums) |
| 23 | +* [Usage](#usage) |
| 24 | +* [Methods](#methods) |
| 25 | +* [Validation](#validation) |
| 26 | +* [Localization](#localization) |
| 27 | +* [License information](#license-infromation) |
| 28 | + |
| 29 | +## Requirements |
| 30 | + |
| 31 | +eloquent/enumeration 6.0 or newer |
| 32 | +Laravel 5.4 or newer; Tested with 5.7 |
| 33 | +PHP 7.1 or newer |
| 34 | + |
| 35 | +## Install |
| 36 | + |
| 37 | +Via Composer |
| 38 | + |
| 39 | +``` bash |
| 40 | +$ composer require sourceboat/laravel-enumeration |
| 41 | +``` |
| 42 | + |
| 43 | +If you're using Laravel < 5.5 you'll need to add the service provider to `config/app.php` |
| 44 | +``` php |
| 45 | +'Sourceboat\Enumeration\EnumerationServiceProvider' |
| 46 | +``` |
| 47 | + |
| 48 | +## Generating enums |
| 49 | + |
| 50 | +```php |
| 51 | +php artisan make:enum UserType |
| 52 | +``` |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +Given the following enum: |
| 57 | +``` php |
| 58 | +<?php |
| 59 | + |
| 60 | +namespace App\Enums; |
| 61 | + |
| 62 | +use BenSampo\Enum\Enum; |
| 63 | + |
| 64 | +/** |
| 65 | + * @method static Administrator() // These are only for autocompletion etc. |
| 66 | + * @method static Moderator() |
| 67 | + * @method static Subscriber() |
| 68 | + * @method static SuperAdministrator() |
| 69 | + */ |
| 70 | +final class UserType extends Enum |
| 71 | +{ |
| 72 | + const Administrator = 0; |
| 73 | + const Moderator = 1; |
| 74 | + const Subscriber = 2; |
| 75 | + const SuperAdministrator = 3; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Values can now be accessed like so: |
| 80 | +``` php |
| 81 | +UserType::Moderator() // Returns an instance of UserType with UserType::Moderator()->value === 1 |
| 82 | +``` |
| 83 | + |
| 84 | +## Methods |
| 85 | + |
| 86 | +### static keys(): array |
| 87 | + |
| 88 | +Returns an array of the keys for an enumeration. |
| 89 | + |
| 90 | +``` php |
| 91 | +UserType::keys(); // Returns ['Administrator', 'Moderator', 'Subscriber', 'SuperAdministrator'] |
| 92 | +``` |
| 93 | + |
| 94 | +### static values(): array |
| 95 | + |
| 96 | +Returns an array of the values for an enum. |
| 97 | + |
| 98 | +``` php |
| 99 | +UserType::values(); // Returns [0, 1, 2, 3] |
| 100 | +``` |
| 101 | + |
| 102 | +### key(): string |
| 103 | + |
| 104 | +Returns the key for the given enum value. |
| 105 | + |
| 106 | +``` php |
| 107 | +UserType::Modelerator()->key(); // Returns 'Moderator' |
| 108 | +``` |
| 109 | + |
| 110 | +### value(): mixed |
| 111 | + |
| 112 | +Returns the value for the given enum key. |
| 113 | + |
| 114 | +``` php |
| 115 | +UserType::Moderator()->value(); // Returns 1 |
| 116 | +``` |
| 117 | + |
| 118 | +### localized(): string |
| 119 | + |
| 120 | +Returns the localized version of the value, default path is `enums.<EnumClass>.<EnumValue>`, path can be overridden by setting `protected static $localizationPath`. |
| 121 | + |
| 122 | +``` php |
| 123 | +UserType::SuperAdministrator()->localized(); // Returns for example 'Super Administrator', but `enums.UserType.3` when not set. |
| 124 | +``` |
| 125 | + |
| 126 | +### static randomMember(): static |
| 127 | + |
| 128 | +Returns a random key from the enum. Useful for factories. |
| 129 | + |
| 130 | +``` php |
| 131 | +UserType::randomMember(); // Returns Administrator(), Moderator(), Subscriber() or SuperAdministrator() |
| 132 | +``` |
| 133 | + |
| 134 | +### static toSelectArray(): array |
| 135 | + |
| 136 | +Returns the enum for use in a select as value => description. |
| 137 | + |
| 138 | +``` php |
| 139 | +UserType::toSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'SuperAdministrator'] |
| 140 | +``` |
| 141 | + |
| 142 | +### toLocalizedSelectArray(): array |
| 143 | + |
| 144 | +Returns the enum for use in a select as value => description, where description is localized using `->localized()`. |
| 145 | + |
| 146 | +``` php |
| 147 | +UserType::toSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'Super Administrator'] |
| 148 | +``` |
| 149 | + |
| 150 | +## Validation |
| 151 | + |
| 152 | +### Array Validation |
| 153 | +You may validate that an enum value passed to a controller is a valid value for a given enum by using the `EnumerationValue` rule, for easier handling there are helper methods for creating the rule: `Enumeraction::makeRule()`, `Enumeration::makeRuleWithWhitelist($whitelist)` and `Enumeration::makeRuleWithBlacklist($blacklist)`. |
| 154 | + |
| 155 | +``` php |
| 156 | +public function store(Request $request) |
| 157 | +{ |
| 158 | + $this->validate($request, [ |
| 159 | + 'user_type' => ['required', UserType::makeRule()], // Allows all enumeration values |
| 160 | + ]); |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +``` php |
| 165 | +public function store(Request $request) |
| 166 | +{ |
| 167 | + $this->validate($request, [ |
| 168 | + 'user_type' => ['required', UserType::makeRuleWithWhitelist([UserType::Moderator(), UserType::Subscriber()])], // allows only the values `1` and `2` |
| 169 | + ]); |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +``` php |
| 174 | +public function store(Request $request) |
| 175 | +{ |
| 176 | + $this->validate($request, [ |
| 177 | + 'user_type' => ['required', UserType::makeRuleWithBlacklist([UserType::SuperAdministrator(), UserType::Administrator()])], // allows all values but the values `0` and `3` |
| 178 | + ]); |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +## Localization |
| 183 | + |
| 184 | +You can translate the strings returned by the `getDescription` method using Laravel's built in [localization](https://laravel.com/docs/5.6/localization) features. |
| 185 | + |
| 186 | +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. |
| 187 | + |
| 188 | +```php |
| 189 | +// resources/lang/en/enums.php |
| 190 | +<?php |
| 191 | + |
| 192 | +use App\Enums\UserType; |
| 193 | + |
| 194 | +return [ |
| 195 | + |
| 196 | + UserType::class => [ |
| 197 | + UserType::Administrator => 'Administrator', |
| 198 | + UserType::SuperAdministrator => 'Super administrator', |
| 199 | + ], |
| 200 | + |
| 201 | +]; |
| 202 | +``` |
| 203 | + |
| 204 | +```php |
| 205 | +// resources/lang/es/enums.php |
| 206 | +<?php |
| 207 | + |
| 208 | +use App\Enums\UserType; |
| 209 | + |
| 210 | +return [ |
| 211 | + |
| 212 | + UserType::class => [ |
| 213 | + UserType::Administrator => 'Administrador', |
| 214 | + UserType::SuperAdministrator => 'Súper administrador', |
| 215 | + ], |
| 216 | + |
| 217 | +]; |
| 218 | +``` |
| 219 | + |
| 220 | +## License information |
| 221 | + |
| 222 | +Much of the functionality in this Package is inspired by [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) and some code has been taken from it and modified, for example the `MakeEnumCommand.php`, the `EnumServiceProvider.php` and this readme. |
| 223 | + |
| 224 | +- [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) is licensed under MIT |
| 225 | +- [eloquent/enumeration](https://github.com/eloquent/enumeration) is licensed under MIT |
| 226 | +- [laravel/framework](https://github.com/laravel/framework) is licensed under MIT |
| 227 | + |
| 228 | +This package is also licensed under the MIT license. |
| 229 | + |
| 230 | +## TODOs |
| 231 | + |
| 232 | +* [ ] Tests |
| 233 | +* [ ] Model-Trait to enable casting of enumerations and "on the fly"-validation for enumeration values on models. |
0 commit comments