Skip to content

Commit f946141

Browse files
committed
traits: Add EnumTrait with common methods for enums
1 parent 1c3fc54 commit f946141

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

app/Traits/EnumTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use Illuminate\Support\Collection;
6+
7+
/**
8+
* Trait for enums with common methods.
9+
*/
10+
trait EnumTrait
11+
{
12+
public static function foo(): string
13+
{
14+
return __('foo');
15+
}
16+
17+
public static function toCollection(): Collection
18+
{
19+
/**
20+
* Convert enum cases to a collection with additional properties.
21+
*/
22+
return collect(self::cases())
23+
->map(fn ($case) => [
24+
'value' => $case->value,
25+
'name' => $case->name,
26+
'label' => method_exists($case, 'label') ? $case->label() : null,
27+
'description' => method_exists($case, 'description') ? $case->description() : null,
28+
'color' => method_exists($case, 'color') ? $case->color() : null,
29+
]);
30+
}
31+
}

0 commit comments

Comments
 (0)