https://github.com/sourceboat/laravel-enumeration
Enum implementation for Laravel.
https://github.com/sourceboat/laravel-enumeration
hacktoberfest laravel php
Last synced: about 1 year ago
JSON representation
Enum implementation for Laravel.
- Host: GitHub
- URL: https://github.com/sourceboat/laravel-enumeration
- Owner: sourceboat
- License: mit
- Created: 2019-03-04T20:30:26.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2023-01-09T16:03:14.000Z (over 3 years ago)
- Last Synced: 2025-05-23T02:40:59.238Z (about 1 year ago)
- Topics: hacktoberfest, laravel, php
- Language: PHP
- Homepage:
- Size: 185 KB
- Stars: 9
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# laravel-enumeration
[](https://github.com/sourceboat/laravel-enumeration/actions/workflows/test.yaml)
[](https://packagist.org/packages/sourceboat/laravel-enumeration)
[](https://packagist.org/packages/sourceboat/laravel-enumeration)
[](https://packagist.org/packages/sourceboat/laravel-enumeration)
[](LICENSE.md)
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)
## Features
* Key/value-definition via class constants
* Full featured suite of methods
* Enum artisan generator
* Validation rules for passing enum values as input parameters
* Localization support
* Support to give enum Members a weight
* Extensible
## Table of Contents
* [Requirements](#requirements)
* [Install](#install)
* [Generating enums](#generating-enums)
* [Usage](#usage)
* [Methods](#methods)
* [Validation](#validation)
* [Localization](#localization)
* [Custom cast](#custom-cast)
* [Weighted Enums](#weighted-enums)
* [License information](#license-information)
## Requirements
* Laravel 8.0 or newer;
* PHP 7.3 or newer
## Install
Via Composer
``` bash
composer require sourceboat/laravel-enumeration
```
## Generating enums
```php
php artisan make:enum UserType
```
## Usage
Given the following enum:
``` php
value === 1
```
## Methods
### static keys(): array
Returns an array of the keys for an enumeration.
``` php
UserType::keys(); // Returns ['Administrator', 'Moderator', 'Subscriber', 'SuperAdministrator']
```
### static values(): array
Returns an array of the values for an enum.
``` php
UserType::values(); // Returns [0, 1, 2, 3]
```
### key(): string
Returns the key for the given enum value.
``` php
UserType::Moderator()->key(); // Returns 'Moderator'
```
### value(): mixed
Returns the value for the given enum key.
``` php
UserType::Moderator()->value(); // Returns 1
```
### localized(): string
Returns the localized version of the value, default path is `enums..`, path can be overridden by setting `protected static $localizationPath`.
``` php
UserType::SuperAdministrator()->localized(); // Returns for example 'Super Administrator', but `enums.App\Enums\UserType.3` when not set.
```
### is(static): bool
Check if the instance is equal to the given member.
``` php
UserType::SuperAdministrator()->is(UserType::Moderator()); // -> false
UserType::SuperAdministrator()->is(UserType::SuperAdministrator()); // -> true
```
### is(): bool
Check if the instance is equal to the member indicated by the method name.
``` php
UserType::SuperAdministrator()->isModerator(); // -> false
UserType::SuperAdministrator()->isSuperAdministrator(); // -> true
UserType::SuperAdministrator()->isStudent(); // -> throws Eloquent\Enumeration\Exception\UndefinedMemberException
```
### static randomMember(): static
Returns a random member from the enum. Useful for factories.
``` php
UserType::randomMember(); // Returns Administrator(), Moderator(), Subscriber() or SuperAdministrator()
```
### static defaultMember(): static
Returns the default member for the enum. This function defaults to the first member of the enum when not overridden.
``` php
UserType::defaultMember(); // Returns Administrator()
```
### static membersByBlacklist(?array): array
Returns all members except the ones given.
``` php
UserType::membersByBlacklist([UserType::Moderator()]); // Returns Administrator(), Subscriber() and SuperAdministrator()
```
### static toSelectArray(?array): array
Returns the enum for use in a select as value => key. It is also possible to set an optional blacklist-parameter to filter the returned values.
``` php
UserType::toSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'SuperAdministrator']
```
### toLocalizedSelectArray(?array): array
Returns the enum for use in a select as value => localizedValue, where localizedValue is localized using `->localized()`.
Like `toSelectArray` it is possible to set an optional blacklist-parameter to filter the returned values.
``` php
UserType::toLocalizedSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'Super Administrator']
```
## Validation
### Array Validation
You may validate that a 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: `Enumeration::makeRule()`, `Enumeration::makeRuleWithWhitelist($whitelist)` and `Enumeration::makeRuleWithBlacklist($blacklist)`.
``` php
public function store(Request $request)
{
$this->validate($request, [
'user_type' => [
'required',
UserType::makeRule(), // Allows all enumeration values
],
]);
}
```
``` php
public function store(Request $request)
{
$this->validate($request, [
'user_type' => [
'required',
UserType::makeRuleWithWhitelist([UserType::Moderator(), UserType::Subscriber()]), // allows only the values `1` and `2`
],
]);
}
```
``` php
public function store(Request $request)
{
$this->validate($request, [
'user_type' => [
'required',
UserType::makeRuleWithBlacklist([UserType::SuperAdministrator(), UserType::Administrator()]), // allows all values but the values `0` and `3`
],
]);
}
```
## Localization
You can translate the strings returned by the `localized` methods using Laravel's built in [localization](https://laravel.com/docs/5.6/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 Danish.
```php
// resources/lang/en/enums.php
[
UserType::Moderator => 'Moderator',
],
];
```
```php
// resources/lang/da/enums.php
[
UserType::Moderator => 'studievært',
],
];
```
## Custom Cast
You can use the `Enum` custom cast to enable automatic casting of properties to enums. When used, it will set the property to the default members value when set to an invalid value, same goes when the initial value is invalid when getting it, it will return the default member.
```php
Enums::class . ':' . UserType::class,
];
}
```
By default the custom cast treats the property as nullable, meaning that the property can be set to null and also return it, instead of an enum member, this can the disabled on an per property basis:
```php
Enums::class . ':' . UserType::class . ',0', // appending the 0 means it is not nullable,
]; // this seems counter intuitive, but thats the way it is recognized as `false`,
} // as `false` is somehow evaluated as `true`.
```
If the casted attribute is not set to be nullable and/or has a value not represented by the enum, you will get the default member of the enum when accessing the attribute.
```php
// For example when the value has been changed manually in the database. Let's say the type is `10`.
$type = $user->type
// Then the following will be the case:
echo $type === UserType::defaultMember(); // "true"
echo $type->value; // "0"
```
Since Laravel 7.5 and version 2.1 of this package it is also possible to use the enum itself as cast type hint, but beware that `null` is always an allowed value.
```php
UserType::class,
];
}
```
## Weighted Enums
It is Possible to define weights for enum members. the standard way is to define the weights a config file and them implement the `Weighted`-interface with the `IsWeighted`-trait and define the path to your config. The weights can be defined as integer or float values.
``` php
// The Enum
`
* `Weighted::getMembersGreaterThanOrEqualToThis(): array`
* `Weighted::getMembersEqualToThis(): array`
* `Weighted::getMembersLessThanOrEqualToThis(): array`
* `Weighted::getMembersLessThanThis(): array`
* `static Weighted::getMembersGreaterThan(Weighted): array`
* `static Weighted::getMembersGreaterThanOrEqualTo(Weighted): array`
* `static Weighted::getMembersEqualTo(Weighted): array`
* `static Weighted::getMembersLessThanOrEqualTo(Weighted): array`
* `static Weighted::getMembersLessThan(Weighted): array`
* `static Weighted::getMembersBetween(Weighted $lowerMember, Weighted $higherMember): array`
* `static Weighted::getMembersBetweenOrEqualTo(Weighted $lowerMember, Weighted $higherMember): array`
### Model-trait `HasWeightedEnumScopes`
With the model trait `HasWeightedEnumsScopes` you can easily search for models with enum values greater than one or between two other enum value, even with string values.
Available scopes:
* `whereGreaterThanEnum(string $attribute, Weighted $member)`
* `whereGreaterThanOrEqualToEnum(string $attribute, Weighted $member)`
* `whereEqualToEnum(string $attribute, Weighted $member)`
* `whereLessThanOrEqualToEnum(string $attribute, Weighted $member)`
* `whereLessThanEnum(string $attribute, Weighted $member)`
* `whereBetweenEnum(string $attribute, Weighted $lowerMember, Weighted $higherMember)`
* `whereBetweenOrEqualEnum(string $attribute, Weighted $lowerMember, Weighted $higherMember)`
## Configurable Enums
It is possible to define config values for enums and access them without defining the whole path, but a logical part of it.
The default path for the configuration is: `enums...`
For example:
``` php
// The Enum
[
'permissions' => [
UserType::Administrator => [
'user.foreign.edit',
'user.foreign.delete',
],
UserType::Subscriber => [
'user.self.edit',
'user.self.delete',
],
]
]
]
```
then you can access these values by:
``` php
UserType::Subscriber()->config('permissions'); // which return the given array.
UserType::Moderator()->config('permissions', ['thread.foreign.archive']); // you can also define a default value.
```
## License information
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. Some code has been copied from [eloquent/enumeration](https://github.com/eloquent/enumeration), as at first this package build upon it, but due to depending only on a fraction of functionality we concluded it would be better to internalize the code that we used.
- [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) is licensed under MIT
- [eloquent/enumeration](https://github.com/eloquent/enumeration) is licensed under MIT
- [laravel/framework](https://github.com/laravel/framework) is licensed under MIT
This package is also licensed under the MIT license.