A simple and opinionated collection of PHP 8.1 enum helpers inspired by archtechx/enums and BenSampo/laravel-enum.
This package is framework agnostic, but if you use Laravel consider using this linked package datomatic/laravel-enum-helper and datomatic/laravel-enum-collections.
Functionalities summary
- Invokable cases: get the value of the enum “invoking” it statically
- Construct enum by name or value: from(),
tryFrom(),fromName(),tryFromName(),fromValue(),tryFromValue()methods - Enums Inspection:
isPure(),isBacked(),has(),hasName(), hasValue() methods - Enums Equality:
is(),isNot(),in(),notIn()methods - Names: methods to have a list of case names (
names(),namesByValue()) - Values: methods to have a list of case values (
values(),valuesByName()) - Unique ID: get an unique identifier from instance or instance from identifier (
uniqueId(),fromUniqueId()) - Descriptions & Translations: add description to enum with optional translation (
description(),descriptions(),descriptionsByName(),descriptionsByValue(),nullableDescriptionsByValue())
Installation
For using this package, PHP 8.1+ is required.
You can install this package via composer.
composer require datomatic/enum-helper
Usage
You can use the traits you need, but for convenience, you can use only the EnumHelper trait that includes (EnumInvokable, EnumFroms, EnumNames, EnumValues, EnumInspection, EnumEquality).
EnumDescription and EnumUniqueId are separated from EnumHelper because they cover edge cases.
The helper support both pure enum (e.g. PureEnum, PascalCasePureEnum) and BackedEnum (e.g. IntBackedEnum, StringBackedEnum).
In all examples we’ll use the classes described below:
use Datomatic\EnumHelper\EnumHelper;
// Pure enum
enum PureEnum
{
use EnumHelper;
case PENDING;
case ACCEPTED;
case DISCARDED;
case NO_RESPONSE;
}
enum PascalCasePureEnum
{
use EnumHelper;
case Pending;
case Accepted;
case Discarded;
case NoResponse;
}
// BackedEnum
enum StringBackedEnum: string
{
use EnumHelper;
case PENDING = 'P';
case ACCEPTED = 'A';
case DISCARDED = 'D';
case NO_RESPONSE = 'N';
}
enum IntBackedEnum: int
{
use EnumHelper;
case PENDING = 0;
case ACCEPTED = 1;
case DISCARDED = 2;
case NO_RESPONSE = 3;
}
The package works with cases written in UPPER_CASE, snake_case and PascalCase.
These are the following methods available in this package.
- Invokable Cases
- Froms
- Enums Inspection
- Enums Equality
- Names
- Values
- Unique ID
- Descriptions & Translations
You can visit the complete documentation of the above methods on Github.
Closing Note
If you are an enterprise and want to use develop a CRM, e-commerce, or SAAS web application in Laravel, you must discover an impeccable team with proficiency in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Our CRM development with Laravel holds a wide range of features for entities to manage their interactions with customers.
So if you have a great idea, then please feel free to contact us or you can launch a project with us.