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