1 | <?php |
||
24 | final class BeginMiddleware implements BasicMiddlewareInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var MiddlewareProcessor |
||
28 | */ |
||
29 | private $processor; |
||
30 | |||
31 | /** |
||
32 | * @var FormService |
||
33 | */ |
||
34 | protected $formService; |
||
35 | |||
36 | /** |
||
37 | * Initialization of this middleware. |
||
38 | */ |
||
39 | public function initialize() |
||
40 | { |
||
41 | $this->formService = Core::instantiate(FormService::class, $this->processor); |
||
42 | |||
43 | $this->checkFormSubmission(); |
||
44 | $this->fetchCurrentStep(); |
||
45 | $this->fetchSubstepsLevel(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * This is the first middleware being called, it will send a signal to all |
||
50 | * middlewares that depend on it. |
||
51 | */ |
||
52 | public function execute() |
||
53 | { |
||
54 | $signalObject = new SignalObject($this->processor, BeginSignal::class, After::class); |
||
55 | $signalObject->dispatch(); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Will check if the current form was submitted by the user. If it is found, |
||
60 | * the form instance is injected in the form object. |
||
61 | */ |
||
62 | protected function checkFormSubmission() |
||
63 | { |
||
64 | if ($this->processor->inSingleFieldValidationContext()) { |
||
65 | /* |
||
66 | * In "single field validation context", there is no need to check |
||
67 | * for the form submission. |
||
68 | */ |
||
69 | return; |
||
70 | } |
||
71 | |||
72 | $request = $this->processor->getRequest(); |
||
73 | $formObject = $this->processor->getFormObject(); |
||
74 | $formName = $formObject->getName(); |
||
75 | |||
76 | if ($this->requestWasSubmitted() |
||
77 | && $this->processor->getRequestArguments()->hasArgument($formName) |
||
78 | ) { |
||
79 | if (false === $request->hasArgument('fz-hash')) { |
||
80 | throw new \Exception('todo fz-hash'); // @todo |
||
81 | } |
||
82 | |||
83 | if (false === $request->hasArgument('formzData')) { |
||
84 | throw new \Exception('todo formzData'); // @todo |
||
85 | } |
||
86 | |||
87 | $form = $this->formService->getFormInstance(); |
||
88 | |||
89 | $formObject->setForm($form); |
||
90 | |||
91 | $formzData = $request->getArgument('formzData'); |
||
92 | $formObject->getRequestData()->fillFromHash($formzData); |
||
93 | |||
94 | $proxy = FormObjectFactory::get()->getProxy($form); |
||
95 | $proxy->markFormAsSubmitted(); |
||
96 | |||
97 | $this->injectFormHashInProxy(); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @todo |
||
103 | */ |
||
104 | protected function fetchCurrentStep() |
||
105 | { |
||
106 | $formObject = $this->processor->getFormObject(); |
||
107 | $request = ($formObject->formWasSubmitted()) |
||
108 | ? $this->processor->getRequest()->getReferringRequest() |
||
109 | : $this->processor->getRequest(); |
||
110 | |||
111 | $formObject->fetchCurrentStep($request); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @todo |
||
116 | */ |
||
117 | protected function fetchSubstepsLevel() |
||
118 | { |
||
119 | $request = $this->processor->getRequest(); |
||
120 | |||
121 | if ($this->requestWasSubmitted() |
||
122 | && $request->hasArgument('substepsLevel') |
||
123 | ) { |
||
124 | $substepLevel = $request->getArgument('substepsLevel'); |
||
125 | FormObjectFactory::get() |
||
126 | ->getStepService($this->processor->getFormObject()) |
||
127 | ->setSubstepsLevel($substepLevel); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Fetches the form hash from the request data that has been submitted with |
||
133 | * the form, and injects it in the form proxy. |
||
134 | */ |
||
135 | protected function injectFormHashInProxy() |
||
136 | { |
||
137 | $formObject = $this->processor->getFormObject(); |
||
138 | $hash = $formObject->getRequestData()->getFormHash(); |
||
139 | |||
140 | $proxy = FormObjectFactory::get()->getProxy($formObject->getForm()); |
||
141 | $proxy->setFormHash($hash); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param MiddlewareProcessor $middlewareProcessor |
||
146 | */ |
||
147 | final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor) |
||
151 | |||
152 | /** |
||
153 | * @return bool |
||
154 | */ |
||
155 | protected function requestWasSubmitted() |
||
159 | } |
||
160 |