1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Common\RequestHandler; |
23
|
|
|
|
24
|
|
|
use Symfony\Component\Form\FormInterface; |
25
|
|
|
use Symfony\Component\Form\FormView; |
26
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
29
|
|
|
|
30
|
|
|
final class JsonRequestHandler implements RequestHandlerInterface |
31
|
|
|
{ |
32
|
|
|
public function supports(Request $request): bool |
33
|
|
|
{ |
34
|
|
|
return 'json' === $request->getContentTypeFormat(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function handleForm(FormInterface $form, Request $request): void |
38
|
|
|
{ |
39
|
|
|
$data = json_decode($request->getContent(), true); |
40
|
|
|
if (is_null($data)) { |
41
|
|
|
throw new \Exception('No JSON'); |
42
|
|
|
} |
43
|
|
|
$form->submit($data); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function generateDefaultOutput(?FormInterface $form, array $extraOutput = []): array|Response |
47
|
|
|
{ |
48
|
|
|
$output = []; |
49
|
|
|
|
50
|
|
|
if ($form) { |
51
|
|
|
$output = $this->convert($form->createView()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return new JsonResponse(array_merge($extraOutput, ['form' => $output])); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function generateSuccessOutput(?FormInterface $form, array $extraOutput = []): array|Response |
58
|
|
|
{ |
59
|
|
|
return new JsonResponse(array_merge($extraOutput, ['success' => true]), Response::HTTP_ACCEPTED); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function generateErrorOutput(?FormInterface $form, array $extraOutput = []): array|Response |
63
|
|
|
{ |
64
|
|
|
$errors = []; |
65
|
|
|
if ($form) { |
66
|
|
|
foreach ($form->getErrors(true, true) as $error) { |
67
|
|
|
$fieldName = $error->getOrigin()->getName(); |
|
|
|
|
68
|
|
|
if (!isset($errors[$fieldName])) { |
69
|
|
|
$errors[$fieldName] = []; |
70
|
|
|
} |
71
|
|
|
$errors[$fieldName][] = $error->getMessage(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return new JsonResponse(array_merge($extraOutput, ['success' => false, 'errors' => $errors]), Response::HTTP_BAD_REQUEST); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function convert(FormView $formView): array |
79
|
|
|
{ |
80
|
|
|
$output = []; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var FormView $field |
84
|
|
|
*/ |
85
|
|
|
foreach ($formView->getIterator() as $name => $field) { |
86
|
|
|
if (!empty($field->children)) { |
87
|
|
|
foreach ($field->children as $childName => $child) { |
88
|
|
|
$output[$childName] = $this->convert($child); |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
$output[$name] = $field->vars['value']; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $output; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.