1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Transformer; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\EnumType; |
8
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
9
|
|
|
use GraphQL\Type\Definition\ListOfType; |
10
|
|
|
use GraphQL\Type\Definition\NonNull; |
11
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
12
|
|
|
use GraphQL\Type\Definition\Type; |
13
|
|
|
use Overblog\GraphQLBundle\Error\InvalidArgumentError; |
14
|
|
|
use Overblog\GraphQLBundle\Error\InvalidArgumentsError; |
15
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
16
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
17
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
18
|
|
|
|
19
|
|
|
class ArgumentsTransformer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ValidatorInterface |
23
|
|
|
*/ |
24
|
|
|
protected $validator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $classesMap; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var PropertyAccessor |
33
|
|
|
*/ |
34
|
|
|
protected $accessor; |
35
|
|
|
|
36
|
3 |
|
public function __construct(ValidatorInterface $validator = null, $classesMap = []) |
37
|
|
|
{ |
38
|
3 |
|
$this->validator = $validator; |
39
|
3 |
|
$this->accessor = PropertyAccess::createPropertyAccessor(); |
40
|
3 |
|
$this->classesMap = $classesMap; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the PHP class for a given type. |
45
|
|
|
* |
46
|
|
|
* @param string $type |
47
|
|
|
* |
48
|
|
|
* @return object|false |
49
|
|
|
*/ |
50
|
3 |
|
private function getTypeClassInstance(string $type) |
51
|
|
|
{ |
52
|
3 |
|
$classname = isset($this->classesMap[$type]) ? $this->classesMap[$type]['class'] : false; |
53
|
|
|
|
54
|
3 |
|
return $classname ? new $classname() : false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Extract given type from Resolve Info. |
59
|
|
|
* |
60
|
|
|
* @param string $type |
61
|
|
|
* @param ResolveInfo $info |
62
|
|
|
* |
63
|
|
|
* @return Type |
64
|
|
|
*/ |
65
|
3 |
|
private function getType(string $type, ResolveInfo $info): Type |
66
|
|
|
{ |
67
|
3 |
|
return $info->schema->getType($type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Populate an object based on type with given data. |
72
|
|
|
* |
73
|
|
|
* @param Type $type |
74
|
|
|
* @param mixed $data |
75
|
|
|
* @param bool $multiple |
76
|
|
|
* @param ResolveInfo $info |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
3 |
|
private function populateObject(Type $type, $data, bool $multiple, ResolveInfo $info) |
81
|
|
|
{ |
82
|
3 |
|
if (null === $data) { |
83
|
3 |
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
if ($type instanceof NonNull) { |
87
|
1 |
|
$type = $type->getWrappedType(); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
if ($multiple) { |
91
|
|
|
return \array_map(function ($data) use ($type, $info) { |
92
|
3 |
|
return $this->populateObject($type, $data, false, $info); |
93
|
3 |
|
}, $data); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
if ($type instanceof EnumType) { |
97
|
3 |
|
$instance = $this->getTypeClassInstance($type->name); |
98
|
3 |
|
if ($instance) { |
99
|
1 |
|
$this->accessor->setValue($instance, 'value', $data); |
100
|
|
|
|
101
|
1 |
|
return $instance; |
102
|
|
|
} else { |
103
|
3 |
|
return $data; |
104
|
|
|
} |
105
|
3 |
|
} elseif ($type instanceof InputObjectType) { |
106
|
3 |
|
$instance = $this->getTypeClassInstance($type->name); |
107
|
3 |
|
if (!$instance) { |
108
|
|
|
return $data; |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
$fields = $type->getFields(); |
112
|
|
|
|
113
|
3 |
|
foreach ($fields as $name => $field) { |
114
|
3 |
|
$fieldData = $this->accessor->getValue($data, \sprintf('[%s]', $name)); |
115
|
|
|
|
116
|
3 |
|
if ($field->getType() instanceof ListOfType) { |
117
|
3 |
|
$fieldValue = $this->populateObject($field->getType()->getWrappedType(), $fieldData, true, $info); |
118
|
|
|
} else { |
119
|
3 |
|
$fieldValue = $this->populateObject($field->getType(), $fieldData, false, $info); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
$this->accessor->setValue($instance, $name, $fieldValue); |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
return $instance; |
126
|
|
|
} else { |
127
|
3 |
|
return $data; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Given a GraphQL type and an array of data, populate corresponding object recursively |
133
|
|
|
* using annoted classes. |
134
|
|
|
* |
135
|
|
|
* @param string $argType |
136
|
|
|
* @param mixed $data |
137
|
|
|
* @param ResolveInfo $info |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
3 |
|
public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info, string $argName) |
142
|
|
|
{ |
143
|
3 |
|
$isRequired = '!' === $argType[\strlen($argType) - 1]; |
144
|
3 |
|
$isMultiple = '[' === $argType[0]; |
145
|
3 |
|
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0); |
146
|
3 |
|
$type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType)); |
147
|
|
|
|
148
|
3 |
|
$result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info); |
149
|
3 |
|
$errors = []; |
150
|
3 |
|
if ($this->validator) { |
151
|
3 |
|
if (\is_object($result)) { |
152
|
2 |
|
$errors = $this->validator->validate($result); |
153
|
|
|
} |
154
|
3 |
|
if (\is_array($result) && $isMultiple) { |
155
|
1 |
|
foreach ($result as $element) { |
156
|
1 |
|
if (\is_object($element)) { |
157
|
1 |
|
$tmpErrors = $this->validator->validate($element); |
158
|
1 |
|
if (\is_array($errors)) { |
159
|
1 |
|
$errors = $tmpErrors; |
160
|
|
|
} else { |
161
|
|
|
$errors->addAll($tmpErrors); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
3 |
|
if (\count($errors) > 0) { |
169
|
2 |
|
throw new InvalidArgumentError($argName, $errors); |
|
|
|
|
170
|
|
|
} else { |
171
|
1 |
|
return $result; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Transform a list of arguments into their corresponding php class and validate them. |
177
|
|
|
* |
178
|
|
|
* @param array $mapping |
179
|
|
|
* @param mixed $data |
180
|
|
|
* @param ResolveInfo $info |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
3 |
|
public function getArguments(array $mapping, $data, ResolveInfo $info) |
185
|
|
|
{ |
186
|
3 |
|
$args = []; |
187
|
3 |
|
$exceptions = []; |
188
|
|
|
|
189
|
3 |
|
foreach ($mapping as $name => $type) { |
190
|
|
|
try { |
191
|
3 |
|
$value = $this->getInstanceAndValidate($type, $data[$name], $info, $name); |
192
|
1 |
|
$args[] = $value; |
193
|
2 |
|
} catch (InvalidArgumentError $exception) { |
194
|
2 |
|
$exceptions[] = $exception; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
3 |
|
if (!empty($exceptions)) { |
199
|
2 |
|
throw new InvalidArgumentsError($exceptions); |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
return $args; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|