1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DERHANSEN\SfEventMgt\Validation\Validator; |
13
|
|
|
|
14
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration; |
15
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
16
|
|
|
use TYPO3\CMS\Extbase\Persistence\ObjectStorage; |
17
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; |
18
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* RegistrationFieldValidator |
22
|
|
|
*/ |
23
|
|
|
class RegistrationFieldValidator extends AbstractValidator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Validates the additional fields of the given registration. |
27
|
|
|
* |
28
|
|
|
* If $registration->getEvent() is null, the registration does not contain any registration fields |
29
|
|
|
* |
30
|
|
|
* @param Registration $value |
31
|
|
|
*/ |
32
|
|
|
protected function isValid(mixed $value): void |
33
|
|
|
{ |
34
|
|
|
if ($value->getEvent() === null || ($value->getFieldValues()->count() === 0 && |
35
|
|
|
$value->getEvent()->getRegistrationFields()->count() === 0)) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @var Registration\Field $registrationField */ |
40
|
|
|
foreach ($value->getEvent()->getRegistrationFields() as $registrationField) { |
41
|
|
|
$this->validateField($registrationField, $value->getFieldValues()); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Validates the given registrationField |
47
|
|
|
*/ |
48
|
|
|
protected function validateField(Registration\Field $registrationField, ObjectStorage $fieldValues): void |
49
|
|
|
{ |
50
|
|
|
if (!$registrationField->getRequired()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$validator = $this->getNotEmptyValidator(); |
55
|
|
|
|
56
|
|
|
$fieldValue = $this->getFieldValue($registrationField, $fieldValues); |
57
|
|
|
$validationResult = $validator->validate($fieldValue); |
58
|
|
|
if ($validationResult->hasErrors()) { |
59
|
|
|
$result = false; |
|
|
|
|
60
|
|
|
foreach ($validationResult->getErrors() as $error) { |
61
|
|
|
$this->result->forProperty('fields.' . $registrationField->getUid())->addError($error); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getNotEmptyValidator(): NotEmptyValidator |
67
|
|
|
{ |
68
|
|
|
return GeneralUtility::makeInstance(NotEmptyValidator::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the value for the given registrationField from the given fieldValues |
73
|
|
|
* |
74
|
|
|
* @param Registration\Field $registrationField |
75
|
|
|
* @param ObjectStorage $fieldValues |
76
|
|
|
* @return string|array |
77
|
|
|
*/ |
78
|
|
|
protected function getFieldValue(Registration\Field $registrationField, ObjectStorage $fieldValues) |
79
|
|
|
{ |
80
|
|
|
$result = ''; |
81
|
|
|
/** @var Registration\FieldValue $fieldValue */ |
82
|
|
|
foreach ($fieldValues as $fieldValue) { |
83
|
|
|
if ($fieldValue->getField()->getUid() === $registrationField->getUid()) { |
|
|
|
|
84
|
|
|
$result = $fieldValue->getValue(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If field value is an array, then treat one single element with an empty string as an empty value |
89
|
|
|
if (is_array($result) && $result === [0 => '']) { |
90
|
|
|
$result = ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|