This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AbstractValidator.php
186 lines (168 loc) 路 5.28 KB
/
AbstractValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
* 2017 Romain CANON <romain.hydrocanon@gmail.com>
*
* This file is part of the TYPO3 FormZ project.
* It is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, either
* version 3 of the License, or any later version.
*
* For the full copyright and license information, see:
* http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Romm\Formz\Validation\Validator;
use Romm\Formz\Error\Error;
use Romm\Formz\Error\Notice;
use Romm\Formz\Error\Warning;
use Romm\Formz\Exceptions\EntryNotFoundException;
use Romm\Formz\Form\FormInterface;
use Romm\Formz\Service\MessageService;
use Romm\Formz\Service\ValidatorService;
use Romm\Formz\Validation\DataObject\ValidatorDataObject;
abstract class AbstractValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
{
/**
* Fill with paths to JavaScript files containing validation code. They will
* be automatically imported when needed.
*
* @var array
*/
protected static $javaScriptValidationFiles = [];
/**
* List of supported messages, which are used whenever an error occurs.
* Can be overridden with TypoScript in the validator configuration.
*
* Example:
* $supportedMessages = [
* 'default' => [
* 'key' => 'path.to.my.message',
* 'extension' => 'extension_containing_message',
* 'value' => 'Some value' // Static value of the message, not recommended though.
* ]
* ]
*
* @var array
*/
protected $supportedMessages = [];
/**
* Set this to true if you want to be able to add any message you want.
*
* @var bool
*/
protected $supportsAllMessages = false;
/**
* Contains the original form instance.
*
* @var FormInterface
*/
protected $form;
/**
* Contains the merge of the supported messages and the TypoScript defined
* messages.
*
* @var array
*/
protected $messages = [];
/**
* @var ValidatorDataObject
*/
protected $dataObject;
/**
* Constructs the validator, sets validation options and messages.
*
* @param array $options Options for the validator.
* @param ValidatorDataObject $dataObject
*/
final public function __construct(array $options = [], ValidatorDataObject $dataObject)
{
parent::__construct($options);
$this->dataObject = $dataObject;
$this->form = $dataObject->getFormObject()->getForm();
$this->messages = ValidatorService::get()->filterMessages(
$this->dataObject->getValidator(),
$this->supportedMessages,
(bool)$this->supportsAllMessages
);
}
/**
* Creates a new validation error and adds it to the result.
*
* @param string $key
* @param int $code
* @param array $arguments
* @param string $title
*/
protected function addError($key, $code, array $arguments = [], $title = '')
{
$message = $this->addMessage(Error::class, $key, $code, $arguments, $title);
$this->result->addError($message);
}
/**
* Creates a new validation warning and adds it to the result.
*
* @param string $key
* @param int $code
* @param array $arguments
* @param string $title
*/
protected function addWarning($key, $code, array $arguments = [], $title = '')
{
$message = $this->addMessage(Warning::class, $key, $code, $arguments, $title);
$this->result->addWarning($message);
}
/**
* Creates a new validation notice and adds it to the result.
*
* @param string $key
* @param int $code
* @param array $arguments
* @param string $title
*/
protected function addNotice($key, $code, array $arguments = [], $title = '')
{
$message = $this->addMessage(Notice::class, $key, $code, $arguments, $title);
$this->result->addNotice($message);
}
/**
* @param string $type
* @param string $key
* @param string $code
* @param array $arguments
* @param string $title
* @return mixed
* @throws EntryNotFoundException
*/
private function addMessage($type, $key, $code, array $arguments, $title)
{
if (!isset($this->messages[$key])) {
throw EntryNotFoundException::errorKeyNotFoundForValidator($key, $this);
}
return new $type(
$this->getMessage($key, $arguments),
$code,
$this->dataObject->getValidator()->getName(),
$key,
[],
$title
);
}
/**
* This function should *always* be used when a message should be translated
* when an error occurs in the validation process.
*
* @param string $key The key of the message, usually "default".
* @param array $arguments Arguments given to the message.
* @return string
*/
private function getMessage($key, array $arguments = [])
{
return MessageService::get()->parseMessageArray($this->messages[$key], $arguments);
}
/**
* @return array
*/
public static function getJavaScriptValidationFiles()
{
return static::$javaScriptValidationFiles;
}
}