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\Service\Traits\FacadeInstanceTrait; |
17
|
|
|
use Romm\Formz\Validation\Validator\AbstractValidator as FormzAbstractValidator; |
18
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
19
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; |
20
|
|
|
|
21
|
|
|
class ValidatorService implements SingletonInterface |
22
|
|
|
{ |
23
|
|
|
use FacadeInstanceTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Contains all the data (options, messages) of every validator which was |
27
|
|
|
* analyzed by the function `getValidatorData()`. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $validatorsData = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $validatorClassName |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function validatorAcceptsEmptyValues($validatorClassName) |
38
|
|
|
{ |
39
|
|
|
$validatorData = $this->getValidatorData($validatorClassName); |
40
|
|
|
|
41
|
|
|
return (isset($validatorData['acceptsEmptyValues'])) |
42
|
|
|
? (bool)$validatorData['acceptsEmptyValues'] |
43
|
|
|
: false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $validatorClassName |
48
|
|
|
* @param array $messages |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getValidatorMessages($validatorClassName, array $messages) |
52
|
|
|
{ |
53
|
|
|
$validatorData = $this->getValidatorData($validatorClassName); |
54
|
|
|
$messages = (isset($validatorData['formzValidator'])) |
55
|
|
|
? MessageService::get()->filterMessages( |
56
|
|
|
$messages, |
57
|
|
|
$validatorData['supportedMessages'], |
58
|
|
|
$validatorData['supportsAllMessages'] |
59
|
|
|
) |
60
|
|
|
: []; |
61
|
|
|
|
62
|
|
|
return $messages; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Will clone the data of the given validator class name. Please note that |
67
|
|
|
* it will use reflection to get the default value of the class properties. |
68
|
|
|
* |
69
|
|
|
* @param $validatorClassName |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function getValidatorData($validatorClassName) |
73
|
|
|
{ |
74
|
|
|
if (false === isset($this->validatorsData[$validatorClassName])) { |
75
|
|
|
$this->validatorsData[$validatorClassName] = []; |
76
|
|
|
|
77
|
|
|
if (in_array(AbstractValidator::class, class_parents($validatorClassName))) { |
78
|
|
|
$validatorReflection = new \ReflectionClass($validatorClassName); |
79
|
|
|
$validatorProperties = $validatorReflection->getDefaultProperties(); |
80
|
|
|
unset($validatorReflection); |
81
|
|
|
|
82
|
|
|
$validatorData = [ |
83
|
|
|
'supportedOptions' => $validatorProperties['supportedOptions'], |
84
|
|
|
'acceptsEmptyValues' => $validatorProperties['acceptsEmptyValues'] |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
if (in_array(FormzAbstractValidator::class, class_parents($validatorClassName))) { |
88
|
|
|
$validatorData['formzValidator'] = true; |
89
|
|
|
$validatorData['supportedMessages'] = $validatorProperties['supportedMessages']; |
90
|
|
|
$validatorData['supportsAllMessages'] = $validatorProperties['supportsAllMessages']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->validatorsData[$validatorClassName] = $validatorData; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->validatorsData[$validatorClassName]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|