Skip to content

Instantly share code, notes, and snippets.

@bennettblack
Created January 22, 2024 14:07
Show Gist options
  • Save bennettblack/81c3be89df6fc42eace80b60498a5de8 to your computer and use it in GitHub Desktop.
Save bennettblack/81c3be89df6fc42eace80b60498a5de8 to your computer and use it in GitHub Desktop.
Arrayable Enum
<?php
namespace App\Concerns;
trait ArrayableEnum
{
/**
* Array represntation of a backed enum, keyed by values.
*/
public static function toArray(): array
{
return array_combine(self::values(), self::names());
}
/**
* Return all values of a backed enum.
*/
public static function values(): array
{
return array_column(self::cases(), 'value');
}
/**
* Return all names of a backed enum.
*/
public static function names(): array
{
return array_column(self::cases(), 'name');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment