1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form\Form; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form\FormView; |
9
|
|
|
use Silverback\ApiComponentBundle\Factory\FormFactory; |
10
|
|
|
use Silverback\ApiComponentBundle\Form\Handler\FormHandlerInterface; |
11
|
|
|
use Silverback\ApiComponentBundle\Validator\ClassNameValidator; |
12
|
|
|
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
17
|
|
|
|
18
|
|
|
class FormSubmitPost extends AbstractForm implements ServiceSubscriberInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var FormHandlerInterface[] |
22
|
|
|
*/ |
23
|
|
|
private $handlers; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
EntityManagerInterface $entityManager, |
27
|
|
|
SerializerInterface $serializer, |
28
|
|
|
FormFactory $formFactory, |
29
|
|
|
iterable $formHandlers |
30
|
|
|
) { |
31
|
|
|
parent::__construct($entityManager, $serializer, $formFactory); |
32
|
|
|
$this->handlers = $formHandlers; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @Route( |
37
|
|
|
* name="silverback_api_component_form_submit", |
38
|
|
|
* path="/component/forms/{id}/submit.{_format}", |
39
|
|
|
* requirements={"id"="[^/]+"}, |
40
|
|
|
* defaults={ |
41
|
|
|
* "_api_resource_class"=Form::class, |
42
|
|
|
* "_api_item_operation_name"="validate_form", |
43
|
|
|
* "_format"="jsonld" |
44
|
|
|
* } |
45
|
|
|
* ) |
46
|
|
|
* @Method("POST") |
47
|
|
|
* @param Request $request |
48
|
|
|
* @param Form $data |
49
|
|
|
* @param string $_format |
50
|
|
|
* @return Response |
51
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
52
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
53
|
|
|
* @throws \UnexpectedValueException |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
56
|
|
|
* @throws \BadMethodCallException |
57
|
|
|
* @throws \ReflectionException |
58
|
|
|
* @throws \LogicException |
59
|
|
|
*/ |
60
|
|
|
public function __invoke(Request $request, Form $data, string $_format) |
61
|
|
|
{ |
62
|
|
|
$form = $this->formFactory->createForm($data); |
63
|
|
|
$formData = $this->deserializeFormData($form, $request->getContent()); |
64
|
|
|
$form->submit($formData); |
65
|
|
|
if (!$form->isSubmitted()) { |
66
|
|
|
return $this->getResponse($data, $_format, false); |
67
|
|
|
} |
68
|
|
|
$valid = $form->isValid(); |
69
|
|
|
$data->setForm(new FormView($form->createView())); |
70
|
|
|
if ($valid && $data->getSuccessHandler()) { |
71
|
|
|
foreach ($this->handlers as $handler) { |
72
|
|
|
if (ClassNameValidator::isClassSame($data->getSuccessHandler(), $handler)) { |
73
|
|
|
$handler->success($data); |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return $this->getResponse($data, $_format, $valid); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..