Download the PHP package sourceboat/laravel-enumeration without Composer
On this page you can find all versions of the php package sourceboat/laravel-enumeration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sourceboat/laravel-enumeration
More information about sourceboat/laravel-enumeration
Files in sourceboat/laravel-enumeration
Package laravel-enumeration
Short Description Simple, extensible and powerful enumeration implementation for Laravel, based on eloquent/enumeration.
License MIT
Homepage https://github.com/sourceboat/laravel-enumeration
Informations about the package laravel-enumeration
laravel-enumeration
Enum implementation for Laravel. Based on eloquent/enumeration and inspired by 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
- Install
- Generating enums
- Usage
- Methods
- Validation
- Localization
- Custom cast
- Weighted Enums
- License information
Requirements
- Laravel 8.0 or newer;
- PHP 7.3 or newer
Install
Via Composer
Generating enums
Usage
Given the following enum:
Values can now be accessed like so:
Methods
static keys(): array
Returns an array of the keys for an enumeration.
static values(): array
Returns an array of the values for an enum.
key(): string
Returns the key for the given enum value.
value(): mixed
Returns the value for the given enum key.
localized(): string
Returns the localized version of the value, default path is enums.<EnumClass>.<EnumValue>
, path can be overridden by setting protected static $localizationPath
.
is(static): bool
Check if the instance is equal to the given member.
is(): bool
Check if the instance is equal to the member indicated by the method name.
static randomMember(): static
Returns a random member from the enum. Useful for factories.
static defaultMember(): static
Returns the default member for the enum. This function defaults to the first member of the enum when not overridden.
static membersByBlacklist(?array): array
Returns all members except the ones given.
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.
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.
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)
.
Localization
You can translate the strings returned by the localized
methods using Laravel's built in 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.
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.
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:
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.
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.
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.
For further customization the Weighted::weight(): int|float
-method can be overridden to use your own way of defining the weights.
The following methods for comparing weighted enum members are available:
Weighted::isGreaterThan(Weighted): bool
Weighted::isGreaterThanOrEuqalTo(Weighted): bool
Weighted::isEuqalTo(Weighted): bool
Weighted::isLessThanOrEqualTo(Weighted): bool
Weighted::isLessThan(Weighted): bool
The following methods for filtering the enum are available:
Weighted::getMembersGreaterThanThis(): array<Weighted>
Weighted::getMembersGreaterThanOrEqualToThis(): array<Weighted>
Weighted::getMembersEqualToThis(): array<Weighted>
Weighted::getMembersLessThanOrEqualToThis(): array<Weighted>
Weighted::getMembersLessThanThis(): array<Weighted>
static Weighted::getMembersGreaterThan(Weighted): array<Weighted>
static Weighted::getMembersGreaterThanOrEqualTo(Weighted): array<Weighted>
static Weighted::getMembersEqualTo(Weighted): array<Weighted>
static Weighted::getMembersLessThanOrEqualTo(Weighted): array<Weighted>
static Weighted::getMembersLessThan(Weighted): array<Weighted>
static Weighted::getMembersBetween(Weighted $lowerMember, Weighted $higherMember): array<Weighted>
static Weighted::getMembersBetweenOrEqualTo(Weighted $lowerMember, Weighted $higherMember): array<Weighted>
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.<enum class>.<given key>.<enum value>
For example:
then you can access these values by:
License information
Much of the functionality in this Package is inspired by 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, 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 is licensed under MIT
- eloquent/enumeration is licensed under MIT
- laravel/framework is licensed under MIT
This package is also licensed under the MIT license.
All versions of laravel-enumeration with dependencies
illuminate/console Version ^8.0|^9.0
illuminate/contracts Version ^8.0|^9.0
illuminate/support Version ^8.0|^9.0