Completed
Push — feature/improve-form-definitio... ( 5df9ba...6ebf3c )
by Romain
02:15
created

ValidatorService::addValueToMessage()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 5
nop 1
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\Service;
15
16
use Romm\Formz\Form\Definition\Field\Validation\Validator;
17
use Romm\Formz\Service\Traits\SelfInstantiateTrait;
18
use Romm\Formz\Validation\Validator\AbstractValidator as FormzAbstractValidator;
19
use TYPO3\CMS\Core\SingletonInterface;
20
use TYPO3\CMS\Core\Utility\ArrayUtility;
21
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
22
23
class ValidatorService implements SingletonInterface
24
{
25
    use SelfInstantiateTrait;
26
27
    /**
28
     * Contains all the data (options, messages) of every validator which was
29
     * analyzed by the function `getValidatorData()`.
30
     *
31
     * @var array
32
     */
33
    protected $validatorsData = [];
34
35
    /**
36
     * @param Validator $validator
37
     * @return bool
38
     */
39
    public function validatorAcceptsEmptyValues(Validator $validator)
40
    {
41
        $validatorData = $this->getValidatorData($validator);
42
43
        return (isset($validatorData['acceptsEmptyValues']))
44
            ? (bool)$validatorData['acceptsEmptyValues']
45
            : false;
46
    }
47
48
    /**
49
     * @param Validator $validator
50
     * @return array
51
     */
52
    public function getValidatorMessages(Validator $validator)
53
    {
54
        $validatorData = $this->getValidatorData($validator);
55
        $messages = (isset($validatorData['formzValidator']))
56
            ? $this->filterMessages(
57
                $validator,
58
                $validatorData['supportedMessages'],
59
                $validatorData['supportsAllMessages']
60
            )
61
            : [];
62
63
        return $messages;
64
    }
65
66
    /**
67
     * Will clone the data of the given validator class name. Please note that
68
     * it will use reflection to get the default value of the class properties.
69
     *
70
     * @param Validator $validator
71
     * @return array
72
     */
73
    protected function getValidatorData(Validator $validator)
74
    {
75
        $validatorClassName = $validator->getClassName();
76
77
        if (false === isset($this->validatorsData[$validatorClassName])) {
78
            $this->validatorsData[$validatorClassName] = [];
79
80
            if (in_array(AbstractValidator::class, class_parents($validatorClassName))) {
81
                $validatorReflection = new \ReflectionClass($validatorClassName);
82
                $validatorProperties = $validatorReflection->getDefaultProperties();
83
                unset($validatorReflection);
84
85
                $validatorData = [
86
                    'supportedOptions'   => $validatorProperties['supportedOptions'],
87
                    'acceptsEmptyValues' => $validatorProperties['acceptsEmptyValues']
88
                ];
89
90
                if (in_array(FormzAbstractValidator::class, class_parents($validatorClassName))) {
91
                    $validatorData['formzValidator'] = true;
92
                    $validatorData['supportedMessages'] = $validatorProperties['supportedMessages'];
93
                    $validatorData['supportsAllMessages'] = $validatorProperties['supportsAllMessages'];
94
                }
95
96
                $this->validatorsData[$validatorClassName] = $validatorData;
97
            }
98
        }
99
100
        return $this->validatorsData[$validatorClassName];
101
    }
102
103
    /**
104
     * Will return an array by considering the supported messages, and filling
105
     * the supported ones with the given values.
106
     *
107
     * @param Validator $validator
108
     * @param array     $supportedMessages
109
     * @param bool      $canCreateNewMessages
110
     * @return array
111
     */
112
    public function filterMessages(Validator $validator, array $supportedMessages, $canCreateNewMessages = false)
113
    {
114
        $messagesArray = [];
115
116
        foreach ($validator->getMessages() as $message) {
117
            $messagesArray[$message->getIdentifier()] = $message->toArray();
118
        }
119
120
        $this->addValueToMessage($messagesArray);
121
        $this->addValueToMessage($supportedMessages);
122
123
        $messagesResult = $supportedMessages;
124
125
        ArrayUtility::mergeRecursiveWithOverrule(
126
            $messagesResult,
127
            $messagesArray,
128
            (bool)$canCreateNewMessages
129
        );
130
131
        return $messagesResult;
132
    }
133
134
    /**
135
     * Adding the keys `value` and `extension` to the messages, only if it is
136
     * missing.
137
     *
138
     * @param array $array
139
     */
140
    private function addValueToMessage(array &$array)
141
    {
142
        foreach ($array as $key => $value) {
143
            if (false === isset($value['value'])) {
144
                $array[$key]['value'] = '';
145
            }
146
147
            if (false === isset($value['extension'])) {
148
                $array[$key]['extension'] = '';
149
            }
150
        }
151
    }
152
}
153