1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Middleware\Item\Step; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Middleware\Item\DefaultMiddleware; |
17
|
|
|
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService; |
18
|
|
|
use Romm\Formz\Middleware\State\PresetMiddlewareInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This middleware should be the last one called, as it is used to dispatch the |
22
|
|
|
* request to the next step, if there is one. |
23
|
|
|
*/ |
24
|
|
|
class StepDispatchingMiddleware extends DefaultMiddleware implements PresetMiddlewareInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $priority = self::PRIORITY_STEP; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var StepMiddlewareService |
33
|
|
|
*/ |
34
|
|
|
protected $service; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Inject the step service. |
38
|
|
|
*/ |
39
|
|
|
public function initializeMiddleware() |
40
|
|
|
{ |
41
|
|
|
$this->service = StepMiddlewareService::get(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see StepDispatchingMiddleware |
46
|
|
|
*/ |
47
|
|
|
protected function process() |
48
|
|
|
{ |
49
|
|
|
$formObject = $this->getFormObject(); |
50
|
|
|
$result = $formObject->getFormResult(); |
51
|
|
|
|
52
|
|
|
if ($formObject->getConfiguration()->hasSteps() |
53
|
|
|
&& $formObject->formWasSubmitted() |
54
|
|
|
&& false === $result->hasErrors() |
55
|
|
|
) { |
56
|
|
|
$currentStep = $this->getCurrentStep(); |
57
|
|
|
$currentStepDefinition = $this->service->getStepDefinition($currentStep); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->service->markStepAsValidated($currentStep, $this->getFormRawValues()); |
|
|
|
|
60
|
|
|
|
61
|
|
|
if ($currentStepDefinition->hasNextSteps()) { |
62
|
|
|
$nextStep = $currentStepDefinition |
63
|
|
|
->getNextSteps() |
64
|
|
|
->getDefaultStep() |
65
|
|
|
->getStep(); |
66
|
|
|
|
67
|
|
|
$this->service->redirectToStep($nextStep, $this->redirect()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Fetches the raw values sent in the request. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
protected function getFormRawValues() |
78
|
|
|
{ |
79
|
|
|
$formName = $this->getFormObject()->getName(); |
80
|
|
|
$formArray = null; |
81
|
|
|
|
82
|
|
|
if ($this->getRequest()->hasArgument($formName)) { |
83
|
|
|
/** @var array $formArray */ |
84
|
|
|
$formArray = $this->getRequest()->getArgument($formName); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (false === is_array($formArray)) { |
88
|
|
|
throw new \Exception('todo'); // @todo |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $formArray; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: