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