Last active
September 17, 2020 00:35
-
-
Save kevindb/0373fdd844a201ac9ce901d2ec3b0b3a to your computer and use it in GitHub Desktop.
Enum wrapper for use with https://github.com/spatie/laravel-enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Enums; | |
use Illuminate\Support\Arr; | |
use Spatie\Enum\Laravel\Enum as BaseEnum; | |
class Enum extends BaseEnum | |
{ | |
public static function getValues(): array | |
{ | |
return array_values(static::values()); | |
} | |
public static function getNames(): array | |
{ | |
return array_keys(static::values()); | |
} | |
public static function getLabels(): array | |
{ | |
return array_values(static::labels()); | |
} | |
/** | |
* Gets array of names and labels to be used in select dropdowns in Backpack | |
* | |
* @return array | |
*/ | |
public static function toArrayForBackpack(): array | |
{ | |
return array_combine(static::getNames(), static::labels()); | |
} | |
/** | |
* Returns random instance of called Enum | |
* | |
* @return static | |
*/ | |
public static function getRandom() | |
{ | |
return new static(Arr::random(static::getValues())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment