Skip to content

Commit a2fa6e4

Browse files
committed
Deprecated values() to wait php 8.6
1 parent cfcb179 commit a2fa6e4

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

packages/utilities/src/Enum/EnumExtendedInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ public static function tryWrap(mixed $value): ?static;
1515
*
1616
* @return array<string|int, static>
1717
*/
18-
public static function values(): array;
18+
// public static function values(): array;
19+
20+
/**
21+
* Return assoc array of cases with case name as key.
22+
*
23+
* @return array<string|int, static>
24+
*/
25+
public static function assocCases(): array;
1926

2027
public static function rawValues(): array;
2128

packages/utilities/src/Enum/EnumExtendedTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public static function tryWrap(mixed $value): ?static
4343
* Return assoc array of cases with case name as key.
4444
*
4545
* @return array<string|int, static>
46+
*
47+
* @deprecated Do not use this method since PHP 8.6 may override this method. Use `assocCases()` instead.
48+
* @see https://wiki.php.net/rfc/add_values_method_to_backed_enum
4649
*/
4750
public static function values(): array
4851
{
@@ -60,6 +63,20 @@ public static function rawValues(): array
6063
return array_map(static fn (self $case) => $case->value, self::values());
6164
}
6265

66+
/**
67+
* @return array<string|int, static>
68+
*/
69+
public static function assocCases(): array
70+
{
71+
$cases = [];
72+
73+
foreach (self::cases() as $case) {
74+
$cases[$case->name] = $case;
75+
}
76+
77+
return $cases;
78+
}
79+
6380
/**
6481
* Returns the names (keys) of all constants in the Enum class
6582
*

0 commit comments

Comments
 (0)