5
5
use BackedEnum ;
6
6
use EncoreDigitalGroup \StdLib \Objects \Support \Types \Str ;
7
7
use LogicException ;
8
+ use RuntimeException ;
8
9
9
10
/**
10
11
* Trait for PHP Enums that provides additional functionality.
@@ -55,8 +56,16 @@ public static function from(int|string $value): ?static
55
56
return null ;
56
57
}
57
58
58
- public static function options (): array
59
+ public static function options (array $ include = [], array $ exclude = [] ): array
59
60
{
61
+ if ($ include != []) {
62
+ return self ::optionsIncluding ($ include );
63
+ }
64
+
65
+ if ($ exclude != []) {
66
+ return self ::optionsExcluding ($ exclude );
67
+ }
68
+
60
69
$ options = [];
61
70
62
71
foreach (self ::cases () as $ case ) {
@@ -68,7 +77,13 @@ public static function options(): array
68
77
69
78
public static function name (self $ self ): string
70
79
{
71
- return $ self ->name ;
80
+ $ name = Str::formattedTitleCase ($ self ->value );
81
+
82
+ if (is_array ($ name )) {
83
+ throw new RuntimeException ("Name was provided as a string but returned as an array. " );
84
+ }
85
+
86
+ return $ name ;
72
87
}
73
88
74
89
private static function enforceEnum (): void
@@ -80,4 +95,30 @@ private static function enforceEnum(): void
80
95
));
81
96
}
82
97
}
98
+
99
+ private static function optionsIncluding (array $ include ): array
100
+ {
101
+ $ options = [];
102
+
103
+ foreach (self ::cases () as $ case ) {
104
+ if (in_array ($ case , $ include )) {
105
+ $ options [$ case ->value ] = self ::name ($ case );
106
+ }
107
+ }
108
+
109
+ return $ options ;
110
+ }
111
+
112
+ private static function optionsExcluding (array $ exclude ): array
113
+ {
114
+ $ options = [];
115
+
116
+ foreach (self ::cases () as $ case ) {
117
+ if (!in_array ($ case , $ exclude )) {
118
+ $ options [$ case ->value ] = self ::name ($ case );
119
+ }
120
+ }
121
+
122
+ return $ options ;
123
+ }
83
124
}
0 commit comments