1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum\Services; |
6
|
|
|
|
7
|
|
|
use Cerbero\LaravelEnum\Concerns\EnumeratesCacheKeys; |
8
|
|
|
use Cerbero\LaravelEnum\Data\MethodAnnotation; |
9
|
|
|
use ReflectionEnum; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The enum inspector. |
13
|
|
|
* |
14
|
|
|
* @template TEnum |
15
|
|
|
*/ |
16
|
|
|
final class Inspector |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The enum reflection. |
20
|
|
|
* |
21
|
|
|
* @var ReflectionEnum<\UnitEnum> |
22
|
|
|
*/ |
23
|
|
|
private readonly ReflectionEnum $reflection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The method annotations. |
27
|
|
|
* |
28
|
|
|
* @var array<string, MethodAnnotation> |
29
|
|
|
*/ |
30
|
|
|
private array $methodAnnotations; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The use statements. |
34
|
|
|
* |
35
|
|
|
* @var array<string, class-string> |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
private array $useStatements; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Instantiate the class. |
41
|
|
|
* |
42
|
|
|
* @param class-string<TEnum> $enum |
|
|
|
|
43
|
|
|
*/ |
44
|
2 |
|
public function __construct( |
45
|
|
|
private readonly string $enum, |
46
|
|
|
private readonly bool $force, |
47
|
|
|
) { |
48
|
|
|
/** @var class-string<\UnitEnum> $enum */ |
49
|
2 |
|
$this->reflection = new ReflectionEnum($enum); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retrieve the enum filename. |
54
|
|
|
*/ |
55
|
1 |
|
public function filename(): string |
56
|
|
|
{ |
57
|
1 |
|
return (string) $this->reflection->getFileName(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieve the enum namespace. |
62
|
|
|
*/ |
63
|
1 |
|
public function namespace(): string |
64
|
|
|
{ |
65
|
1 |
|
return $this->reflection->getNamespaceName(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Determine whether the given namespace matches the enum namespace. |
70
|
|
|
*/ |
71
|
1 |
|
public function hasSameNamespace(string $namespace): bool |
72
|
|
|
{ |
73
|
1 |
|
return $namespace == $this->namespace() . '\\' . class_basename($namespace); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieve the DocBlock of the enum. |
78
|
|
|
*/ |
79
|
1 |
|
public function docBlock(): string |
80
|
|
|
{ |
81
|
1 |
|
return $this->reflection->getDocComment() ?: ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieve the enum cases. |
86
|
|
|
* |
87
|
|
|
* @return list<TEnum> |
88
|
|
|
*/ |
89
|
1 |
|
public function cases(): array |
90
|
|
|
{ |
91
|
|
|
/** @var list<TEnum> */ |
92
|
1 |
|
return $this->enum::cases(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retrieve the meta attribute names of the enum. |
97
|
|
|
* |
98
|
|
|
* @return list<string> |
99
|
|
|
*/ |
100
|
1 |
|
public function metaAttributeNames(): array |
101
|
|
|
{ |
102
|
|
|
/** @var list<string> */ |
103
|
1 |
|
return $this->enum::metaAttributeNames(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Determine whether the enum enumerates cache keys. |
108
|
|
|
*/ |
109
|
1 |
|
public function enumeratesCacheKeys(): bool |
110
|
|
|
{ |
111
|
1 |
|
return $this->uses(EnumeratesCacheKeys::class); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Determine whether the enum uses the given trait. |
116
|
|
|
*/ |
117
|
2 |
|
public function uses(string $trait): bool |
118
|
|
|
{ |
119
|
2 |
|
return isset($this->traits()[$trait]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Retrieve all the enum traits. |
124
|
|
|
* |
125
|
|
|
* @return array<class-string, class-string> |
|
|
|
|
126
|
|
|
*/ |
127
|
2 |
|
public function traits(): array |
128
|
|
|
{ |
129
|
2 |
|
$traits = []; |
130
|
|
|
|
131
|
2 |
|
foreach ($this->reflection->getTraitNames() as $trait) { |
132
|
1 |
|
$traits += [$trait => $trait, ...trait_uses_recursive($trait)]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @var array<class-string, class-string> */ |
136
|
2 |
|
return $traits; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Retrieve the use statements. |
141
|
|
|
* |
142
|
|
|
* @return array<string, class-string> |
|
|
|
|
143
|
|
|
*/ |
144
|
1 |
|
public function useStatements(): array |
145
|
|
|
{ |
146
|
1 |
|
return $this->useStatements ??= [...new UseStatements($this)]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Retrieve the method annotations. |
151
|
|
|
* |
152
|
|
|
* @return array<string, MethodAnnotation> |
153
|
|
|
*/ |
154
|
1 |
|
public function methodAnnotations(): array |
155
|
|
|
{ |
156
|
1 |
|
return $this->methodAnnotations ??= [...new MethodAnnotations($this, $this->force)]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|