|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cerbero\Enum; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The global behavior for all enums. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Enums |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The logic to run when an inaccessible enum method is called. |
|
16
|
|
|
* |
|
17
|
|
|
* @var ?Closure(class-string $enum, string $name, array<array-key, mixed> $arguments): mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static ?Closure $onStaticCall = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The logic to run when an inaccessible case method is called. |
|
23
|
|
|
* |
|
24
|
|
|
* @var ?Closure(object $case, string $name, array<array-key, mixed> $arguments): mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static ?Closure $onCall = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The logic to run when a case is invoked. |
|
30
|
|
|
* |
|
31
|
|
|
* @var ?Closure(object $case, mixed ...$arguments): mixed |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static ?Closure $onInvoke = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set the logic to run when an inaccessible enum method is called. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Closure(class-string $enum, string $name, array<array-key, mixed> $arguments): mixed $callback |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public static function onStaticCall(Closure $callback): void |
|
41
|
|
|
{ |
|
42
|
2 |
|
static::$onStaticCall = $callback; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set the logic to run when an inaccessible case method is called. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Closure(object $case, string $name, array<array-key, mixed> $arguments): mixed $callback |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public static function onCall(Closure $callback): void |
|
51
|
|
|
{ |
|
52
|
2 |
|
static::$onCall = $callback; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the logic to run when a case is invoked. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Closure(object $case, mixed ...$arguments): mixed $callback |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public static function onInvoke(Closure $callback): void |
|
61
|
|
|
{ |
|
62
|
2 |
|
static::$onInvoke = $callback; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Handle the call to an inaccessible enum method. |
|
67
|
|
|
* |
|
68
|
|
|
* @param class-string $enum |
|
|
|
|
|
|
69
|
|
|
* @param array<array-key, mixed> $arguments |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public static function handleStaticCall(string $enum, string $name, array $arguments): mixed |
|
72
|
|
|
{ |
|
73
|
4 |
|
return static::$onStaticCall |
|
74
|
2 |
|
? (static::$onStaticCall)($enum, $name, $arguments) |
|
75
|
4 |
|
: $enum::fromName($name)->value(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Handle the call to an inaccessible case method. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array<array-key, mixed> $arguments |
|
|
|
|
|
|
82
|
|
|
*/ |
|
83
|
14 |
|
public static function handleCall(object $case, string $name, array $arguments): mixed |
|
84
|
|
|
{ |
|
85
|
|
|
/** @phpstan-ignore method.notFound */ |
|
86
|
14 |
|
return static::$onCall ? (static::$onCall)($case, $name, $arguments) : $case->resolveMetaAttribute($name); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Handle the invocation of a case. |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public static function handleInvoke(object $case, mixed ...$arguments): mixed |
|
93
|
|
|
{ |
|
94
|
|
|
/** @phpstan-ignore method.notFound */ |
|
95
|
2 |
|
return static::$onInvoke ? (static::$onInvoke)($case, ...$arguments) : $case->value(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|