-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Muetze42/development
feat: add enum trait
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
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)]; | ||
}); | ||
} | ||
} |