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\Mutation; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
14
|
|
|
use Symfony\Component\Form\FormInterface; |
15
|
|
|
use Symfony\Component\Validator\ConstraintViolation as SymfonyConstraintViolation; |
16
|
|
|
use Ynlo\GraphQLBundle\Form\DataTransformer\DataWithIdToNodeTransformer; |
17
|
|
|
use Ynlo\GraphQLBundle\Model\ConstraintViolation; |
18
|
|
|
use Ynlo\GraphQLBundle\Resolver\AbstractResolver; |
19
|
|
|
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base class for mutations |
23
|
|
|
* Implement the method "process()" and "returnPayload()" is enough in many scenarios |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractMutationAbstractResolver extends AbstractResolver |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param array $input |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
7 |
|
public function __invoke($input) |
33
|
|
|
{ |
34
|
7 |
|
$form = $this->createDefinitionForm($input)->getForm(); |
35
|
|
|
|
36
|
7 |
|
$this->preValidate($input); |
37
|
7 |
|
$form->submit($input, false); |
38
|
|
|
|
39
|
7 |
|
$data = $form->getData(); |
40
|
7 |
|
$this->onSubmit($input, $data); |
41
|
|
|
|
42
|
7 |
|
$violations = new ConstraintViolationList(); |
43
|
7 |
|
$this->extractFormErrors($form, $violations); |
44
|
7 |
|
$this->postValidation($data, $violations); |
45
|
|
|
|
46
|
7 |
|
$dryRun = $input['dryRun'] ?? false; |
47
|
|
|
|
48
|
7 |
|
if ($dryRun) { |
49
|
2 |
|
$data = null; |
50
|
|
|
} else { |
51
|
5 |
|
if ($form->isSubmitted() && $form->isValid() && !$violations->count()) { |
52
|
5 |
|
$this->process($data); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
7 |
|
return $this->returnPayload($data, $violations, $input); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Actions to process |
61
|
|
|
* the result processed data is given to payload |
62
|
|
|
* |
63
|
|
|
* @param mixed $data |
64
|
|
|
*/ |
65
|
|
|
abstract protected function process(&$data); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The payload object or array matching the GraphQL definition |
69
|
|
|
* |
70
|
|
|
* @param mixed $data normalized data, its the input data processed by the form |
71
|
|
|
* @param ConstraintViolationList $violations violations returned by the form validation process |
72
|
|
|
* @param array $inputSource the original submitted data in array |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
abstract protected function returnPayload($data, ConstraintViolationList $violations, $inputSource); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $data |
80
|
|
|
* |
81
|
|
|
* @return FormBuilderInterface |
82
|
|
|
*/ |
83
|
7 |
|
protected function createDefinitionForm($data): FormBuilderInterface |
84
|
|
|
{ |
85
|
7 |
|
if (!$this->context->getDefinition()->getMeta('form')) { |
86
|
|
|
throw new \RuntimeException(sprintf('Can`t find a valid form for %s', $this->context->getDefinition()->getName())); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
$form = $this->context->getDefinition()->getMeta('form'); |
90
|
|
|
|
91
|
|
|
$options = [ |
92
|
7 |
|
'csrf_protection' => false, |
93
|
|
|
'allow_extra_fields' => true, |
94
|
|
|
]; |
95
|
7 |
|
if ($this->context->getDefinition()->hasMeta('form_options')) { |
96
|
7 |
|
$options = array_merge($options, $this->context->getDefinition()->getMeta('form_options')); |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
$form = $this->createFormBuilder($form, $data, $options); |
100
|
7 |
|
$viewTransformer = new DataWithIdToNodeTransformer($this->getManager(), $this->context->getDefinitionManager()); |
101
|
7 |
|
$form->addViewTransformer($viewTransformer); |
102
|
|
|
|
103
|
7 |
|
return $form; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param FormInterface $form |
108
|
|
|
* @param ConstraintViolationList $violations |
109
|
|
|
* @param null|string $parentName |
110
|
|
|
*/ |
111
|
7 |
|
protected function extractFormErrors(FormInterface $form, ConstraintViolationList $violations, ?string $parentName = null) |
112
|
|
|
{ |
113
|
7 |
|
$errors = $form->getErrors(); |
114
|
7 |
|
foreach ($errors as $error) { |
115
|
1 |
|
$violation = new ConstraintViolation(); |
116
|
|
|
|
117
|
1 |
|
$path = null; |
118
|
1 |
|
if ($form->getParent()) { |
119
|
1 |
|
$path = $form->getName(); |
120
|
|
|
} |
121
|
1 |
|
if ($parentName) { |
|
|
|
|
122
|
1 |
|
$path = $parentName.'.'.$form->getName(); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$violation->setPropertyPath($path); |
126
|
1 |
|
$violation->setMessage($error->getMessage()); |
|
|
|
|
127
|
1 |
|
$violation->setMessageTemplate($error->getMessageTemplate()); |
|
|
|
|
128
|
1 |
|
foreach ($error->getMessageParameters() as $key => $value) { |
|
|
|
|
129
|
1 |
|
$violation->addParameter($key, $value); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$cause = $error->getCause(); |
|
|
|
|
133
|
1 |
|
if ($cause instanceof SymfonyConstraintViolation) { |
134
|
1 |
|
$violation->setCode($cause->getCode()); |
135
|
1 |
|
$violation->setInvalidValue($cause->getInvalidValue()); |
136
|
1 |
|
$violation->setPlural($cause->getPlural()); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
$violations->addViolation($violation); |
140
|
|
|
} |
141
|
7 |
|
if ($form->all()) { |
142
|
7 |
|
foreach ($form->all() as $child) { |
143
|
7 |
|
$parentName = $form->getName(); |
144
|
|
|
|
145
|
|
|
//avoid set the name of the form |
146
|
7 |
|
if ($form->getName() === $parentName && !$form->getParent()) { |
147
|
7 |
|
$parentName = null; |
148
|
|
|
} |
149
|
|
|
|
150
|
7 |
|
$this->extractFormErrors($child, $violations, $parentName); |
151
|
|
|
} |
152
|
|
|
} |
153
|
7 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Can use this method to verify if submitted data is valid |
157
|
|
|
* otherwise can trow a error |
158
|
|
|
* |
159
|
|
|
* @param mixed $inputSource contain the original submitted input data |
160
|
|
|
* @param mixed $normData contains the processed and normalized data by the form |
161
|
|
|
*/ |
162
|
3 |
|
protected function onSubmit($inputSource, &$normData) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
//override in child |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Can do something before validate the submitted data |
169
|
|
|
* |
170
|
|
|
* @param mixed $data |
171
|
|
|
*/ |
172
|
7 |
|
protected function preValidate(&$data) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
//override in child |
175
|
7 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Can use this to add your custom validations errors |
179
|
|
|
* |
180
|
|
|
* @param mixed $data |
181
|
|
|
* @param ConstraintViolationList $violations |
182
|
|
|
*/ |
183
|
6 |
|
protected function postValidation($data, ConstraintViolationList $violations) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
//override in child |
186
|
6 |
|
} |
187
|
|
|
} |
188
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: