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 FormResult |
53
|
|
|
*/ |
54
|
|
|
protected $lastValidationResult; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $hash; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
protected $hashShouldBeCalculated = true; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* You should never create a new instance of this class directly, use the |
68
|
|
|
* `FormObjectFactory->getInstanceFromClassName()` function instead. |
69
|
|
|
* |
70
|
|
|
* @param string $className |
71
|
|
|
* @param string $name |
72
|
|
|
* @param array $formConfiguration |
73
|
|
|
*/ |
74
|
|
|
public function __construct($className, $name, array $formConfiguration) |
75
|
|
|
{ |
76
|
|
|
$this->className = $className; |
77
|
|
|
$this->name = $name; |
78
|
|
|
$this->setUpConfiguration($formConfiguration); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $formConfiguration |
83
|
|
|
*/ |
84
|
|
|
protected function setUpConfiguration(array $formConfiguration) |
85
|
|
|
{ |
86
|
|
|
$this->configuration = Core::instantiate(FormObjectConfiguration::class, $this, $formConfiguration); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getName() |
94
|
|
|
{ |
95
|
|
|
return $this->name; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getClassName() |
102
|
|
|
{ |
103
|
|
|
return $this->className; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Registers a new property for this form. |
108
|
|
|
* |
109
|
|
|
* @param string $name |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function addProperty($name) |
113
|
|
|
{ |
114
|
|
|
if (false === $this->hasProperty($name)) { |
115
|
|
|
$this->properties[] = $name; |
116
|
|
|
$this->hashShouldBeCalculated = true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $name |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function hasProperty($name) |
127
|
|
|
{ |
128
|
|
|
return in_array($name, $this->properties); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getProperties() |
135
|
|
|
{ |
136
|
|
|
return $this->properties; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return Form |
141
|
|
|
*/ |
142
|
|
|
public function getConfiguration() |
143
|
|
|
{ |
144
|
|
|
/** @var Form $configuration */ |
145
|
|
|
$configuration = $this->configuration->getConfigurationObject()->getObject(true); |
146
|
|
|
|
147
|
|
|
return $configuration; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* This function will merge and return the validation results of both the |
152
|
|
|
* global Formz configuration object, and this form configuration object. |
153
|
|
|
* |
154
|
|
|
* @return Result |
155
|
|
|
*/ |
156
|
|
|
public function getConfigurationValidationResult() |
157
|
|
|
{ |
158
|
|
|
return $this->configuration->getConfigurationValidationResult(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the hash, which should be calculated only once for performance |
163
|
|
|
* concerns. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getHash() |
168
|
|
|
{ |
169
|
|
|
if (true === $this->hashShouldBeCalculated |
170
|
|
|
|| null === $this->hash |
171
|
|
|
) { |
172
|
|
|
$this->hashShouldBeCalculated = false; |
173
|
|
|
$this->hash = $this->calculateHash(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->hash; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the calculated hash of this class. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function calculateHash() |
185
|
|
|
{ |
186
|
|
|
return sha1(serialize($this)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return FormResult |
191
|
|
|
*/ |
192
|
|
|
public function getLastValidationResult() |
193
|
|
|
{ |
194
|
|
|
return $this->lastValidationResult; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function hasLastValidationResult() |
201
|
|
|
{ |
202
|
|
|
return null !== $this->lastValidationResult; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param FormResult $lastValidationResult |
207
|
|
|
*/ |
208
|
|
|
public function setLastValidationResult($lastValidationResult) |
209
|
|
|
{ |
210
|
|
|
$this->lastValidationResult = $lastValidationResult; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* When this instance is saved in TYPO3 cache, we need not to store all the |
215
|
|
|
* properties to increase performance. |
216
|
|
|
* |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
public function __sleep() |
220
|
|
|
{ |
221
|
|
|
return ['name', 'className', 'properties', 'hash', 'configuration']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* When this class is unserialized, we update the flag to know if the hash |
226
|
|
|
* should be calculated or not (if it was calculated before it was |
227
|
|
|
* serialized, there is no need to calculate it again). |
228
|
|
|
*/ |
229
|
|
|
public function __wakeup() |
230
|
|
|
{ |
231
|
|
|
$this->hashShouldBeCalculated = (null === $this->hash); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|