17
OctA 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.
tryFrom()
, fromName()
, tryFromName()
, fromValue()
, tryFromValue()
methodsisPure()
, isBacked()
, has()
, hasName()
, hasValue() methodsis()
, isNot()
, in()
, notIn()
methodsnames()
, namesByValue()
)values()
, valuesByName()
)uniqueId()
, fromUniqueId()
)description()
,descriptions()
,descriptionsByName()
,descriptionsByValue()
,nullableDescriptionsByValue()
)For using this package, PHP 8.1+ is required.
You can install this package via composer.
composer require datomatic/enum-helper
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.
You can visit the complete documentation of the above methods on Github.
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.
Published at : 17-10-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project