1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation\FieldDecorator; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Annotation; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationReaderAwareTrait; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Decorate a field definition using common GraphQL annotations |
21
|
|
|
*/ |
22
|
|
|
class GraphQLFieldDefinitionDecorator implements FieldDefinitionDecoratorInterface |
23
|
|
|
{ |
24
|
|
|
use AnnotationReaderAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
1 |
|
public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition) |
30
|
|
|
{ |
31
|
1 |
|
if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) { |
32
|
|
|
throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method'); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
if (($name = $this->resolveFieldName($field)) && null !== $name) { |
36
|
1 |
|
$definition->setName($name); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
if (($type = $this->resolveFieldType($field)) && null !== $type) { |
40
|
1 |
|
$definition->setType($this->resolveFieldType($field)); |
41
|
1 |
|
$definition->setList($this->resolveFieldIsList($field)); |
42
|
1 |
|
$definition->setNonNull($this->resolveFieldNonNull($field)); |
43
|
1 |
|
$definition->setNonNullList($this->resolveFieldNonNullList($field)); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
if (($description = $this->resolveFieldDescription($field)) && null !== $description) { |
47
|
1 |
|
$definition->setDescription($description); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (($deprecationReason = $this->resolveFieldDeprecationReason($field)) && null !== $deprecationReason) { |
51
|
|
|
$definition->setDeprecationReason($deprecationReason); |
52
|
|
|
} |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get field specific annotation matching given objectDefinition |
57
|
|
|
* |
58
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
59
|
|
|
* @param string $annotationClass |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
1 |
|
protected function getFieldAnnotation($prop, string $annotationClass) |
64
|
|
|
{ |
65
|
1 |
|
if ($prop instanceof \ReflectionProperty) { |
66
|
1 |
|
return $this->reader->getPropertyAnnotation($prop, $annotationClass); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $this->reader->getMethodAnnotation($prop, $annotationClass); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
74
|
|
|
* |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
1 |
|
protected function resolveFieldDescription($prop): ?string |
78
|
|
|
{ |
79
|
|
|
/** @var Annotation\Field $annotation */ |
80
|
1 |
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
81
|
1 |
|
return $annotation->description; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
89
|
|
|
* |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
1 |
|
protected function resolveFieldDeprecationReason($prop): ?string |
93
|
|
|
{ |
94
|
|
|
/** @var Annotation\Field $annotation */ |
95
|
1 |
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
96
|
1 |
|
return $annotation->deprecationReason; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
104
|
|
|
* |
105
|
|
|
* @return bool|null |
106
|
|
|
*/ |
107
|
1 |
|
protected function resolveFieldNonNull($prop): ?bool |
108
|
|
|
{ |
109
|
|
|
/** @var Annotation\Field $annotationField */ |
110
|
1 |
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
111
|
1 |
|
if ($annotationField && $annotationField->type) { |
112
|
1 |
|
return TypeUtil::isTypeNonNull($annotationField->type); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
120
|
|
|
* |
121
|
|
|
* @return bool|null |
122
|
|
|
*/ |
123
|
1 |
|
protected function resolveFieldNonNullList($prop): ?bool |
124
|
|
|
{ |
125
|
|
|
/** @var Annotation\Field $annotationField */ |
126
|
1 |
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
127
|
1 |
|
if ($annotationField && $annotationField->type) { |
128
|
1 |
|
return TypeUtil::isTypeNonNullList($annotationField->type); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
136
|
|
|
* |
137
|
|
|
* @return bool|null |
138
|
|
|
*/ |
139
|
1 |
|
protected function resolveFieldIsList($prop): ?bool |
140
|
|
|
{ |
141
|
|
|
/** @var Annotation\Field $annotationField */ |
142
|
1 |
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
143
|
1 |
|
if ($annotationField && $annotationField->type) { |
144
|
1 |
|
return TypeUtil::isTypeList($annotationField->type); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
152
|
|
|
* |
153
|
|
|
* @return string|null |
154
|
|
|
*/ |
155
|
1 |
|
protected function resolveFieldType($prop): ?string |
156
|
|
|
{ |
157
|
1 |
|
$type = null; |
158
|
|
|
|
159
|
|
|
/** @var Annotation\Field $annotationField */ |
160
|
1 |
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
161
|
1 |
|
if ($annotationField && $annotationField->type) { |
162
|
1 |
|
$type = TypeUtil::normalize($annotationField->type); |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
return $type; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
170
|
|
|
* |
171
|
|
|
* @return string|null |
172
|
|
|
*/ |
173
|
1 |
|
protected function resolveFieldName($prop): ?string |
174
|
|
|
{ |
175
|
|
|
/** @var Annotation\Field $annotation */ |
176
|
1 |
|
if (($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) && $annotation->name) { |
177
|
1 |
|
return $annotation->name; |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
if ($prop instanceof \ReflectionMethod) { |
181
|
1 |
|
return lcfirst(preg_replace('/^(get|set)/', null, $prop->name)); |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
return $prop->name; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|