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\Definition\Field\Validation; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor; |
17
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface; |
18
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait; |
19
|
|
|
use Romm\Formz\Exceptions\DuplicateEntryException; |
20
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
21
|
|
|
use Romm\Formz\Exceptions\SilentException; |
22
|
|
|
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent; |
23
|
|
|
use Romm\Formz\Form\Definition\Condition\Activation; |
24
|
|
|
use Romm\Formz\Form\Definition\Condition\ActivationInterface; |
25
|
|
|
use Romm\Formz\Form\Definition\Condition\ActivationUsageInterface; |
26
|
|
|
use Romm\Formz\Form\Definition\Field\Field; |
27
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
28
|
|
|
|
29
|
|
|
class Validator extends AbstractFormDefinitionComponent implements ActivationUsageInterface, DataPreProcessorInterface |
30
|
|
|
{ |
31
|
|
|
use ArrayConversionTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
* @validate NotEmpty |
36
|
|
|
*/ |
37
|
|
|
private $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
* @validate NotEmpty |
42
|
|
|
* @validate Romm.ConfigurationObject:ClassImplements(interface=TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface) |
43
|
|
|
*/ |
44
|
|
|
private $className; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
* @validate Number |
49
|
|
|
*/ |
50
|
|
|
protected $priority; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $options = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \Romm\Formz\Form\Definition\Field\Validation\Message[] |
59
|
|
|
*/ |
60
|
|
|
protected $messages = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \Romm\Formz\Form\Definition\Condition\Activation |
64
|
|
|
* @validate Romm.Formz:Internal\ConditionIsValid |
65
|
|
|
*/ |
66
|
|
|
protected $activation; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
protected $useAjax = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $className |
76
|
|
|
*/ |
77
|
|
|
public function __construct($name, $className) |
78
|
|
|
{ |
79
|
|
|
$this->name = $name; |
80
|
|
|
$this->className = $className; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getName() |
87
|
|
|
{ |
88
|
|
|
return $this->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getClassName() |
95
|
|
|
{ |
96
|
|
|
return $this->className; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getPriority() |
103
|
|
|
{ |
104
|
|
|
return $this->priority; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int $priority |
109
|
|
|
*/ |
110
|
|
|
public function setPriority($priority) |
111
|
|
|
{ |
112
|
|
|
$this->checkDefinitionFreezeState(); |
113
|
|
|
|
114
|
|
|
$this->priority = $priority; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getOptions() |
121
|
|
|
{ |
122
|
|
|
return $this->options; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $options |
127
|
|
|
*/ |
128
|
|
|
public function setOptions(array $options) |
129
|
|
|
{ |
130
|
|
|
$this->checkDefinitionFreezeState(); |
131
|
|
|
|
132
|
|
|
$this->options = $options; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return Message[] |
137
|
|
|
*/ |
138
|
|
|
public function getMessages() |
139
|
|
|
{ |
140
|
|
|
return $this->messages; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $identifier |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function hasMessage($identifier) |
148
|
|
|
{ |
149
|
|
|
return true === isset($this->messages[$identifier]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $identifier |
154
|
|
|
* @return Message |
155
|
|
|
* @throws EntryNotFoundException |
156
|
|
|
*/ |
157
|
|
|
public function getMessage($identifier) |
158
|
|
|
{ |
159
|
|
|
if (false === $this->hasMessage($identifier)) { |
160
|
|
|
throw EntryNotFoundException::messageNotFound($identifier); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->messages[$identifier]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $identifier |
168
|
|
|
* @return Message |
169
|
|
|
* @throws DuplicateEntryException |
170
|
|
|
*/ |
171
|
|
|
public function addMessage($identifier) |
172
|
|
|
{ |
173
|
|
|
$this->checkDefinitionFreezeState(); |
174
|
|
|
|
175
|
|
|
if ($this->hasMessage($identifier)) { |
176
|
|
|
throw DuplicateEntryException::validatorMessageAlreadyAdded($identifier, $this); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** @var Message $message */ |
180
|
|
|
$message = GeneralUtility::makeInstance(Message::class, $identifier); |
181
|
|
|
$message->attachParent($this); |
182
|
|
|
|
183
|
|
|
$this->messages[$identifier] = $message; |
184
|
|
|
|
185
|
|
|
return $message; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return ActivationInterface |
190
|
|
|
* @throws SilentException |
191
|
|
|
*/ |
192
|
|
|
public function getActivation() |
193
|
|
|
{ |
194
|
|
|
if (false === $this->hasActivation()) { |
195
|
|
|
throw SilentException::validatorHasNoActivation($this); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->activation; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function hasActivation() |
205
|
|
|
{ |
206
|
|
|
return $this->activation instanceof ActivationInterface; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return ActivationInterface |
211
|
|
|
*/ |
212
|
|
|
public function addActivation() |
213
|
|
|
{ |
214
|
|
|
$this->checkDefinitionFreezeState(); |
215
|
|
|
|
216
|
|
|
if (null === $this->activation) { |
217
|
|
|
$this->activation = GeneralUtility::makeInstance(Activation::class); |
218
|
|
|
$this->activation->attachParent($this); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->activation; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
public function doesUseAjax() |
228
|
|
|
{ |
229
|
|
|
return (bool)$this->useAjax; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Turns the Ajax usage ON. |
234
|
|
|
*/ |
235
|
|
|
public function activateAjaxUsage() |
236
|
|
|
{ |
237
|
|
|
$this->checkDefinitionFreezeState(); |
238
|
|
|
|
239
|
|
|
$this->useAjax = true; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Turns the Ajax usage OFF. |
244
|
|
|
*/ |
245
|
|
|
public function deactivateAjaxUsage() |
246
|
|
|
{ |
247
|
|
|
$this->checkDefinitionFreezeState(); |
248
|
|
|
|
249
|
|
|
$this->useAjax = false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return Field |
254
|
|
|
*/ |
255
|
|
|
public function getParentField() |
256
|
|
|
{ |
257
|
|
|
/** @var Field $field */ |
258
|
|
|
$field = $this->getFirstParent(Field::class); |
259
|
|
|
|
260
|
|
|
return $field; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param DataPreProcessor $processor |
265
|
|
|
*/ |
266
|
|
|
public static function dataPreProcessor(DataPreProcessor $processor) |
267
|
|
|
{ |
268
|
|
|
$data = $processor->getData(); |
269
|
|
|
|
270
|
|
|
/* |
271
|
|
|
* Forcing the identifiers of the messages: they are the keys of the |
272
|
|
|
* array entries. |
273
|
|
|
*/ |
274
|
|
|
self::forceNameForProperty($data, 'messages', 'identifier'); |
275
|
|
|
|
276
|
|
|
$processor->setData($data); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|