1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Del\Form\Field;
|
4
|
|
|
|
5
|
|
|
use Del\Form\Collection\FilterCollection;
|
6
|
|
|
use Del\Form\Collection\ValidatorCollection;
|
7
|
|
|
use Del\Form\Filter\Adapter\FilterAdapterZf;
|
8
|
|
|
use Del\Form\Filter\FilterInterface;
|
9
|
|
|
use Del\Form\FormInterface;
|
10
|
|
|
use Del\Form\Renderer\Field\FieldRendererInterface;
|
11
|
|
|
use Del\Form\Renderer\Field\TextRender;
|
12
|
|
|
use Del\Form\Traits\HasAttributesTrait;
|
13
|
|
|
use Del\Form\Validator\NotEmpty;
|
14
|
|
|
use Del\Form\Validator\ValidatorInterface;
|
15
|
|
|
use Exception;
|
16
|
|
|
use Laminas\Filter\ToNull;
|
17
|
|
|
|
18
|
|
|
abstract class FieldAbstract implements FieldInterface
|
19
|
|
|
{
|
20
|
|
|
|
21
|
|
|
/** @var FormInterface[] $dynamicFormCollection */
|
22
|
|
|
private $dynamicFormCollection;
|
23
|
|
|
|
24
|
|
|
/** @var FilterCollection $filterCollection */
|
25
|
|
|
private $filterCollection;
|
26
|
|
|
|
27
|
|
|
/** @var ValidatorCollection $validatorCollection */
|
28
|
|
|
private $validatorCollection;
|
29
|
|
|
|
30
|
|
|
/** @var ValidatorCollection $validatorCollection */
|
31
|
|
|
private $transformer;
|
32
|
|
|
|
33
|
|
|
/** @var FieldRendererInterface $renderer */
|
34
|
|
|
private $renderer;
|
35
|
|
|
|
36
|
|
|
/** @var array $errorMessages */
|
37
|
|
|
private $errorMessages;
|
38
|
|
|
|
39
|
|
|
/** @var string $customErrorMessage */
|
40
|
|
|
private $customErrorMessage;
|
41
|
|
|
|
42
|
|
|
/** @var string $label */
|
43
|
|
|
private $label;
|
44
|
|
|
|
45
|
|
|
/** @var bool $required */
|
46
|
|
|
private $required;
|
47
|
|
|
|
48
|
|
|
use HasAttributesTrait;
|
49
|
|
|
|
50
|
|
|
/**
|
51
|
|
|
* @return string
|
52
|
|
|
*/
|
53
|
|
|
abstract public function getTag(): string;
|
54
|
|
|
|
55
|
|
|
abstract public function init();
|
56
|
|
|
|
57
|
65 |
|
public function __construct($name, $value = null)
|
58
|
|
|
{
|
59
|
65 |
|
$this->required = false;
|
60
|
65 |
|
$this->dynamicFormCollection = [];
|
61
|
65 |
|
$this->filterCollection = new FilterCollection();
|
62
|
65 |
|
$this->validatorCollection = new ValidatorCollection();
|
63
|
65 |
|
$this->renderer = new TextRender();
|
64
|
65 |
|
$this->setName($name);
|
65
|
65 |
|
$value === null ? null : $this->setValue($value);
|
66
|
65 |
|
$this->init();
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* @return string
|
71
|
|
|
*/
|
72
|
51 |
|
public function getName(): string
|
73
|
|
|
{
|
74
|
51 |
|
return $this->getAttribute('name');
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
/**
|
78
|
|
|
* @param string $name
|
79
|
|
|
*/
|
80
|
65 |
|
public function setName(string $name): void
|
81
|
|
|
{
|
82
|
65 |
|
$this->setAttribute('name', $name);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* @return string
|
87
|
|
|
*/
|
88
|
35 |
|
public function getId(): ?string
|
89
|
|
|
{
|
90
|
35 |
|
return $this->getAttribute('id');
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* @param string $id
|
95
|
|
|
*/
|
96
|
8 |
|
public function setId(string $id): void
|
97
|
|
|
{
|
98
|
8 |
|
$this->setAttribute('id', $id);
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* @return string
|
103
|
|
|
*/
|
104
|
1 |
|
public function getClass(): string
|
105
|
|
|
{
|
106
|
1 |
|
return $this->getAttribute('class') ?: 'form-control';
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* @param string $class
|
111
|
|
|
* @return FieldAbstract
|
112
|
|
|
*/
|
113
|
11 |
|
public function setClass(string $class): void
|
114
|
|
|
{
|
115
|
11 |
|
$this->setAttribute('class', $class);
|
116
|
|
|
}
|
117
|
|
|
|
118
|
50 |
|
public function getValue(): mixed
|
119
|
|
|
{
|
120
|
50 |
|
return $this->getAttribute('value');
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* @param string $value
|
125
|
|
|
*/
|
126
|
42 |
|
public function setValue($value): void
|
127
|
|
|
{
|
128
|
42 |
|
$this->setAttribute('value', $value);
|
129
|
42 |
|
$this->filterValue();
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
/**
|
133
|
|
|
* @param ValidatorInterface $validator
|
134
|
|
|
*/
|
135
|
26 |
|
public function addValidator(ValidatorInterface $validator): void
|
136
|
|
|
{
|
137
|
26 |
|
$this->validatorCollection->append($validator);
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
/**
|
141
|
|
|
* @return ValidatorCollection
|
142
|
|
|
*/
|
143
|
2 |
|
public function getValidators(): ValidatorCollection
|
144
|
|
|
{
|
145
|
2 |
|
return $this->validatorCollection;
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* @param FilterInterface $filter
|
150
|
|
|
*/
|
151
|
37 |
|
public function addFilter(FilterInterface $filter): void
|
152
|
|
|
{
|
153
|
37 |
|
$this->filterCollection->append($filter);
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* @param FilterInterface $transformer
|
158
|
|
|
*/
|
159
|
2 |
|
public function setTransformer(TransformerInterface $transformer): void
|
160
|
|
|
{
|
161
|
2 |
|
$this->transformer = $transformer;
|
|
|
|
|
162
|
|
|
}
|
163
|
|
|
|
164
|
|
|
/**
|
165
|
|
|
* @return TransformerInterface
|
166
|
|
|
*/
|
167
|
2 |
|
public function getTransformer(): TransformerInterface
|
168
|
|
|
{
|
169
|
2 |
|
return $this->transformer;
|
|
|
|
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
/**
|
173
|
|
|
* @return bool
|
174
|
|
|
*/
|
175
|
12 |
|
public function hasTransformer(): bool
|
176
|
|
|
{
|
177
|
12 |
|
return $this->transformer instanceof TransformerInterface;
|
178
|
|
|
}
|
179
|
|
|
|
180
|
|
|
/**
|
181
|
|
|
* @return FilterCollection
|
182
|
|
|
*/
|
183
|
1 |
|
public function getFilters(): FilterCollection
|
184
|
|
|
{
|
185
|
1 |
|
return $this->filterCollection;
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
/**
|
189
|
|
|
* Runs the checkForErrors method for each field, which adds to errorMessages if invalid
|
190
|
|
|
*
|
191
|
|
|
* @return bool
|
192
|
|
|
*/
|
193
|
43 |
|
public function isValid(): bool
|
194
|
|
|
{
|
195
|
43 |
|
$this->errorMessages = [];
|
196
|
43 |
|
$this->validatorCollection->rewind();
|
197
|
|
|
|
198
|
43 |
|
while ($this->validatorCollection->valid()) {
|
199
|
23 |
|
$this->checkForErrors($this->validatorCollection->current());
|
200
|
23 |
|
$this->validatorCollection->next();
|
201
|
|
|
}
|
202
|
|
|
|
203
|
43 |
|
$count = \count($this->errorMessages);
|
204
|
|
|
|
205
|
43 |
|
return $count == 0;
|
206
|
|
|
}
|
207
|
|
|
|
208
|
|
|
/**
|
209
|
|
|
* @param ValidatorInterface $validator
|
210
|
|
|
*/
|
211
|
23 |
|
private function checkForErrors(ValidatorInterface $validator): void
|
212
|
|
|
{
|
213
|
23 |
|
$value = $this->getValue();
|
214
|
|
|
|
215
|
23 |
|
if ((!$validator->isValid($value)) && $this->isRequired()) {
|
216
|
18 |
|
$this->errorMessages = array_merge($this->errorMessages, $validator->getMessages());
|
217
|
|
|
}
|
218
|
|
|
}
|
219
|
|
|
|
220
|
|
|
/**
|
221
|
|
|
* @throws Exception
|
222
|
|
|
*/
|
223
|
42 |
|
private function filterValue(): void
|
224
|
|
|
{
|
225
|
42 |
|
$value = $this->getAttribute('value');
|
226
|
42 |
|
$this->filterCollection->rewind();
|
227
|
|
|
|
228
|
42 |
|
while ($this->filterCollection->valid()) {
|
229
|
16 |
|
$value = $this->filterCollection->current()->filter($value);
|
230
|
16 |
|
$this->filterCollection->next();
|
231
|
|
|
}
|
232
|
|
|
|
233
|
42 |
|
$this->filterCollection->rewind();
|
234
|
42 |
|
$this->setAttribute('value', $value);
|
235
|
|
|
}
|
236
|
|
|
|
237
|
|
|
/**
|
238
|
|
|
* @return array
|
239
|
|
|
*/
|
240
|
14 |
|
public function getMessages(): array
|
241
|
|
|
{
|
242
|
14 |
|
return isset($this->customErrorMessage) ? [$this->customErrorMessage] : array_values($this->errorMessages);
|
243
|
|
|
}
|
244
|
|
|
|
245
|
|
|
/**
|
246
|
|
|
* @return string
|
247
|
|
|
*/
|
248
|
34 |
|
public function getLabel(): ?string
|
249
|
|
|
{
|
250
|
34 |
|
return $this->label;
|
251
|
|
|
}
|
252
|
|
|
|
253
|
|
|
/**
|
254
|
|
|
* @param string $label
|
255
|
|
|
*/
|
256
|
20 |
|
public function setLabel(string $label): void
|
257
|
|
|
{
|
258
|
20 |
|
$this->label = $label;
|
259
|
|
|
}
|
260
|
|
|
|
261
|
|
|
/**
|
262
|
|
|
* @param string $message
|
263
|
|
|
*/
|
264
|
6 |
|
public function setCustomErrorMessage(string $message): void
|
265
|
|
|
{
|
266
|
6 |
|
$this->customErrorMessage = $message;
|
267
|
|
|
}
|
268
|
|
|
|
269
|
|
|
/**
|
270
|
|
|
* @return bool
|
271
|
|
|
*/
|
272
|
4 |
|
public function hasCustomErrorMessage(): bool
|
273
|
|
|
{
|
274
|
4 |
|
return $this->customErrorMessage != null;
|
275
|
|
|
}
|
276
|
|
|
|
277
|
|
|
/**
|
278
|
|
|
* @return string
|
279
|
|
|
*/
|
280
|
2 |
|
public function getCustomErrorMessage(): string
|
281
|
|
|
{
|
282
|
2 |
|
return $this->customErrorMessage;
|
283
|
|
|
}
|
284
|
|
|
|
285
|
|
|
/**
|
286
|
|
|
* @return FieldRendererInterface
|
287
|
|
|
*/
|
288
|
35 |
|
public function getRenderer(): FieldRendererInterface
|
289
|
|
|
{
|
290
|
35 |
|
return $this->renderer;
|
291
|
|
|
}
|
292
|
|
|
|
293
|
|
|
/**
|
294
|
|
|
* @param FieldRendererInterface $renderer
|
295
|
|
|
*/
|
296
|
41 |
|
public function setRenderer(FieldRendererInterface $renderer): void
|
297
|
|
|
{
|
298
|
41 |
|
$this->renderer = $renderer;
|
299
|
|
|
}
|
300
|
|
|
|
301
|
|
|
/**
|
302
|
|
|
* If a field is required then it must have a value
|
303
|
|
|
* We add a not empty validator
|
304
|
|
|
*
|
305
|
|
|
* @return boolean
|
306
|
|
|
*/
|
307
|
46 |
|
public function isRequired(): bool
|
308
|
|
|
{
|
309
|
46 |
|
return $this->required;
|
310
|
|
|
}
|
311
|
|
|
|
312
|
|
|
/**
|
313
|
|
|
* @param boolean $required
|
314
|
|
|
*/
|
315
|
23 |
|
public function setRequired(bool $required): void
|
316
|
|
|
{
|
317
|
23 |
|
$required ? $this->addNotEmptyValidator() : $this->removeNotEmptyValidator();
|
318
|
23 |
|
$this->required = $required;
|
319
|
|
|
}
|
320
|
|
|
|
321
|
|
|
/**
|
322
|
|
|
* adds not empty validator
|
323
|
|
|
*/
|
324
|
23 |
|
private function addNotEmptyValidator(): void
|
325
|
|
|
{
|
326
|
23 |
|
$notEmpty = new NotEmpty();
|
327
|
23 |
|
$this->addValidator($notEmpty);
|
328
|
|
|
}
|
329
|
|
|
|
330
|
|
|
/**
|
331
|
|
|
* removes not empty validator
|
332
|
|
|
*/
|
333
|
2 |
|
private function removeNotEmptyValidator(): void
|
334
|
|
|
{
|
335
|
2 |
|
$this->validatorCollection->rewind();
|
336
|
|
|
|
337
|
2 |
|
while ($this->validatorCollection->valid()) {
|
338
|
2 |
|
$validator = $this->validatorCollection->current();
|
339
|
2 |
|
$validator instanceof NotEmpty
|
340
|
2 |
|
? $this->validatorCollection->offsetUnset($this->validatorCollection->key())
|
|
|
|
|
341
|
1 |
|
: null;
|
342
|
2 |
|
$this->validatorCollection->next();
|
343
|
|
|
}
|
344
|
|
|
}
|
345
|
|
|
|
346
|
|
|
/**
|
347
|
|
|
* @param FormInterface $form
|
348
|
|
|
* @param string $triggerValue
|
349
|
|
|
*/
|
350
|
3 |
|
public function addDynamicForm(FormInterface $form, string $triggerValue): void
|
351
|
|
|
{
|
352
|
3 |
|
$this->dynamicFormCollection[$triggerValue] = $form;
|
353
|
|
|
}
|
354
|
|
|
|
355
|
|
|
/**
|
356
|
|
|
* @return bool
|
357
|
|
|
*/
|
358
|
48 |
|
public function hasDynamicForms(): bool
|
359
|
|
|
{
|
360
|
48 |
|
return count($this->dynamicFormCollection) > 0;
|
361
|
|
|
}
|
362
|
|
|
|
363
|
|
|
/**
|
364
|
|
|
* @return FormInterface[]
|
365
|
|
|
* @throws Exception
|
366
|
|
|
*/
|
367
|
4 |
|
public function getDynamicForms(): array
|
368
|
|
|
{
|
369
|
4 |
|
if (!$this->hasDynamicForms()) {
|
370
|
1 |
|
throw new Exception('No dynamic form for this value - Did you check hasDynamicForm() ?');
|
371
|
|
|
}
|
372
|
3 |
|
return $this->dynamicFormCollection;
|
373
|
|
|
}
|
374
|
|
|
}
|
375
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..