Created
January 22, 2024 14:07
-
-
Save bennettblack/81c3be89df6fc42eace80b60498a5de8 to your computer and use it in GitHub Desktop.
Arrayable 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\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