1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Romm\Formz\Middleware\Item\Step; |
5
|
|
|
|
6
|
|
|
use Romm\Formz\Form\Definition\Step\Step\Step; |
7
|
|
|
use Romm\Formz\Form\FormObject\FormObjectFactory; |
8
|
|
|
use Romm\Formz\Form\FormObject\Service\FormObjectSteps; |
9
|
|
|
use Romm\Formz\Middleware\Argument\Arguments; |
10
|
|
|
use Romm\Formz\Middleware\Item\AbstractMiddleware; |
11
|
|
|
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal; |
12
|
|
|
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService; |
13
|
|
|
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface; |
14
|
|
|
use Romm\Formz\Middleware\Signal\After; |
15
|
|
|
|
16
|
|
|
class SubstepFetchingMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $priority = self::PRIORITY_STEP_FETCHING + 150; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Step |
25
|
|
|
*/ |
26
|
|
|
protected $currentStep; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var StepMiddlewareService |
30
|
|
|
*/ |
31
|
|
|
protected $service; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var FormObjectSteps |
35
|
|
|
*/ |
36
|
|
|
protected $stepService; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Inject the step service. |
40
|
|
|
*/ |
41
|
|
|
public function initializeMiddleware() |
42
|
|
|
{ |
43
|
|
|
$this->service = StepMiddlewareService::get(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Arguments $arguments |
48
|
|
|
*/ |
49
|
|
|
public function after(Arguments $arguments) |
50
|
|
|
{ |
51
|
|
|
if ($this->getFormObject()->formWasSubmitted()) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$formObject = $this->getFormObject(); |
56
|
|
|
|
57
|
|
|
if (false === $formObject->getDefinition()->hasSteps()) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->currentStep = $this->getCurrentStep(); |
62
|
|
|
|
63
|
|
|
if (null === $this->currentStep |
64
|
|
|
|| false === $this->currentStep->hasSubsteps() |
65
|
|
|
) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->stepService = FormObjectFactory::get()->getStepService($formObject); |
70
|
|
|
|
71
|
|
|
$this->fetchCurrentSubstep(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Fetches the current substep that should be displayed (the first |
76
|
|
|
* substep(s) may have an activation condition). |
77
|
|
|
*/ |
78
|
|
|
private function fetchCurrentSubstep() |
79
|
|
|
{ |
80
|
|
|
$this->service->reset($this->getFormObject(), $this->getRequest()); |
81
|
|
|
|
82
|
|
|
$substepDefinition = $this->service->findFirstSubstepDefinition($this->currentStep); |
83
|
|
|
|
84
|
|
|
$this->stepService->setCurrentSubstepDefinition($substepDefinition); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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: