File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments