Created
January 20, 2025 16:49
-
-
Save ricardov03/aa3493deb55191106cdd8d6454a0425e to your computer and use it in GitHub Desktop.
Example of a Enum definition that includes two methods to define Labels and Select Options.
This file contains hidden or 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 Exception; | |
| enum MonthsEnum: int | |
| { | |
| case JANUARY = 1; | |
| case FEBRUARY = 2; | |
| case MARCH = 3; | |
| case APRIL = 4; | |
| case MAY = 5; | |
| case JUNE = 6; | |
| case JULY = 7; | |
| case AUGUST = 8; | |
| case SEPTEMBER = 9; | |
| case OCTOBER = 10; | |
| case NOVEMBER = 11; | |
| case DECEMBER = 12; | |
| public static function toArray(): array | |
| { | |
| return array_column(WeekDaysEnum::cases(), 'value'); | |
| } | |
| public static function toLabel($value): string | |
| { | |
| return match ($value) { | |
| self::JANUARY->value => 'Enero', | |
| self::FEBRUARY->value => 'Febrero', | |
| self::MARCH->value => 'Marzo', | |
| self::APRIL->value => 'Abril', | |
| self::MAY->value => 'Mayo', | |
| self::JUNE->value => 'Junio', | |
| self::JULY->value => 'Julio', | |
| self::AUGUST->value => 'Agosto', | |
| self::SEPTEMBER->value => 'Septiembre', | |
| self::OCTOBER->value => 'Octubre', | |
| self::NOVEMBER->value => 'Noviembre', | |
| self::DECEMBER->value => 'Diciembre', | |
| }; | |
| } | |
| public static function toSelectArray(): array | |
| { | |
| return array_map(fn ($format) => [ | |
| 'label' => self::toLabel($format), | |
| 'value' => $format, | |
| ], self::toArray()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment