Skip to content

Commit

Permalink
feat: add enum trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Muetze42 committed Nov 29, 2023
1 parent 8b22a94 commit 57ed44e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Traits/EnumTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace NormanHuth\HelpersLaravel\Traits;

use Illuminate\Support\Arr;

trait EnumTrait
{
/**
* Return name value array.
*
* @return array<string, string>
*/
public static function toOptionsArray(): array
{
return Arr::mapWithKeys(self::cases(), function (self $enum) {
return [$enum->name => $enum->value];
});
}

/**
* Return value label array.
*
* @return array<string, string>
*/
public static function toValueLabelArray(): array
{
return Arr::mapWithKeys(self::cases(), function (self $enum) {
$label = method_exists($enum, 'label') ? $enum->label() : $enum->value;

return [$enum->value => __($label)];
});
}
}

0 comments on commit 57ed44e

Please sign in to comment.