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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-enumeration

laravel-enumeration

Tests Latest Version on Packagist Packagist Stable Version Packagist downloads

Enum implementation for Laravel. Based on eloquent/enumeration and inspired by bensampo/laravel-enum

Features

Table of Contents

Requirements

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:

The following methods for filtering the enum are available:

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:

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.

This package is also licensed under the MIT license.


All versions of laravel-enumeration with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
illuminate/console Version ^8.0|^9.0
illuminate/contracts Version ^8.0|^9.0
illuminate/support Version ^8.0|^9.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sourceboat/laravel-enumeration contains the following files

Loading the files please wait ....