1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Silverback\ApiComponentBundle\Dto\Form\FormView; |
6
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form\Form; |
7
|
|
|
use Symfony\Component\Form\FormInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
class FormPatchAction extends AbstractFormAction |
12
|
|
|
{ |
13
|
|
|
private function isAssocArray(array $arr): bool |
14
|
|
|
{ |
15
|
|
|
if (array() === $arr) { |
16
|
|
|
return false; |
17
|
|
|
} |
18
|
|
|
return array_keys($arr) !== range(0, count($arr) - 1); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function arrayIsStrings(array $arr): bool |
22
|
|
|
{ |
23
|
|
|
foreach ($arr as $item) { |
24
|
|
|
if (!is_string($item)) { |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function getNestedKey(FormInterface $form, array $formData): FormInterface |
32
|
|
|
{ |
33
|
|
|
$child = $form->get($key = key($formData)); |
34
|
|
|
while(is_array($formData = $formData[$key]) && $count = \count($formData)) { |
35
|
|
|
if (!$this->isAssocArray($formData) && $this->arrayIsStrings($formData)) { |
36
|
|
|
break; |
37
|
|
|
} |
38
|
|
|
// front-end should submit empty objects for each item in a collection up to the one we are trying to validate |
39
|
|
|
// so let us just get the last item to validate |
40
|
|
|
$key = ($count - 1); |
41
|
|
|
|
42
|
|
|
if ($count === 1 || !$child->has($key)) { |
43
|
|
|
$child = $child->get($key = key($formData)); |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$child = $child->get($key); |
48
|
|
|
} |
49
|
|
|
return $child; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Request $request |
54
|
|
|
* @param Form $data |
55
|
|
|
* @return Response |
56
|
|
|
*/ |
57
|
|
|
public function __invoke(Request $request, Form $data) |
58
|
|
|
{ |
59
|
|
|
$contentType = $request->headers->get('CONTENT_TYPE'); |
60
|
|
|
$_format = $request->attributes->get('_format') ?: $request->getFormat($contentType); |
61
|
|
|
|
62
|
|
|
$builder = $this->formFactory->create($data); |
63
|
|
|
$form = $builder->getForm(); |
64
|
|
|
$formData = $this->deserializeFormData($form, $request->getContent()); |
65
|
|
|
$form->submit($formData, false); |
66
|
|
|
|
67
|
|
|
$dataCount = \count($formData); |
68
|
|
|
if ($dataCount === 1) { |
69
|
|
|
$formItem = $this->getNestedKey($form, $formData); |
70
|
|
|
$data->setForm($formView = new FormView($formItem->createView())); |
71
|
|
|
return $this->getResponse($data, $_format, $this->getFormValid($formView)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$datum = []; |
75
|
|
|
$valid = true; |
76
|
|
|
foreach ($formData as $key => $value) { |
77
|
|
|
$dataItem = clone $data; |
78
|
|
|
$formItem = $this->getNestedKey($form, $formData[$key]); |
79
|
|
|
$dataItem->setForm($formView = new FormView($formItem->createView())); |
80
|
|
|
$datum[] = $dataItem; |
81
|
|
|
if ($valid && !$this->getFormValid($formView)) { |
82
|
|
|
$valid = false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->getResponse($datum, $_format, $valid); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|