Blog Detail

17

Oct
A Simple Opinionated Framework Agnostic PHP 8.1 Enum Helper cover image

arrow_back A Simple Opinionated Framework Agnostic PHP 8.1 Enum Helper

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

  1. Invokable cases: get the value of the enum “invoking” it statically
  2. Construct enum by name or value: from(), tryFrom(), fromName(), tryFromName(), fromValue(), tryFromValue() methods
  3. Enums Inspection: isPure(), isBacked(), has(), hasName(), hasValue() methods
  4. Enums Equality: is(), isNot(), in(), notIn() methods
  5. Names: methods to have a list of case names (names(), namesByValue())
  6. Values: methods to have a list of case values (values(), valuesByName())
  7. Unique ID: get an unique identifier from instance or instance from identifier (uniqueId(), fromUniqueId())
  8. 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.

Published at : 17-10-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

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 your project

Launch project