@@ -24,13 +24,27 @@ abstract class Enumeration extends AbstractEnumeration implements Castable
24
24
protected static $ localizationPath = null ;
25
25
26
26
/**
27
- * Get the localization path for this enum.
27
+ * Get the default enum member.
28
+ * Override for your own value / logic.
28
29
*
29
- * @return string
30
+ * @return static
30
31
*/
31
- protected static function getLocalizationPath (): string
32
+ public static function defaultMember ()
32
33
{
33
- return static ::$ localizationPath ?? sprintf ('enums.%s ' , static ::class);
34
+ return collect (static ::members ())
35
+ ->first ();
36
+ }
37
+
38
+ /**
39
+ * Get a random member of this enum.
40
+ *
41
+ * @param array<mixed>|null $blacklist
42
+ * @return static
43
+ */
44
+ public static function randomMember (?array $ blacklist = [])
45
+ {
46
+ return collect (self ::membersByBlacklist ($ blacklist ))
47
+ ->random ();
34
48
}
35
49
36
50
/**
@@ -61,7 +75,10 @@ public function is($value): bool
61
75
*/
62
76
public static function values (): array
63
77
{
64
- return collect (self ::members ())->map ->value ()->values ()->all ();
78
+ return collect (self ::members ())
79
+ ->map ->value ()
80
+ ->values ()
81
+ ->all ();
65
82
}
66
83
67
84
/**
@@ -71,7 +88,10 @@ public static function values(): array
71
88
*/
72
89
public static function localizedValues (): array
73
90
{
74
- return collect (self ::members ())->map ->localized ()->values ()->all ();
91
+ return collect (self ::members ())
92
+ ->map ->localized ()
93
+ ->values ()
94
+ ->all ();
75
95
}
76
96
77
97
/**
@@ -81,7 +101,10 @@ public static function localizedValues(): array
81
101
*/
82
102
public static function keys (): array
83
103
{
84
- return collect (self ::members ())->map ->key ()->values ()->all ();
104
+ return collect (self ::members ())
105
+ ->map ->key ()
106
+ ->values ()
107
+ ->all ();
85
108
}
86
109
87
110
/**
@@ -94,8 +117,9 @@ public static function toLocalizedSelectArray(?array $blacklist = []): array
94
117
{
95
118
return collect (self ::membersByBlacklist ($ blacklist ))
96
119
->mapWithKeys (static function (Enumeration $ item ): array {
97
- return [ $ item ->value () => $ item ->localized () ];
98
- })->all ();
120
+ return [$ item ->value () => $ item ->localized ()];
121
+ })
122
+ ->all ();
99
123
}
100
124
101
125
/**
@@ -108,8 +132,9 @@ public static function toSelectArray(?array $blacklist = []): array
108
132
{
109
133
return collect (self ::membersByBlacklist ($ blacklist ))
110
134
->mapWithKeys (static function (Enumeration $ item ): array {
111
- return [ $ item ->value () => $ item ->key () ];
112
- })->all ();
135
+ return [$ item ->value () => $ item ->key ()];
136
+ })
137
+ ->all ();
113
138
}
114
139
115
140
/**
@@ -120,20 +145,9 @@ public static function toSelectArray(?array $blacklist = []): array
120
145
*/
121
146
public static function membersByBlacklist (?array $ blacklist = []): array
122
147
{
123
- return collect ( self ::membersByPredicate (static function (Enumeration $ enumValue ) use ($ blacklist ): bool {
148
+ return self ::membersByPredicate (static function (Enumeration $ enumValue ) use ($ blacklist ): bool {
124
149
return !$ enumValue ->anyOfArray ($ blacklist );
125
- }))->all ();
126
- }
127
-
128
- /**
129
- * Get a random member of this enum.
130
- *
131
- * @param array<mixed>|null $blacklist
132
- * @return static
133
- */
134
- public static function randomMember (?array $ blacklist = [])
135
- {
136
- return collect (self ::membersByBlacklist ($ blacklist ))->random ();
150
+ });
137
151
}
138
152
139
153
/**
@@ -168,17 +182,6 @@ public static function makeRuleWithBlacklist(?array $blacklist = []): Enumeratio
168
182
return self ::makeRuleWithWhitelist (self ::membersByBlacklist ($ blacklist ));
169
183
}
170
184
171
- /**
172
- * Get the default enum member.
173
- * Override for your own value / logic.
174
- *
175
- * @return static
176
- */
177
- public static function defaultMember ()
178
- {
179
- return collect (static ::members ())->first ();
180
- }
181
-
182
185
/**
183
186
* Checks if this enum has a member with the given value.
184
187
*
@@ -187,7 +190,7 @@ public static function defaultMember()
187
190
*/
188
191
public static function hasValue ($ value ): bool
189
192
{
190
- return in_array ($ value , static ::values ());
193
+ return in_array ($ value , static ::values (), true );
191
194
}
192
195
193
196
/**
@@ -198,7 +201,26 @@ public static function hasValue($value): bool
198
201
*/
199
202
public static function hasKey (string $ key ): bool
200
203
{
201
- return in_array ($ key , static ::keys ());
204
+ return in_array ($ key , static ::keys (), true );
205
+ }
206
+
207
+ /**
208
+ * @param array<mixed> $arguments
209
+ * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes
210
+ */
211
+ public static function castUsing (array $ arguments ): CastsAttributes
212
+ {
213
+ return new Enum (static ::class, ...$ arguments );
214
+ }
215
+
216
+ /**
217
+ * Get the localization path for this enum.
218
+ *
219
+ * @return string
220
+ */
221
+ protected static function getLocalizationPath (): string
222
+ {
223
+ return static ::$ localizationPath ?? sprintf ('enums.%s ' , static ::class);
202
224
}
203
225
204
226
/**
@@ -227,9 +249,4 @@ public function __call($method, $arguments)
227
249
return $ this ->is (static ::memberByKey ($ key , false ));
228
250
}
229
251
}
230
-
231
- public static function castUsing (): CastsAttributes
232
- {
233
- return new Enum (static ::class);
234
- }
235
252
}
0 commit comments