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\FormObject\Service; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectFactory; |
17
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInstance; |
18
|
|
|
use Romm\Formz\Configuration\ConfigurationFactory; |
19
|
|
|
use Romm\Formz\Configuration\Form\Form; |
20
|
|
|
use Romm\Formz\Form\FormObject\FormObjectStatic; |
21
|
|
|
use Romm\Formz\Service\CacheService; |
22
|
|
|
use Romm\Formz\Validation\Validator\Internal\FormConfigurationValidator; |
23
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; |
24
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
25
|
|
|
|
26
|
|
|
class FormObjectConfiguration |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var FormObjectStatic |
30
|
|
|
*/ |
31
|
|
|
protected $static; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $configurationArray = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Contains the form configuration object, which was created from the |
40
|
|
|
* configuration array. |
41
|
|
|
* |
42
|
|
|
* @var ConfigurationObjectInstance |
43
|
|
|
*/ |
44
|
|
|
protected $configurationObject; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Result |
48
|
|
|
*/ |
49
|
|
|
protected $configurationValidationResult; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Flag to know if the form configuration is valid or not: if the |
53
|
|
|
* configuration is fetched from cache, then we know the configuration is |
54
|
|
|
* valid because it saved in cache only when no error is found. |
55
|
|
|
* |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
protected $configurationIsValid = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param FormObjectStatic $static |
62
|
|
|
* @param array $configurationArray |
63
|
|
|
*/ |
64
|
|
|
public function __construct(FormObjectStatic $static, array $configurationArray) |
65
|
|
|
{ |
66
|
|
|
$this->static = $static; |
67
|
|
|
$this->configurationArray = $configurationArray; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns an instance of configuration object. Checks if it was previously |
72
|
|
|
* stored in cache, otherwise it is created from scratch. |
73
|
|
|
* |
74
|
|
|
* @return ConfigurationObjectInstance |
75
|
|
|
*/ |
76
|
|
|
public function getConfigurationObject() |
77
|
|
|
{ |
78
|
|
|
if (null === $this->configurationObject) { |
79
|
|
|
$this->configurationObject = $this->getConfigurationObjectFromCache(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->configurationObject; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* This function will merge and return the validation results of both the |
87
|
|
|
* global FormZ configuration object, and this form configuration object. |
88
|
|
|
* |
89
|
|
|
* @return Result |
90
|
|
|
*/ |
91
|
|
|
public function getConfigurationValidationResult() |
92
|
|
|
{ |
93
|
|
|
if (null === $this->configurationValidationResult) { |
94
|
|
|
$this->configurationValidationResult = $this->getGlobalConfigurationValidationResult(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->configurationValidationResult; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Resets the validation result and merges it with the global FormZ |
102
|
|
|
* configuration. |
103
|
|
|
* |
104
|
|
|
* @return Result |
105
|
|
|
*/ |
106
|
|
|
protected function getGlobalConfigurationValidationResult() |
107
|
|
|
{ |
108
|
|
|
$result = new Result; |
109
|
|
|
$formzConfigurationValidationResult = ConfigurationFactory::get() |
110
|
|
|
->getFormzConfiguration() |
111
|
|
|
->getValidationResult(); |
112
|
|
|
|
113
|
|
|
$result->merge($formzConfigurationValidationResult); |
114
|
|
|
|
115
|
|
|
if (false === $this->configurationIsValid) { |
116
|
|
|
$propertyName = 'forms.' . $this->static->getClassName(); |
117
|
|
|
$formValidationResult = $this->getFormConfigurationValidationResult($this->getConfigurationObject()); |
118
|
|
|
|
119
|
|
|
$result->forProperty($propertyName)->merge($formValidationResult); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param ConfigurationObjectInstance $configurationObject |
127
|
|
|
* @return Result |
128
|
|
|
*/ |
129
|
|
|
protected function getFormConfigurationValidationResult(ConfigurationObjectInstance $configurationObject) |
130
|
|
|
{ |
131
|
|
|
$baseResult = $configurationObject->getValidationResult(); |
132
|
|
|
|
133
|
|
|
if (false === $baseResult->hasErrors()) { |
134
|
|
|
$validator = new FormConfigurationValidator; |
135
|
|
|
$extendedResult = $validator->validate($configurationObject->getObject(true)); |
136
|
|
|
|
137
|
|
|
$baseResult->merge($extendedResult); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $baseResult; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return ConfigurationObjectInstance |
145
|
|
|
*/ |
146
|
|
|
protected function getConfigurationObjectFromCache() |
147
|
|
|
{ |
148
|
|
|
$cacheInstance = $this->getCacheInstance(); |
149
|
|
|
$cacheIdentifier = 'configuration-' . $this->static->getObjectHash(); |
150
|
|
|
|
151
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
152
|
|
|
$configurationObject = $cacheInstance->get($cacheIdentifier); |
153
|
|
|
$this->configurationIsValid = true; |
154
|
|
|
} else { |
155
|
|
|
$configurationArray = $this->sanitizeConfiguration($this->configurationArray); |
156
|
|
|
$configurationObject = $this->getConfigurationObjectInstance($configurationArray); |
157
|
|
|
|
158
|
|
|
if (false === $this->getFormConfigurationValidationResult($configurationObject)->hasErrors()) { |
159
|
|
|
$cacheInstance->set($cacheIdentifier, $configurationObject); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $configurationObject; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* This function will clean the configuration array by removing useless data |
168
|
|
|
* and updating needed ones. |
169
|
|
|
* |
170
|
|
|
* @param array $configuration |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
protected function sanitizeConfiguration(array $configuration) |
174
|
|
|
{ |
175
|
|
|
// Removing configuration of fields which do not exist for this form. |
176
|
|
|
$sanitizedFieldsConfiguration = []; |
177
|
|
|
$fieldsConfiguration = (isset($configuration['fields'])) |
178
|
|
|
? $configuration['fields'] |
179
|
|
|
: []; |
180
|
|
|
|
181
|
|
|
foreach ($this->static->getProperties() as $property) { |
182
|
|
|
$sanitizedFieldsConfiguration[$property] = (isset($fieldsConfiguration[$property])) |
183
|
|
|
? $fieldsConfiguration[$property] |
184
|
|
|
: []; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$configuration['fields'] = $sanitizedFieldsConfiguration; |
188
|
|
|
|
189
|
|
|
return $configuration; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param array $configuration |
194
|
|
|
* @return ConfigurationObjectInstance |
195
|
|
|
*/ |
196
|
|
|
protected function getConfigurationObjectInstance(array $configuration) |
197
|
|
|
{ |
198
|
|
|
return ConfigurationObjectFactory::getInstance()->get(Form::class, $configuration); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return FrontendInterface |
203
|
|
|
*/ |
204
|
|
|
protected function getCacheInstance() |
205
|
|
|
{ |
206
|
|
|
return CacheService::get()->getCacheInstance(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public function __sleep() |
213
|
|
|
{ |
214
|
|
|
return ['static', 'configurationArray']; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|