Passed
Push — main ( a6632b...f84dcf )
by Torben
05:07 queued 02:24
created

isArrayWithEmptyValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Extbase\Persistence\ObjectStorage;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Persistence\ObjectStorage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Valida...dator\AbstractValidator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Valida...dator\NotEmptyValidator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * RegistrationFieldValidator
21
 */
22
class RegistrationFieldValidator extends AbstractValidator
23
{
24
    /**
25
     * Validates the additional fields of the given registration.
26
     *
27
     * If $registration->getEvent() is null, the registration does not contain any registration fields
28
     *
29
     * @param Registration $value
30
     */
31
    protected function isValid(mixed $value): void
32
    {
33
        if ($value->getEvent() === null || ($value->getFieldValues()->count() === 0 &&
34
            $value->getEvent()->getRegistrationFields()->count() === 0)) {
35
            return;
36
        }
37
38
        /** @var Registration\Field $registrationField */
39
        foreach ($value->getEvent()->getRegistrationFields() as $registrationField) {
40
            $this->validateField($registrationField, $value->getFieldValues());
41
        }
42
    }
43
44
    /**
45
     * Validates the given registrationField
46
     */
47
    protected function validateField(Registration\Field $registrationField, ObjectStorage $fieldValues): void
48
    {
49
        if (!$registrationField->getRequired()) {
50
            return;
51
        }
52
53
        $validator = $this->getNotEmptyValidator();
54
55
        $fieldValue = $this->getFieldValue($registrationField, $fieldValues);
56
        $validationResult = $validator->validate($fieldValue);
57
        if ($validationResult->hasErrors()) {
58
            foreach ($validationResult->getErrors() as $error) {
59
                $this->result->forProperty('fields.' . $registrationField->getUid())->addError($error);
60
            }
61
        }
62
    }
63
64
    protected function getNotEmptyValidator(): NotEmptyValidator
65
    {
66
        $validator = new NotEmptyValidator();
67
        $validator->setOptions([
68
            'nullMessage' => 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.required_field',
69
            'emptyMessage' => 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.required_field',
70
        ]);
71
72
        return $validator;
73
    }
74
75
    /**
76
     * Returns the value for the given registrationField from the given fieldValues
77
     *
78
     * @return string|array
79
     */
80
    protected function getFieldValue(Registration\Field $registrationField, ObjectStorage $fieldValues)
81
    {
82
        $result = '';
83
        /** @var Registration\FieldValue $fieldValue */
84
        foreach ($fieldValues as $fieldValue) {
85
            if ($fieldValue->getField()->getUid() === $registrationField->getUid()) {
86
                $result = $fieldValue->getValue();
87
            }
88
        }
89
90
        // If field value is an array, then return empty string if it contains only empty values
91
        if (is_array($result) && $this->isArrayWithEmptyValues($result)) {
92
            $result = '';
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Returns, if the given array contains only empty values
100
     */
101
    private function isArrayWithEmptyValues(array $array): bool
102
    {
103
        foreach ($array as $value) {
104
            if ($value !== '') {
105
                return false;
106
            }
107
        }
108
109
        return true;
110
    }
111
}
112