Skip to content

lazerg/laravel-enum-pro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wallpaper

Version Downloads count Repository count Last commit Stars count

Finally, in php81 has been added support for Enums. But as enums are new in php, we do not have some helpers to work with that easily. So with our enum-pro package you have pretty more options for working with enums than built-in methods.

It is just trait Lazerg\LaravelEnumPro\EnumPro which must be added into enum. So it means it use built-in enum class while enhancing it

Installation

composer require lazerg/laravel-enum-pro

Usage

Create a new enum class and use our trait on it.

enum LevelTypes: int {
    use \Lazerg\LaravelEnumPro\EnumPro;

    case VERY_EASY = 1;
    case EASY = 2;
    case MEDIUM = 3;
    case STRONG = 4;
    case VERY_STRONG = 5
}

Calling

With default functions, if you want to get value of case you should write LevelTypes::VERY_EASY->value which is little long. With our package, you can get value of case, by just calling it statically

LevelTypes::VERY_EASY() // 1

Names

As you can see, names here VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG. To get all case names of enum. you can use these helper methods:

LevelTypes::names();         // Collection: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
LevelTypes::namesToArray();  // Array: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
LevelTypes::namesToString(); // String: VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG
LevelTypes::nameOf(1);       // String: VERY_EASY

Values

As you can see, values here 1, 2, 3, 4, 5. Common usage is for: validate incoming request data.

LevelTypes::values();         // Collection: [1, 2, 3, 4, 5]
LevelTypes::valuesToArray();  // Array: [1, 2, 3, 4, 5]
LevelTypes::valuesToString(); // String: 1, 2, 3, 4, 5
LevelTypes::valueOf('very easy'); // 1

Randomize

Sometimes we need to get random value or values from enum. This is mainly used in factories.

LevelTypes::random(int $count = 1);      // Collection of $count random values
LevelTypes::randomArray(int $count = 1); // Array of $count random values
LevelTypes::randomFirst();               // One random value

Options

While creating admin panel, we always change state of models. And basically, it is recommended to save all state types in enum. So in admin panel we need to get all options of enum for select. That's why we have options() method.

LevelTypes::options(); // Return collection of selectable options
LevelTypes::optionsToArray(); // Return array of selectable options

Example of options:

Illuminate\Support\Collection {#7777
    #items: array:5 [
        1 => "Very Easy"
        2 => "Easy"
        3 => "Medium"
        4 => "Strong"
        5 => "Very Strong"
    ]
}

Selections

Sometimes in admin panel, it is easier to give options as array of objects. For such needs we have also selections() method

LevelTypes::selections();
LevelTypes::selectionsToArray();

Example of selections:

Illuminate\Support\Collection {#7777
    #items: array:5 [
        0 => [
            "value"   => "1",
            "display" => "Very Easy",
        ],
        1 => [
            "value"   => "2",
            "display" => "Easy",
        ],
        2 => [
            "value"   => "3",
            "display" => "Medium",
        ],
        3 => [
            "value"   => "4",
            "display" => "Strong",
        ],
        4 => [
            "value"   => "5",
            "display" => "Very Strong",
        ],
    ]
}

Testing

To run test

./vendor/bin/pest

About

Simplified and enhanced version of php built-in enum.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages