1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | namespace Thunder\Platenum\Enum; |
||
4 | |||
5 | use Thunder\Platenum\Exception\PlatenumException; |
||
6 | |||
7 | /** |
||
8 | * @template TMember |
||
9 | * @template TValue |
||
10 | * |
||
11 | * @author Tomasz Kowalczyk <[email protected]> |
||
12 | 1 | */ |
|
13 | trait CallbackEnumTrait |
||
14 | { |
||
15 | /** @use EnumTrait<TMember,TValue> */ |
||
16 | use EnumTrait; |
||
17 | |||
18 | 2 | /** @var non-empty-array<class-string,callable():array<string,int|string>> */ |
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
19 | protected static $callbacks = []; |
||
20 | 2 | ||
21 | 1 | /** @param callable():array<string,int|string> $callback */ |
|
22 | final public static function initialize(callable $callback): void |
||
23 | { |
||
24 | 2 | if(array_key_exists(static::class, static::$callbacks)) { |
|
25 | 2 | throw PlatenumException::fromAlreadyInitializedCallback(static::class); |
|
26 | } |
||
27 | 3 | ||
28 | static::$callbacks[static::class] = $callback; |
||
29 | 3 | } |
|
30 | 3 | ||
31 | 2 | private static function resolve(): array |
|
32 | { |
||
33 | $class = static::class; |
||
34 | 1 | if(false === (array_key_exists($class, static::$callbacks) && is_callable(static::$callbacks[$class]))) { |
|
35 | throw PlatenumException::fromInvalidCallback($class); |
||
36 | } |
||
37 | |||
38 | return (static::$callbacks[$class])(); |
||
39 | } |
||
40 | } |
||
41 |