Skip to content

Instantly share code, notes, and snippets.

@kevindb
Last active September 17, 2020 00:35
Show Gist options
  • Save kevindb/0373fdd844a201ac9ce901d2ec3b0b3a to your computer and use it in GitHub Desktop.
Save kevindb/0373fdd844a201ac9ce901d2ec3b0b3a to your computer and use it in GitHub Desktop.
Enum wrapper for use with https://github.com/spatie/laravel-enum
<?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