1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum\Services; |
6
|
|
|
|
7
|
|
|
use Cerbero\Enum\Services\Inspector as BaseInspector; |
8
|
|
|
use Cerbero\LaravelEnum\Capsules; |
9
|
|
|
use Cerbero\LaravelEnum\Concerns; |
10
|
|
|
use Cerbero\LaravelEnum\Data\MethodAnnotation; |
11
|
|
|
use UnitEnum; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The enum inspector. |
15
|
|
|
* |
16
|
|
|
* @template TEnum of UnitEnum |
17
|
|
|
* |
18
|
|
|
* @extends BaseInspector<TEnum> |
19
|
|
|
*/ |
20
|
|
|
final class Inspector extends BaseInspector |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The main trait to supercharge enums. |
24
|
|
|
* |
25
|
|
|
* @var class-string |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
protected string $mainTrait = Concerns\Enumerates::class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The capsules keyed by the related trait. |
31
|
|
|
* |
32
|
|
|
* @var array<class-string, class-string> |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
private array $capsules = [ |
35
|
|
|
Concerns\EnumeratesCacheKeys::class => Capsules\CacheKey::class, |
36
|
|
|
Concerns\EnumeratesSessionKeys::class => Capsules\SessionKey::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Determine whether the given namespace matches the enum namespace. |
41
|
|
|
*/ |
42
|
1 |
|
public function hasSameNamespace(string $namespace): bool |
43
|
|
|
{ |
44
|
1 |
|
return $this->reflection->getNamespaceName() . '\\' . class_basename($namespace) == $namespace; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve the method annotation for the given case. |
49
|
|
|
*/ |
50
|
1 |
|
public function caseAnnotation(UnitEnum $case): MethodAnnotation |
51
|
|
|
{ |
52
|
1 |
|
foreach ($this->capsules as $trait => $capsule) { |
53
|
1 |
|
if ($this->uses($trait)) { |
54
|
|
|
/** @var \BackedEnum $case */ |
55
|
1 |
|
return MethodAnnotation::forCapsule($case, $capsule); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return MethodAnnotation::forCase($case); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve the use statements. |
64
|
|
|
* |
65
|
|
|
* @return array<string, class-string> |
|
|
|
|
66
|
|
|
*/ |
67
|
1 |
|
public function useStatements(bool $includeExisting = true): array |
68
|
|
|
{ |
69
|
1 |
|
return $this->useStatements ??= [...new UseStatements($this, $includeExisting)]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve the method annotations. |
74
|
|
|
* |
75
|
|
|
* @return array<string, MethodAnnotation> |
76
|
|
|
*/ |
77
|
1 |
|
public function methodAnnotations(bool $includeExisting = true): array |
78
|
|
|
{ |
79
|
|
|
/** @var array<string, MethodAnnotation> */ |
80
|
1 |
|
return $this->methodAnnotations ??= [...new MethodAnnotations($this, $includeExisting)]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|