1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\TypeResolver\Middleware; |
6
|
|
|
|
7
|
|
|
use Andi\GraphQL\Attribute; |
8
|
|
|
use Andi\GraphQL\Common\DefinitionAwareTrait; |
9
|
|
|
use Andi\GraphQL\TypeResolver\TypeResolverInterface; |
10
|
|
|
use GraphQL\Type\Definition as Webonyx; |
11
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated; |
12
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
13
|
|
|
use Spiral\Attributes\ReaderInterface; |
14
|
|
|
|
15
|
|
|
final class EnumTypeMiddleware implements MiddlewareInterface |
16
|
|
|
{ |
17
|
|
|
use DefinitionAwareTrait; |
|
|
|
|
18
|
|
|
|
19
|
|
|
public const PRIORITY = AttributedGraphQLTypeMiddleware::PRIORITY + 256; |
20
|
|
|
|
21
|
6 |
|
public function __construct( |
22
|
|
|
private readonly ReaderInterface $reader, |
23
|
|
|
) { |
24
|
6 |
|
} |
25
|
|
|
|
26
|
5 |
|
public function process(mixed $type, TypeResolverInterface $typeResolver): Webonyx\Type |
27
|
|
|
{ |
28
|
5 |
|
$enum = \is_string($type) && \enum_exists($type) |
|
|
|
|
29
|
1 |
|
? new \ReflectionEnum($type) |
|
|
|
|
30
|
4 |
|
: $type; |
31
|
|
|
|
32
|
5 |
|
if ($enum instanceof \ReflectionEnum) { |
33
|
4 |
|
return $this->buildEnumType($enum, $this->reader->firstClassMetadata($enum, Attribute\EnumType::class)); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
return $typeResolver->resolve($type); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
private function buildEnumType(\ReflectionEnum $class, ?Attribute\EnumType $attribute): Webonyx\EnumType |
40
|
|
|
{ |
41
|
4 |
|
$config = [ |
42
|
4 |
|
'name' => $this->getTypeName($class, $attribute), |
43
|
4 |
|
'description' => $this->getTypeDescription($class, $attribute), |
44
|
4 |
|
'values' => [], |
45
|
4 |
|
]; |
46
|
|
|
|
47
|
4 |
|
foreach ($class->getCases() as $case) { |
48
|
4 |
|
$caseAttribute = $this->reader->firstConstantMetadata($case, Attribute\EnumValue::class); |
49
|
|
|
|
50
|
4 |
|
$config['values'][$this->getCaseName($case, $caseAttribute)] = [ |
51
|
4 |
|
'value' => $case->getValue(), |
52
|
4 |
|
'description' => $this->getCaseDescription($case, $caseAttribute), |
53
|
4 |
|
'deprecationReason' => $this->getCaseDeprecationReason($case, $caseAttribute), |
54
|
4 |
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
return new Webonyx\EnumType($config); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
private function getCaseName(\ReflectionEnumUnitCase $case, ?Attribute\EnumValue $attribute): string |
|
|
|
|
61
|
|
|
{ |
62
|
4 |
|
if ($attribute?->name) { |
63
|
1 |
|
\assert($attribute->name !== null); |
64
|
1 |
|
return $attribute->name; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
return $case->getName(); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
private function getCaseDescription(\ReflectionEnumUnitCase $case, ?Attribute\EnumValue $attribute): ?string |
71
|
|
|
{ |
72
|
4 |
|
if ($attribute?->description) { |
73
|
1 |
|
return $attribute->description; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
if ($docComment = $case->getDocComment()) { |
77
|
1 |
|
return DocBlockFactory::createInstance()->create($docComment)->getSummary() ?: null; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
private function getCaseDeprecationReason(\ReflectionEnumUnitCase $case, ?Attribute\EnumValue $attribute): ?string |
84
|
|
|
{ |
85
|
4 |
|
if ($attribute?->deprecationReason) { |
86
|
1 |
|
return $attribute->deprecationReason; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
if ($docComment = $case->getDocComment()) { |
90
|
1 |
|
$docBlock = DocBlockFactory::createInstance()->create($docComment); |
91
|
1 |
|
foreach ($docBlock->getTags() as $tag) { |
92
|
1 |
|
if ($tag instanceof Deprecated) { |
93
|
1 |
|
return (string) $tag->getDescription() ?: null; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return null; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|