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\Form\Definition\Step\Step; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
17
|
|
|
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent; |
18
|
|
|
use Romm\Formz\Form\Definition\Condition\Activation; |
19
|
|
|
use Romm\Formz\Form\Definition\Condition\ActivationInterface; |
20
|
|
|
use Romm\Formz\Form\Definition\Step\Steps; |
21
|
|
|
|
22
|
|
|
class StepDefinition extends AbstractFormDefinitionComponent |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
* @validate NotEmpty |
27
|
|
|
*/ |
28
|
|
|
protected $step; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Romm\Formz\Form\Definition\Condition\Activation |
32
|
|
|
* @validate Romm.Formz:Internal\ConditionIsValid |
33
|
|
|
*/ |
34
|
|
|
protected $activation; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Romm\Formz\Form\Definition\Step\Step\StepDefinition |
38
|
|
|
*/ |
39
|
|
|
protected $next; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Romm\Formz\Form\Definition\Step\Step\DivergenceStepDefinition[] |
43
|
|
|
*/ |
44
|
|
|
protected $divergence; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return Step |
48
|
|
|
*/ |
49
|
|
|
public function getStep() |
50
|
|
|
{ |
51
|
|
|
return $this->withFirstParent( |
52
|
|
|
Steps::class, |
53
|
|
|
function (Steps $steps) { |
54
|
|
|
return $steps->getEntry($this->step); |
55
|
|
|
} |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Activation |
61
|
|
|
*/ |
62
|
|
|
public function getActivation() |
63
|
|
|
{ |
64
|
|
|
return $this->activation; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function hasActivation() |
71
|
|
|
{ |
72
|
|
|
return $this->activation instanceof ActivationInterface; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getStepLevel() |
76
|
|
|
{ |
77
|
|
|
$level = 1; |
78
|
|
|
|
79
|
|
|
if ($this->hasPreviousDefinition()) { |
80
|
|
|
$this->getPreviousDefinition()->alongParents( |
81
|
|
|
function ($parent) use (&$level) { |
82
|
|
|
if ($parent instanceof self) { |
83
|
|
|
$level += $parent->getStepWeight(); |
84
|
|
|
} elseif ($parent instanceof Steps) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $level; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getStepWeight() |
97
|
|
|
{ |
98
|
|
|
$weight = 1; |
99
|
|
|
$childWeight = $this->hasNextStep() ? 1 : 0; |
100
|
|
|
|
101
|
|
|
if ($this->hasDivergence()) { |
102
|
|
|
foreach ($this->getDivergenceSteps() as $divergenceStep) { |
103
|
|
|
$childWeight = max($childWeight, $divergenceStep->getStepWeight()); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $weight + $childWeight; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasNextStep() |
114
|
|
|
{ |
115
|
|
|
return null !== $this->next; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Alias for Fluid usage. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function getHasNextStep() |
124
|
|
|
{ |
125
|
|
|
return $this->hasNextStep(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return StepDefinition |
130
|
|
|
* @throws EntryNotFoundException |
131
|
|
|
*/ |
132
|
|
|
public function getNextStep() |
133
|
|
|
{ |
134
|
|
|
if (false === $this->hasNextStep()) { |
135
|
|
|
throw EntryNotFoundException::nextStepsNotFound($this); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->next; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function hasPreviousDefinition() |
145
|
|
|
{ |
146
|
|
|
return $this->hasParent(self::class); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Alias for Fluid usage. |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function getHasPreviousDefinition() |
155
|
|
|
{ |
156
|
|
|
return $this->hasParent(self::class); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return StepDefinition |
161
|
|
|
* @throws EntryNotFoundException |
162
|
|
|
*/ |
163
|
|
|
public function getPreviousDefinition() |
164
|
|
|
{ |
165
|
|
|
if (false === $this->hasPreviousDefinition()) { |
166
|
|
|
throw EntryNotFoundException::previousDefinitionNotFound($this); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** @var StepDefinition $previousStepDefinition */ |
170
|
|
|
$previousStepDefinition = $this->getFirstParent(self::class); |
171
|
|
|
|
172
|
|
|
return $previousStepDefinition; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return DivergenceStepDefinition[] |
177
|
|
|
*/ |
178
|
|
|
public function getDivergenceSteps() |
179
|
|
|
{ |
180
|
|
|
return $this->divergence; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function hasDivergence() |
187
|
|
|
{ |
188
|
|
|
return false === empty($this->divergence); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function hash() |
195
|
|
|
{ |
196
|
|
|
return serialize([ |
197
|
|
|
$this->step, |
198
|
|
|
(function () { |
199
|
|
|
if (!$this->activation) { |
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return [ |
204
|
|
|
$this->activation->getExpression(), |
205
|
|
|
$this->activation->getAllConditions(), |
206
|
|
|
]; |
207
|
|
|
})(), |
208
|
|
|
$this->next ? $this->next->hash() : null, |
209
|
|
|
(function () { |
210
|
|
|
if (!$this->divergence) { |
|
|
|
|
211
|
|
|
return null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return array_map(function (DivergenceStepDefinition $divergenceStepDefinition) { |
215
|
|
|
return $divergenceStepDefinition->hash(); |
216
|
|
|
}, $this->divergence); |
217
|
|
|
})(), |
218
|
|
|
]); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.