1 | <?php |
||
25 | class SubstepValidationService |
||
26 | { |
||
27 | /** |
||
28 | * @var FormValidatorExecutor |
||
29 | */ |
||
30 | protected $formValidatorExecutor; |
||
31 | |||
32 | /** |
||
33 | * @var FormValidatorDataObject |
||
34 | */ |
||
35 | protected $dataObject; |
||
36 | |||
37 | /** |
||
38 | * @param FormValidatorExecutor $formValidatorExecutor |
||
39 | * @param FormValidatorDataObject $dataObject |
||
40 | */ |
||
41 | public function __construct(FormValidatorExecutor $formValidatorExecutor, FormValidatorDataObject $dataObject) |
||
46 | |||
47 | /** |
||
48 | * @todo |
||
49 | */ |
||
50 | public function handleSubsteps() |
||
51 | { |
||
52 | $stepService = FormObjectFactory::get()->getStepService($this->getFormObject()); |
||
53 | |||
54 | $currentSubstepDefinition = $this->getCurrentSubstepDefinition(); |
||
55 | |||
56 | if ($this->dataObject->getValidatedStep() === $stepService->getCurrentStep()) { |
||
57 | $nextSubstep = $this->getNextSubstep($currentSubstepDefinition); |
||
58 | |||
59 | if (!$nextSubstep) { |
||
60 | $stepService->markLastSubstepAsValidated(); |
||
61 | } |
||
62 | |||
63 | if ($this->getResult()->hasErrors()) { |
||
64 | $stepService->setCurrentSubstepDefinition($currentSubstepDefinition); |
||
65 | } elseif ($nextSubstep) { |
||
66 | $stepService->setCurrentSubstepDefinition($nextSubstep); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return SubstepDefinition |
||
73 | */ |
||
74 | protected function getCurrentSubstepDefinition() |
||
75 | { |
||
76 | $stepService = FormObjectFactory::get()->getStepService($this->getFormObject()); |
||
77 | |||
78 | $firstSubstepDefinition = $this->dataObject->getValidatedStep()->getSubsteps()->getFirstSubstepDefinition(); |
||
79 | $currentSubstepDefinition = $firstSubstepDefinition; |
||
80 | $substepDefinition = $firstSubstepDefinition; |
||
81 | $substepsLevel = $stepService->getSubstepsLevel(); |
||
82 | |||
83 | while ($this->dataObject->isDummyMode() || $substepsLevel > 0) { |
||
84 | $substepsLevel--; |
||
85 | $substepIsActivated = true; |
||
86 | |||
87 | if ($substepDefinition->hasActivation()) { |
||
88 | $substepIsActivated = $this->getSubstepDefinitionActivationResult($substepDefinition); |
||
89 | } |
||
90 | |||
91 | if (true === $substepIsActivated) { |
||
92 | $supportedFields = $substepDefinition->getSubstep()->getSupportedFields(); |
||
93 | |||
94 | foreach ($supportedFields as $supportedField) { |
||
95 | $this->formValidatorExecutor->validateField($supportedField->getField()); |
||
96 | } |
||
97 | |||
98 | if ($this->getResult()->hasErrors()) { |
||
99 | $currentSubstepDefinition = $substepDefinition; |
||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | if (!$this->dataObject->isDummyMode() && $substepsLevel === 0) { |
||
105 | $currentSubstepDefinition = $substepDefinition; |
||
106 | break; |
||
107 | } |
||
108 | |||
109 | if (false === $substepDefinition->hasNextSubstep()) { |
||
110 | break; |
||
111 | } |
||
112 | |||
113 | $substepDefinition = $substepDefinition->getNextSubstep(); |
||
114 | } |
||
115 | |||
116 | return $currentSubstepDefinition; |
||
117 | } |
||
118 | |||
119 | protected function getNextSubstep(SubstepDefinition $substepDefinition) |
||
145 | |||
146 | /** |
||
147 | * @param SubstepDefinition $substepDefinition |
||
148 | * @return bool |
||
149 | */ |
||
150 | protected function getSubstepDefinitionActivationResult(SubstepDefinition $substepDefinition) |
||
158 | |||
159 | /** |
||
160 | * @return FormObject |
||
161 | */ |
||
162 | protected function getFormObject() |
||
166 | |||
167 | /** |
||
168 | * @return FormResult |
||
169 | */ |
||
170 | protected function getResult() |
||
174 | } |
||
175 |