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