|
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; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor; |
|
17
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface; |
|
18
|
|
|
use Romm\Formz\Exceptions\DuplicateEntryException; |
|
19
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
|
20
|
|
|
use Romm\Formz\Exceptions\SilentException; |
|
21
|
|
|
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent; |
|
22
|
|
|
use Romm\Formz\Form\Definition\Condition\Activation; |
|
23
|
|
|
use Romm\Formz\Form\Definition\Condition\ActivationInterface; |
|
24
|
|
|
use Romm\Formz\Form\Definition\Condition\ActivationUsageInterface; |
|
25
|
|
|
use Romm\Formz\Form\Definition\Field\Behaviour\Behaviour; |
|
26
|
|
|
use Romm\Formz\Form\Definition\Field\Settings\FieldSettings; |
|
27
|
|
|
use Romm\Formz\Form\Definition\Field\Validation\Validator; |
|
28
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
29
|
|
|
|
|
30
|
|
|
class Field extends AbstractFormDefinitionComponent implements ActivationUsageInterface, DataPreProcessorInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
* @validate NotEmpty |
|
35
|
|
|
*/ |
|
36
|
|
|
private $name; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \Romm\Formz\Form\Definition\Field\Validation\Validator[] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $validation = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Romm\Formz\Form\Definition\Field\Behaviour\Behaviour[] |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $behaviours = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Romm\Formz\Form\Definition\Condition\Activation |
|
50
|
|
|
* @validate Romm.Formz:Internal\ConditionIsValid |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $activation; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var \Romm\Formz\Form\Definition\Field\Settings\FieldSettings |
|
56
|
|
|
*/ |
|
57
|
|
|
private $settings; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct($name) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->name = $name; |
|
65
|
|
|
|
|
66
|
|
|
$this->settings = GeneralUtility::makeInstance(FieldSettings::class); |
|
67
|
|
|
$this->settings->attachParent($this); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getName() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->name; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Validator[] |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getValidators() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->validation; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function hasValidator($name) |
|
91
|
|
|
{ |
|
92
|
|
|
return true === isset($this->validation[$name]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $name |
|
97
|
|
|
* @return Validator |
|
98
|
|
|
* @throws EntryNotFoundException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getValidator($name) |
|
101
|
|
|
{ |
|
102
|
|
|
if (false === $this->hasValidator($name)) { |
|
103
|
|
|
throw EntryNotFoundException::validatorNotFound($name); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->validation[$name]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* @param string $className |
|
112
|
|
|
* @return Validator |
|
113
|
|
|
* @throws DuplicateEntryException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function addValidator($name, $className) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->checkDefinitionFreezeState(); |
|
118
|
|
|
|
|
119
|
|
|
if ($this->hasValidator($name)) { |
|
120
|
|
|
throw DuplicateEntryException::fieldValidatorAlreadyAdded($name, $this->getName()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** @var Validator $validator */ |
|
124
|
|
|
$validator = GeneralUtility::makeInstance(Validator::class, $name, $className); |
|
125
|
|
|
$validator->attachParent($this); |
|
126
|
|
|
|
|
127
|
|
|
$this->validation[$name] = $validator; |
|
128
|
|
|
|
|
129
|
|
|
return $validator; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return Behaviour[] |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getBehaviours() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->behaviours; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
public function hasBehaviour($name) |
|
145
|
|
|
{ |
|
146
|
|
|
return true === isset($this->behaviours[$name]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $name |
|
151
|
|
|
* @return Behaviour |
|
152
|
|
|
* @throws EntryNotFoundException |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getBehaviour($name) |
|
155
|
|
|
{ |
|
156
|
|
|
if (false === $this->hasBehaviour($name)) { |
|
157
|
|
|
throw EntryNotFoundException::behaviourNotFound($name); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $this->behaviours[$name]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $name |
|
165
|
|
|
* @param string $className |
|
166
|
|
|
* @return Behaviour |
|
167
|
|
|
* @throws DuplicateEntryException |
|
168
|
|
|
*/ |
|
169
|
|
|
public function addBehaviour($name, $className) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->checkDefinitionFreezeState(); |
|
172
|
|
|
|
|
173
|
|
|
if ($this->hasBehaviour($name)) { |
|
174
|
|
|
throw DuplicateEntryException::fieldBehaviourAlreadyAdded($name, $this); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** @var Behaviour $behaviour */ |
|
178
|
|
|
$behaviour = GeneralUtility::makeInstance(Behaviour::class, $name, $className); |
|
179
|
|
|
$behaviour->attachParent($this); |
|
180
|
|
|
|
|
181
|
|
|
$this->behaviours[$name] = $behaviour; |
|
182
|
|
|
|
|
183
|
|
|
return $behaviour; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return ActivationInterface |
|
188
|
|
|
* @throws SilentException |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getActivation() |
|
191
|
|
|
{ |
|
192
|
|
|
if (false === $this->hasActivation()) { |
|
193
|
|
|
throw SilentException::fieldHasNoActivation($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 FieldSettings |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getSettings() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->settings; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param DataPreProcessor $processor |
|
232
|
|
|
*/ |
|
233
|
|
|
public static function dataPreProcessor(DataPreProcessor $processor) |
|
234
|
|
|
{ |
|
235
|
|
|
$data = $processor->getData(); |
|
236
|
|
|
|
|
237
|
|
|
/* |
|
238
|
|
|
* Forcing the names of the validators and the behaviours: they are the |
|
239
|
|
|
* keys of the array entries. |
|
240
|
|
|
*/ |
|
241
|
|
|
self::forceNameForProperty($data, 'validation'); |
|
242
|
|
|
self::forceNameForProperty($data, 'behaviours'); |
|
243
|
|
|
|
|
244
|
|
|
$processor->setData($data); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|