1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Form\Handler; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Silverback\ApiComponentBundle\ApiComponentBundleEvents; |
18
|
|
|
use Silverback\ApiComponentBundle\Dto\Form\FormView; |
19
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form; |
20
|
|
|
use Silverback\ApiComponentBundle\Event\FormSuccessEvent; |
21
|
|
|
use Silverback\ApiComponentBundle\Form\Factory\FormFactory; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
23
|
|
|
use Symfony\Component\Form\FormInterface; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
27
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
28
|
|
|
use function json_decode; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author Daniel West <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class FormSubmitHandler |
34
|
|
|
{ |
35
|
|
|
private FormFactory $formFactory; |
36
|
|
|
private EventDispatcher $eventDispatcher; |
37
|
|
|
private SerializerInterface $serializer; |
38
|
|
|
|
39
|
|
|
public function __construct(FormFactory $formFactory, EventDispatcher $eventDispatcher, SerializerInterface $serializer) |
40
|
|
|
{ |
41
|
|
|
$this->formFactory = $formFactory; |
42
|
|
|
$this->eventDispatcher = $eventDispatcher; |
43
|
|
|
$this->serializer = $serializer; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function handle(Request $request, Form $formResource): Response |
47
|
|
|
{ |
48
|
|
|
$contentType = $request->headers->get('CONTENT_TYPE'); |
49
|
|
|
$builder = $this->formFactory->create($formResource); |
50
|
|
|
$form = $builder->getForm(); |
51
|
|
|
$formData = $this->deserializeFormData($form, $request->getContent()); |
52
|
|
|
$isPatchRequest = Request::METHOD_PATCH === $request->getMethod(); |
53
|
|
|
$form->submit($formData, !$isPatchRequest); |
54
|
|
|
$_format = $request->attributes->get('_format') ?: $request->getFormat($contentType); |
55
|
|
|
if ($isPatchRequest) { |
56
|
|
|
return $this->handlePatch($formResource, $form, $_format, $formData); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->handlePost($formResource, $form, $_format); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function handlePatch(Form $formResource, FormInterface $form, $_format, array $formData): Response |
63
|
|
|
{ |
64
|
|
|
$isFormViewValid = static function (FormView $formView): bool { |
65
|
|
|
return $formView->getVars()['valid']; |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
$dataCount = \count($formData); |
69
|
|
|
if (1 === $dataCount) { |
70
|
|
|
$formItem = $this->getChildFormByKey($form, $formData); |
71
|
|
|
$formResource->form = $formView = new FormView($formItem); |
72
|
|
|
|
73
|
|
|
return $this->getResponse($formResource, $_format, $isFormViewValid($formView)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$formResources = []; |
77
|
|
|
$valid = true; |
78
|
|
|
foreach ($formData as $key => $value) { |
79
|
|
|
$dataItem = clone $formResource; |
80
|
|
|
$formItem = $this->getChildFormByKey($form, $formData[$key]); |
81
|
|
|
$dataItem->form = $formView = new FormView($formItem); |
82
|
|
|
$formResources[] = $dataItem; |
83
|
|
|
if ($valid && !$isFormViewValid($formView)) { |
84
|
|
|
$valid = false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->getResponse($formResources, $_format, $valid); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function handlePost(Form $formResource, FormInterface $form, $_format): Response |
92
|
|
|
{ |
93
|
|
|
$valid = $form->isValid(); |
94
|
|
|
$formResource->form = new FormView($form); |
95
|
|
|
$context = null; |
96
|
|
|
if ($valid) { |
97
|
|
|
$event = new FormSuccessEvent($formResource, $form); |
98
|
|
|
$this->eventDispatcher->dispatch($event, ApiComponentBundleEvents::FORM_SUCCESS); |
99
|
|
|
$context = $event->serializerContext; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->getResponse($formResource, $_format, $valid, $context); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function deserializeFormData(FormInterface $form, $content): array |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
|
|
$decoded = json_decode($content, true, 512, JSON_THROW_ON_ERROR | 0); |
109
|
|
|
} catch (\JsonException $exception) { |
110
|
|
|
throw new InvalidArgumentException('json_decode error: ' . $exception->getMessage()); |
111
|
|
|
} |
112
|
|
|
if (!isset($decoded[$form->getName()])) { |
113
|
|
|
throw new BadRequestHttpException(sprintf('Form object key could not be found. Expected: <b>%s</b>: { "input_name": "input_value" }', $form->getName())); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $decoded[$form->getName()]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getChildFormByKey(FormInterface $form, array $formData): FormInterface |
120
|
|
|
{ |
121
|
|
|
$child = $form->get($key = key($formData)); |
122
|
|
|
while (\is_array($formData = $formData[$key]) && $count = \count($formData)) { |
123
|
|
|
if (!$this->isAssocArray($formData) && $this->arrayIsStrings($formData)) { |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (1 === $count) { |
128
|
|
|
$child = $child->get($key = key($formData)); |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
// front-end should submit empty objects for each item in a collection up to the one we are trying to validate |
132
|
|
|
// so let us just get the last item to validate |
133
|
|
|
// key should be numeric, if not it is probably first and second for repeated field. These should both be checked... |
134
|
|
|
$key = ($count - 1); |
135
|
|
|
if (!$child->has($key)) { |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
$child = $child->get($key); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $child; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function isAssocArray(array $arr): bool |
145
|
|
|
{ |
146
|
|
|
if ([] === $arr) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return array_keys($arr) !== range(0, \count($arr) - 1); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function arrayIsStrings(array $arr): bool |
154
|
|
|
{ |
155
|
|
|
foreach ($arr as $item) { |
156
|
|
|
if (!\is_string($item)) { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function getResponse( |
165
|
|
|
$data, |
166
|
|
|
$_format, |
167
|
|
|
$valid, |
168
|
|
|
?array $context = null |
169
|
|
|
): Response { |
170
|
|
|
$response = new Response(); |
171
|
|
|
// if (!$context) { |
172
|
|
|
// $context = ['groups' => ['component']]; |
173
|
|
|
// } |
174
|
|
|
$response->setStatusCode($valid ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST); |
175
|
|
|
$response->setContent($this->serializer->serialize($data, $_format, $context)); |
|
|
|
|
176
|
|
|
|
177
|
|
|
return $response; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|