├── README.MD ├── composer.json └── src ├── EnumNames.php ├── EnumOptions.php ├── EnumPro.php ├── EnumRandom.php ├── EnumStaticCalls.php └── EnumValues.php /README.MD: -------------------------------------------------------------------------------- 1 | ![wallpaper](./wallpaper/wallpaper.png) 2 | 3 | ![Version](https://img.shields.io/packagist/v/lazerg/laravel-enum-pro.svg?style=plastic) 4 | ![Downloads count](https://img.shields.io/packagist/dm/lazerg/laravel-enum-pro?style=plastic) 5 | ![Repository count](https://img.shields.io/github/repo-size/lazerg/laravel-enum-pro?style=plastic) 6 | ![Last commit](https://img.shields.io/github/last-commit/lazerg/laravel-enum-pro?style=plastic) 7 | ![Stars count](https://img.shields.io/packagist/stars/lazerg/laravel-enum-pro?style=plastic) 8 | 9 | Finally, in [`php81`](https://php.watch/versions/8.1) has been added support 10 | for [Enums](https://www.wikiwand.com/en/Enumerated_type). But as enums are new in php, we do not have some helpers to work with 11 | that easily. So with our enum-pro package you have pretty more options for working with enums than built-in methods. 12 | 13 | It is just trait `Lazerg\LaravelEnumPro\EnumPro` which must be added into enum. So it means it use built-in enum class 14 | while enhancing it 15 | 16 | ### Installation 17 | 18 | ```bash 19 | composer require lazerg/laravel-enum-pro 20 | ``` 21 | 22 | ### Usage 23 | 24 | Create a new enum class and use our trait on it. 25 | 26 | ```php 27 | enum LevelTypes: int { 28 | use \Lazerg\LaravelEnumPro\EnumPro; 29 | 30 | case VERY_EASY = 1; 31 | case EASY = 2; 32 | case MEDIUM = 3; 33 | case STRONG = 4; 34 | case VERY_STRONG = 5 35 | } 36 | ``` 37 | 38 | ### Calling 39 | With default functions, if you want to get value of case you should write `LevelTypes::VERY_EASY->value` which is little long. 40 | With our package, you can get value of case, by just calling it statically 41 | ```php 42 | LevelTypes::VERY_EASY() // 1 43 | ``` 44 | 45 | 46 | ### Names 47 | As you can see, names here `VERY_EASY`, `EASY`, `MEDIUM`, `STRONG`, `VERY_STRONG`. 48 | To get all case names of enum. you can use these helper methods: 49 | 50 | ```php 51 | LevelTypes::names(); // Collection: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG'] 52 | LevelTypes::namesToArray(); // Array: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG'] 53 | LevelTypes::namesToString(); // String: VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG 54 | LevelTypes::nameOf(1); // String: VERY_EASY 55 | ``` 56 | 57 | ### Values 58 | As you can see, values here `1`, `2`, `3`, `4`, `5`. 59 | **Common usage is for:** _validate incoming request data._ 60 | 61 | ```php 62 | LevelTypes::values(); // Collection: [1, 2, 3, 4, 5] 63 | LevelTypes::valuesToArray(); // Array: [1, 2, 3, 4, 5] 64 | LevelTypes::valuesToString(); // String: 1, 2, 3, 4, 5 65 | LevelTypes::valueOf('very easy'); // 1 66 | ``` 67 | 68 | ### Randomize 69 | Sometimes we need to get random value or values from enum. 70 | This is mainly used in factories. 71 | 72 | ```php 73 | LevelTypes::random(int $count = 1); // Collection of $count random values 74 | LevelTypes::randomArray(int $count = 1); // Array of $count random values 75 | LevelTypes::randomFirst(); // One random value 76 | ``` 77 | 78 | ### Options 79 | While creating admin panel, we always change state of models. 80 | And basically, it is recommended to save all state types in enum. 81 | So in admin panel we need to get all options of enum for select. 82 | That's why we have `options()` method. 83 | 84 | ```php 85 | LevelTypes::options(); // Return collection of selectable options 86 | LevelTypes::optionsToArray(); // Return array of selectable options 87 | ``` 88 | 89 | **Example of options:** 90 | 91 | ```bash 92 | Illuminate\Support\Collection {#7777 93 | #items: array:5 [ 94 | 1 => "Very Easy" 95 | 2 => "Easy" 96 | 3 => "Medium" 97 | 4 => "Strong" 98 | 5 => "Very Strong" 99 | ] 100 | } 101 | ``` 102 | 103 | #### Selections 104 | Sometimes in admin panel, it is easier to give options as array of objects. 105 | For such needs we have also `selections()` method 106 | 107 | ```php 108 | LevelTypes::selections(); 109 | LevelTypes::selectionsToArray(); 110 | ``` 111 | 112 | **Example of selections:** 113 | 114 | ```bash 115 | Illuminate\Support\Collection {#7777 116 | #items: array:5 [ 117 | 0 => [ 118 | "value" => "1", 119 | "display" => "Very Easy", 120 | ], 121 | 1 => [ 122 | "value" => "2", 123 | "display" => "Easy", 124 | ], 125 | 2 => [ 126 | "value" => "3", 127 | "display" => "Medium", 128 | ], 129 | 3 => [ 130 | "value" => "4", 131 | "display" => "Strong", 132 | ], 133 | 4 => [ 134 | "value" => "5", 135 | "display" => "Very Strong", 136 | ], 137 | ] 138 | } 139 | ``` 140 | 141 | ### Testing 142 | To run test 143 | ```bash 144 | ./vendor/bin/pest 145 | ``` 146 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lazerg/laravel-enum-pro", 3 | "description": "Laravel Enum Pro", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "lazerg", 9 | "email": "lazerg2@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "require": { 14 | "php": "^8.1|^8.2|^8.3|^8.4", 15 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Lazerg\\LaravelEnumPro\\": "src/" 20 | } 21 | }, 22 | "require-dev": { 23 | "pestphp/pest": "^1.22|^2.0|^3.0" 24 | }, 25 | "config": { 26 | "allow-plugins": { 27 | "pestphp/pest-plugin": true 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/EnumNames.php: -------------------------------------------------------------------------------- 1 | map(fn($case) => $case->name); 18 | } 19 | 20 | /** 21 | * Return all names of enum as string separated by comma 22 | * 23 | * @return string 24 | */ 25 | public static function namesToString(): string 26 | { 27 | return self::names()->join(', '); 28 | } 29 | 30 | /** 31 | * Return all names of enum as array 32 | * 33 | * @return array 34 | */ 35 | public static function namesToArray(): array 36 | { 37 | return self::names()->toArray(); 38 | } 39 | 40 | public static function nameOf(mixed $case): string 41 | { 42 | return array_column(self::cases(), 'name', 'value')[$case]; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/EnumOptions.php: -------------------------------------------------------------------------------- 1 | "Rental Truck" 22 | * 2 => "Container" 23 | * 3 => "Freight Trailer" 24 | * ] 25 | * 26 | * @return Collection 27 | */ 28 | public static function options(): Collection 29 | { 30 | return collect(self::cases())->mapWithKeys(fn(UnitEnum $enum) => [ 31 | $enum->value => Str::of($enum->name) 32 | ->replace('_', ' ') 33 | ->title() 34 | ->value() 35 | ]); 36 | } 37 | 38 | /** 39 | * Get a value of an option 40 | * 41 | * @param string $value 42 | * @return string 43 | */ 44 | public static function getOption(string $value): string 45 | { 46 | return self::options()[$value]; 47 | } 48 | 49 | /** 50 | * Get values of options 51 | * 52 | * @param array $values 53 | * @return Collection 54 | */ 55 | public static function getOptions(array $values): Collection 56 | { 57 | return self::options() 58 | ->filter(fn($value, $key) => in_array($key, $values)) 59 | ->values(); 60 | } 61 | 62 | /** 63 | * Convert cases of enum to collection of selections 64 | * 65 | * @input 66 | * case RENTAL_TRUCK = 1; 67 | * case CONTAINER = 2; 68 | * case FREIGHT_TRAILER = 3; 69 | * 70 | * @output 71 | * [ 72 | * 0 => ['value' => 1, 'display' => 'Rental Truck'], 73 | * 1 => ['value' => 2, 'display' => 'Container'], 74 | * 3 => ['value' => 3, 'display' => 'Freight Trailer'] 75 | * ] 76 | * 77 | * @return Collection 78 | */ 79 | public static function selections(): Collection 80 | { 81 | return self::options() 82 | ->map(fn($display, $value) => compact('value', 'display')) 83 | ->values(); 84 | } 85 | 86 | /** 87 | * Convert cases of enum to array of options 88 | * 89 | * @return array 90 | */ 91 | public static function optionsToArray(): array 92 | { 93 | return self::options()->toArray(); 94 | } 95 | 96 | /** 97 | * Convert cases of enum to array of selections 98 | * 99 | * @return array 100 | */ 101 | public static function selectionsToArray(): array 102 | { 103 | return self::selections()->toArray(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/EnumPro.php: -------------------------------------------------------------------------------- 1 | self::values()->count()) { 19 | throw new InvalidArgumentException('Count of random values is greater than count of enum values'); 20 | } 21 | 22 | return self::values()->shuffle()->take($count); 23 | } 24 | 25 | /** 26 | * Get one random value 27 | * 28 | * @return int|string 29 | */ 30 | public static function randomFirst(): int|string 31 | { 32 | return self::random()->first(); 33 | } 34 | 35 | /** 36 | * Get $count random values in array 37 | * 38 | * @param int $count 39 | * @return array 40 | */ 41 | public static function randomArray(int $count = 1): array 42 | { 43 | return self::random($count)->toArray(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/EnumStaticCalls.php: -------------------------------------------------------------------------------- 1 | value ?? $this->name; 17 | } 18 | 19 | /** 20 | * @throws Exception 21 | */ 22 | public static function __callStatic(string $name, array $arguments): int|string 23 | { 24 | foreach (self::cases() as $case) { 25 | if ($case->name === $name) { 26 | return $case->value; 27 | } 28 | } 29 | 30 | throw new Exception("Case with name $name does not exist"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/EnumValues.php: -------------------------------------------------------------------------------- 1 | map(fn($case) => $case->value ?? $case->name); 19 | } 20 | 21 | /** 22 | * Return all values of enum as string separated by comma 23 | * 24 | * @return string 25 | */ 26 | public static function valuesToString(): string 27 | { 28 | return self::values()->join(','); 29 | } 30 | 31 | /** 32 | * Return all values of enum as array 33 | * 34 | * @return array 35 | */ 36 | public static function valuesToArray(): array 37 | { 38 | return self::values()->toArray(); 39 | } 40 | 41 | /** 42 | * Return value of enum by name 43 | * 44 | * @param string $name 45 | * @return int|string|null 46 | */ 47 | public static function valueOf(string $name): null|int|string 48 | { 49 | $name = Str::replace(' ', '_', Str::upper($name)); 50 | 51 | foreach (self::cases() as $case) { 52 | if ($case->name === $name) { 53 | return $case->value; 54 | } 55 | } 56 | 57 | return null; 58 | } 59 | } 60 | --------------------------------------------------------------------------------