• Reading time ~ 4 min
  • 22.08.2023

EnumConcern is a newly introduced PHP package tailored to streamline the usage of PHP's Enum feature by providing a comprehensive suite of methods. Backed by Laravel Collections, EnumConcern aims to offer a seamless and familiar experience.

Key Features of EnumConcern:

  • Versatility: EnumConcern includes a variety of methods that enable easy handling of Enums, such as all, has, caseExists, and more.
  • Conversion Capabilities: The package offers methods to convert Enums to various formats like JSON, array, or key-value collections.
  • Customization: There's flexibility in obtaining subsets of values with the methods only, onlyAsArray, except, exceptAsArray, etc.
  • Utility Functions: Functions like randomValue, randomCase, first, and last provide extra utility.

EnumConcern Usage:

EnumConcern integrates smoothly into existing enums, enabling functions like translation and various checks. Here's an example of how to translate color Enums to Turkish:

namespace App\Enums;

use EmreYarligan\EnumConcern\EnumConcern;

enum Color: string
{
    use EnumConcern;

    case RED = "Red";
    case GREEN = "Green";
    case BLUE = "Blue";

    public function translateToTurkish(): string
    {
        return match ($this) {
            self::RED    => 'Kırmızı',
            self::GREEN  => 'Yeşil',
            self::BLUE   => 'Mavi',
        };
    }
}
Color::all();
// Result: Illuminate\Support\Collection (7) [
//   [0] => 'Red',
//   [1] => 'Green',
//   [2] => 'Blue
// ]
Color::all('translateToTurkish');
// Result: Illuminate\Support\Collection (7) [
//   [0] => 'Kırmızı',
//   [1] => 'Yeşil',
//   [2] => 'Mavi
// ]
Color::has('Purple');
// false
Color::has('Mavi','translateToTurkish');
// true

With functions like Color::all('translateToTurkish'), you can retrieve translated values effortlessly.

EnumConcern Methods

  • all: Get all the values as a Collection. Parameters: method = '' (optional). Return Type: Collection.
  • allAsArray: Get all the values as an array. Parameters: method = '' (optional). Return Type: array.
  • has: Check if a specific value exists. Parameters: value, method = '' (optional). Return Type: bool.
  • caseExists: Check if a specific case (key / name) exists. Parameters: value, method = '' (optional). Return Type: bool.
  • allCasesExists: Check if all the given cases (keys/names) exist. Parameters: cases, method = '' (optional). Return Type: bool.
  • anyCaseExists: Check if any of the given cases (keys/names) exist. Parameters: cases, method = '' (optional). Return Type: bool.
  • caseByValue: Get the case (key/name) for a specific value. Parameters: value, method = '' (optional). Return Type: string.
  • toJson: Convert all the values to a JSON string. Parameters: method = '' (optional), jsonEncodeOption (optional). Return Type: string.
  • toArray: Convert all the values to an array. Parameters: method = '' (optional). Return Type: array.
  • toKeyValueCollection: Convert all the values to a key-value format as a Collection. Parameters: method = '' (optional). Return Type: Collection.
  • toKeyValueArray: Convert all the values to a key-value format as an array. Parameters: method = '' (optional). Return Type: array.
  • randomValue: Get a random value from the collection of values. Parameters: method = '' (optional). Return Type: mixed.
  • randomCase: Get a random case (key/name) from the collection of values. Parameters: None. Return Type: string.
  • casesCollection: Get all the cases (keys/names) of the Enum as a Collection. Parameters: None. Return Type: Collection.
  • casesArray: Get all the cases (keys/names) of the Enum as an array. Parameters: None. Return Type: array.
  • allToArray: Get all the values as an array (Alias for toArray method). Parameters: method = '' (optional). Return Type: array.
  • only: Get a subset of the values as a Collection, only including the specified cases (keys/names). Parameters: cases, method = '' (optional). Return Type: Collection.
  • onlyAsArray: Get a subset of the values as an array, only including the specified cases (keys/names). Parameters: cases, method = '' (optional). Return Type: array.
  • except: Get a subset of the values as a Collection, excluding the specified cases (keys/names). Parameters: cases, method = '' (optional). Return Type: Collection.
  • exceptAsArray: Get a subset of the values as an array, excluding the specified cases (keys/names). Parameters: cases, method = '' (optional). Return Type: array.
  • first: Get the first value in the Enum. Parameters: method = '' (optional). Return Type: mixed.
  • last: Get the last value in the Enum. Parameters: method = '' (optional). Return Type: mixed.

Conclusion:

EnumConcern offers a robust solution for managing Enums in PHP, integrating seamlessly with Laravel Collections and providing a host of useful methods. Whether you need to check, convert, or manipulate Enums, EnumConcern simplifies the process, saving time and effort.

For more detailed examples and comprehensive documentation, you can refer to the official EnumConcern documentation.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297