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