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