lazerg / laravel-enum-pro
Laravel Enum Pro
Installs: 16 661
Dependents: 0
Suggesters: 0
Security: 0
Stars: 42
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^8.1|^8.2|^8.3|^8.4
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- pestphp/pest: ^1.22|^2.0|^3.0
README
Laravel Enum Pro
is a simple trait that extends PHP 8.1+ enums with helpful utilities for Laravel applications. It lets you access enum data in a variety of convenient ways while keeping your code clean and expressive.
Features
- Works directly with native PHP enums
- Access case values via static method calls
- Retrieve enum names and values as collections, arrays or strings
- Generate random values for testing and factories
- Build option and selection lists for form inputs
Installation
composer require lazerg/laravel-enum-pro
Basic Usage
Create an enum and include the trait:
enum LevelTypes: int { use \Lazerg\LaravelEnumPro\EnumPro; case VERY_EASY = 1; case EASY = 2; case MEDIUM = 3; case STRONG = 4; case VERY_STRONG = 5; }
Accessing Values
LevelTypes::VERY_EASY(); // 1 LevelTypes::valueOf('very easy'); // 1
Working With Names
LevelTypes::names(); // Collection: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG'] LevelTypes::namesToArray(); // ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG'] LevelTypes::namesToString(); // "VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG" LevelTypes::nameOf(1); // 'VERY_EASY'
Working With Values
LevelTypes::values(); // Collection: [1, 2, 3, 4, 5] LevelTypes::valuesToArray(); // [1, 2, 3, 4, 5] LevelTypes::valuesToString(); // "1,2,3,4,5"
Randomization
LevelTypes::random(); // Collection with one random value LevelTypes::randomArray(); // Array with one random value LevelTypes::randomFirst(); // Single random value
Options and Selections
Use these helpers when building form inputs.
LevelTypes::options(); // Collection of [value => display] LevelTypes::optionsToArray(); LevelTypes::selections(); // Collection of [value => ..., display => ...] LevelTypes::selectionsToArray();
Example output of options()
:
Illuminate\Support\Collection { #items: [ 1 => "Very Easy", 2 => "Easy", 3 => "Medium", 4 => "Strong", 5 => "Very Strong", ] }
Testing
Run the test suite with Pest:
./vendor/bin/pest
License
This package is open-sourced software licensed under the MIT license as specified in composer.json
.