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; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Form; |
17
|
|
|
use Romm\Formz\Core\Core; |
18
|
|
|
use Romm\Formz\Error\FormResult; |
19
|
|
|
use Romm\Formz\Service\HashService; |
20
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This is the object representation of a form. In here we can manage which |
24
|
|
|
* properties the form does have, its configuration, and more. |
25
|
|
|
*/ |
26
|
|
|
class FormObject |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Name of the form. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $name; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $className; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The properties of the form. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $properties = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var FormObjectConfiguration |
49
|
|
|
*/ |
50
|
|
|
protected $configuration; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var FormObjectRequestData |
54
|
|
|
*/ |
55
|
|
|
protected $requestData; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var FormInterface |
59
|
|
|
*/ |
60
|
|
|
protected $form; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
protected $formWasSubmitted = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var FormResult |
69
|
|
|
*/ |
70
|
|
|
protected $formResult; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $hash; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* You should never create a new instance of this class directly, use the |
79
|
|
|
* `FormObjectFactory->getInstanceFromClassName()` function instead. |
80
|
|
|
* |
81
|
|
|
* @param string $className |
82
|
|
|
* @param string $name |
83
|
|
|
* @param array $formConfiguration |
84
|
|
|
*/ |
85
|
|
|
public function __construct($className, $name, array $formConfiguration) |
86
|
|
|
{ |
87
|
|
|
$this->className = $className; |
88
|
|
|
$this->name = $name; |
89
|
|
|
$this->setUpConfiguration($formConfiguration); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getName() |
96
|
|
|
{ |
97
|
|
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getClassName() |
104
|
|
|
{ |
105
|
|
|
return $this->className; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getProperties() |
112
|
|
|
{ |
113
|
|
|
return $this->properties; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $name |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function hasProperty($name) |
121
|
|
|
{ |
122
|
|
|
return in_array($name, $this->properties); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Registers a new property for this form. |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function addProperty($name) |
132
|
|
|
{ |
133
|
|
|
if (false === $this->hasProperty($name)) { |
134
|
|
|
$this->properties[] = $name; |
135
|
|
|
$this->resetHash(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Form |
143
|
|
|
*/ |
144
|
|
|
public function getConfiguration() |
145
|
|
|
{ |
146
|
|
|
/** @var Form $configuration */ |
147
|
|
|
$configuration = $this->configuration->getConfigurationObject()->getObject(true); |
148
|
|
|
|
149
|
|
|
return $configuration; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* This function will merge and return the validation results of both the |
154
|
|
|
* global FormZ configuration object, and this form configuration object. |
155
|
|
|
* |
156
|
|
|
* @return Result |
157
|
|
|
*/ |
158
|
|
|
public function getConfigurationValidationResult() |
159
|
|
|
{ |
160
|
|
|
return $this->configuration->getConfigurationValidationResult(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return FormObjectRequestData |
165
|
|
|
*/ |
166
|
|
|
public function getRequestData() |
167
|
|
|
{ |
168
|
|
|
return $this->requestData; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return FormInterface |
173
|
|
|
*/ |
174
|
|
|
public function getForm() |
175
|
|
|
{ |
176
|
|
|
return $this->form; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function hasForm() |
183
|
|
|
{ |
184
|
|
|
return null !== $this->form; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param FormInterface $form |
189
|
|
|
*/ |
190
|
|
|
public function setForm(FormInterface $form) |
191
|
|
|
{ |
192
|
|
|
$this->form = $form; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Will mark the form as submitted (change the result returned by the |
197
|
|
|
* function `formWasSubmitted()`). |
198
|
|
|
*/ |
199
|
|
|
public function markFormAsSubmitted() |
200
|
|
|
{ |
201
|
|
|
$this->formWasSubmitted = true; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns `true` if the form was submitted by the user. |
206
|
|
|
* |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
|
public function formWasSubmitted() |
210
|
|
|
{ |
211
|
|
|
return $this->formWasSubmitted; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return FormResult |
216
|
|
|
*/ |
217
|
|
|
public function getFormResult() |
218
|
|
|
{ |
219
|
|
|
return $this->formResult; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
public function hasFormResult() |
226
|
|
|
{ |
227
|
|
|
return null !== $this->formResult; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param FormResult $formResult |
232
|
|
|
*/ |
233
|
|
|
public function setFormResult($formResult) |
234
|
|
|
{ |
235
|
|
|
$this->formResult = $formResult; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns the hash, which should be calculated only once for performance |
240
|
|
|
* concerns. |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getHash() |
245
|
|
|
{ |
246
|
|
|
if (null === $this->hash) { |
247
|
|
|
$this->hash = $this->calculateHash(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->hash; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param array $formConfiguration |
255
|
|
|
*/ |
256
|
|
|
protected function setUpConfiguration(array $formConfiguration) |
257
|
|
|
{ |
258
|
|
|
$this->configuration = Core::instantiate(FormObjectConfiguration::class, $this, $formConfiguration); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the calculated hash of this class. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
protected function calculateHash() |
267
|
|
|
{ |
268
|
|
|
return HashService::get()->getHash(serialize($this)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Resets the hash, which will be calculated on next access. |
273
|
|
|
*/ |
274
|
|
|
protected function resetHash() |
275
|
|
|
{ |
276
|
|
|
$this->hash = null; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param FormObjectRequestData $requestData |
281
|
|
|
*/ |
282
|
|
|
public function injectRequestData(FormObjectRequestData $requestData) |
283
|
|
|
{ |
284
|
|
|
$this->requestData = $requestData; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Injects external dependencies. |
289
|
|
|
*/ |
290
|
|
|
public function __wakeup() |
291
|
|
|
{ |
292
|
|
|
$this->requestData = Core::instantiate(FormObjectRequestData::class); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* When this instance is saved in TYPO3 cache, we need not to store all the |
297
|
|
|
* properties to increase performance. |
298
|
|
|
* |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
|
public function __sleep() |
302
|
|
|
{ |
303
|
|
|
return ['name', 'className', 'properties', 'hash', 'configuration']; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|