File tree Expand file tree Collapse file tree 3 files changed +110
-0
lines changed
Expand file tree Collapse file tree 3 files changed +110
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Helpers \Enums ;
4+
5+ interface IsEnum
6+ {
7+ public static function labels (): array ;
8+
9+ public function getLabel (): string ;
10+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Helpers \Enums ;
4+
5+
6+ use BackedEnum ;
7+
8+ trait NativeEnumsTrait
9+ {
10+ public function getLabel (): string
11+ {
12+ return self ::labels ()[$ this ->value ];
13+ }
14+
15+ public static function getLabelFromKey (string $ key ): string
16+ {
17+ return self ::labels ()[$ key ] ?? '' ;
18+ }
19+
20+ public static function getKeys (): array
21+ {
22+ return array_column (self ::cases (), 'value ' );
23+ }
24+
25+ public static function slugs (string |BackedEnum $ input )
26+ {
27+ if ($ input instanceof BackedEnum) {
28+ $ input = $ input ->value ;
29+ }
30+
31+ $ status_slugs = [];
32+ $ cases = self ::cases ();
33+
34+ foreach ($ cases as $ case ) {
35+ $ status_slugs [$ case ->value ] = $ case ->value ;
36+ }
37+
38+ return $ status_slugs [$ input ] ?? '' ;
39+ }
40+
41+ public static function names (string |BackedEnum $ input )
42+ {
43+ if ($ input instanceof BackedEnum) {
44+ $ input = $ input ->value ;
45+ }
46+
47+ $ status_names = [];
48+ $ cases = self ::cases ();
49+
50+ foreach ($ cases as $ case ) {
51+ $ status_names [$ case ->value ] = $ case ->getLabel ();
52+ }
53+
54+ return $ status_names [$ input ] ?? '' ;
55+ }
56+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Helpers \Enums ;
4+
5+
6+ enum UserStatuses: string implements IsEnum
7+ {
8+ use NativeEnumsTrait;
9+
10+ case APPROVED = 'approved ' ;
11+ case PENDING = 'pending ' ;
12+ case BANNED = 'banned ' ;
13+
14+ /**
15+ * Initialize Messages
16+ */
17+ public static function messages (): array
18+ {
19+ return [
20+ self ::APPROVED ->value => __ ('Your account is approved. ' ),
21+ self ::PENDING ->value => __ ('Your account needs to be approved before you can access it. ' ),
22+ self ::BANNED ->value => __ ('Your account has been banned. ' ),
23+ ];
24+ }
25+
26+ public function getMessage (): string
27+ {
28+ return self ::messages ()[$ this ->value ];
29+ }
30+
31+ public static function getMessageFromKey (string $ key ): string
32+ {
33+ return self ::messages ()[$ key ] ?? '' ;
34+ }
35+
36+ public static function labels (): array
37+ {
38+ return [
39+ self ::APPROVED ->value => __ ("Approved " ),
40+ self ::PENDING ->value => __ ("Pending " ),
41+ self ::BANNED ->value => __ ("Banned " ),
42+ ];
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments