Skip to content

Commit 3fab391

Browse files
committed
enum
1 parent 4ac8eb5 commit 3fab391

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

packages/utilities/src/Enum/EnumExtendedTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public static function tryWrap(mixed $value): ?static
4040
}
4141

4242
/**
43-
* @return array<string, static>
43+
* Return assoc array of cases with case name as key.
44+
*
45+
* @return array<string|int, static>
4446
*/
4547
public static function values(): array
4648
{

packages/utilities/src/Enum/EnumMetaInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Interface EnumMetaInterface
1111
*/
12-
interface EnumMetaInterface extends EnumAdapterInterface
12+
interface EnumMetaInterface
1313
{
1414
public function getTitle(?LanguageInterface $lang = null, ...$args): string;
1515

packages/utilities/src/Enum/EnumMetaTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public static function maxLength(): int
166166

167167
$max = 0;
168168

169-
foreach (self::values() as $value) {
170-
$max = max($max, strlen($value->value));
169+
foreach (self::cases() as $case) {
170+
$max = max($max, strlen($case->value));
171171
}
172172

173173
return $max;

packages/utilities/src/Enum/EnumRichTrait.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Windwalker\Utilities\Enum;
66

77
use Windwalker\Utilities\Contract\LanguageInterface;
8+
use Windwalker\Utilities\StrNormalize;
89

910
trait EnumRichTrait
1011
{
@@ -16,9 +17,22 @@ public static function getTransItems(LanguageInterface $lang, ...$args): array
1617
$items = [];
1718

1819
foreach (self::cases() as $item) {
19-
$items[$item->value] = $item->getTitle($lang, ...$args);
20+
$items[$item->value ?? $item->name] = $item->getTitle($lang, ...$args);
2021
}
2122

2223
return $items;
2324
}
25+
26+
public function trans(LanguageInterface $lang, ...$args): string
27+
{
28+
return $lang->trans($this->translateKey($this->name), ...$args);
29+
}
30+
31+
protected function translateKey(string $name): string
32+
{
33+
$ref = new \ReflectionEnum($this);
34+
$id = strtolower(StrNormalize::toDashSeparated($ref->getName()));
35+
36+
return 'app.enum.' . $id . '.' . $name;
37+
}
2438
}

0 commit comments

Comments
 (0)