| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Validation;
|
|
|
4 |
|
|
|
5 |
use Closure;
|
|
|
6 |
use Illuminate\Contracts\Container\Container;
|
|
|
7 |
use Illuminate\Contracts\Translation\Translator;
|
|
|
8 |
use Illuminate\Contracts\Validation\Factory as FactoryContract;
|
|
|
9 |
use Illuminate\Support\Str;
|
|
|
10 |
|
|
|
11 |
class Factory implements FactoryContract
|
|
|
12 |
{
|
|
|
13 |
/**
|
|
|
14 |
* The Translator implementation.
|
|
|
15 |
*
|
|
|
16 |
* @var \Illuminate\Contracts\Translation\Translator
|
|
|
17 |
*/
|
|
|
18 |
protected $translator;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* The Presence Verifier implementation.
|
|
|
22 |
*
|
|
|
23 |
* @var \Illuminate\Validation\PresenceVerifierInterface
|
|
|
24 |
*/
|
|
|
25 |
protected $verifier;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The IoC container instance.
|
|
|
29 |
*
|
|
|
30 |
* @var \Illuminate\Contracts\Container\Container|null
|
|
|
31 |
*/
|
|
|
32 |
protected $container;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* All of the custom validator extensions.
|
|
|
36 |
*
|
|
|
37 |
* @var array<string, \Closure|string>
|
|
|
38 |
*/
|
|
|
39 |
protected $extensions = [];
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* All of the custom implicit validator extensions.
|
|
|
43 |
*
|
|
|
44 |
* @var array<string, \Closure|string>
|
|
|
45 |
*/
|
|
|
46 |
protected $implicitExtensions = [];
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* All of the custom dependent validator extensions.
|
|
|
50 |
*
|
|
|
51 |
* @var array<string, \Closure|string>
|
|
|
52 |
*/
|
|
|
53 |
protected $dependentExtensions = [];
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* All of the custom validator message replacers.
|
|
|
57 |
*
|
|
|
58 |
* @var array<string, \Closure|string>
|
|
|
59 |
*/
|
|
|
60 |
protected $replacers = [];
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* All of the fallback messages for custom rules.
|
|
|
64 |
*
|
|
|
65 |
* @var array<string, string>
|
|
|
66 |
*/
|
|
|
67 |
protected $fallbackMessages = [];
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Indicates that unvalidated array keys should be excluded, even if the parent array was validated.
|
|
|
71 |
*
|
|
|
72 |
* @var bool
|
|
|
73 |
*/
|
|
|
74 |
protected $excludeUnvalidatedArrayKeys = true;
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* The Validator resolver instance.
|
|
|
78 |
*
|
|
|
79 |
* @var \Closure
|
|
|
80 |
*/
|
|
|
81 |
protected $resolver;
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Create a new Validator factory instance.
|
|
|
85 |
*
|
|
|
86 |
* @param \Illuminate\Contracts\Translation\Translator $translator
|
|
|
87 |
* @param \Illuminate\Contracts\Container\Container|null $container
|
|
|
88 |
* @return void
|
|
|
89 |
*/
|
|
|
90 |
public function __construct(Translator $translator, Container $container = null)
|
|
|
91 |
{
|
|
|
92 |
$this->container = $container;
|
|
|
93 |
$this->translator = $translator;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Create a new Validator instance.
|
|
|
98 |
*
|
|
|
99 |
* @param array $data
|
|
|
100 |
* @param array $rules
|
|
|
101 |
* @param array $messages
|
|
|
102 |
* @param array $customAttributes
|
|
|
103 |
* @return \Illuminate\Validation\Validator
|
|
|
104 |
*/
|
|
|
105 |
public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
|
|
|
106 |
{
|
|
|
107 |
$validator = $this->resolve(
|
|
|
108 |
$data, $rules, $messages, $customAttributes
|
|
|
109 |
);
|
|
|
110 |
|
|
|
111 |
// The presence verifier is responsible for checking the unique and exists data
|
|
|
112 |
// for the validator. It is behind an interface so that multiple versions of
|
|
|
113 |
// it may be written besides database. We'll inject it into the validator.
|
|
|
114 |
if (! is_null($this->verifier)) {
|
|
|
115 |
$validator->setPresenceVerifier($this->verifier);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// Next we'll set the IoC container instance of the validator, which is used to
|
|
|
119 |
// resolve out class based validator extensions. If it is not set then these
|
|
|
120 |
// types of extensions will not be possible on these validation instances.
|
|
|
121 |
if (! is_null($this->container)) {
|
|
|
122 |
$validator->setContainer($this->container);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$validator->excludeUnvalidatedArrayKeys = $this->excludeUnvalidatedArrayKeys;
|
|
|
126 |
|
|
|
127 |
$this->addExtensions($validator);
|
|
|
128 |
|
|
|
129 |
return $validator;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Validate the given data against the provided rules.
|
|
|
134 |
*
|
|
|
135 |
* @param array $data
|
|
|
136 |
* @param array $rules
|
|
|
137 |
* @param array $messages
|
|
|
138 |
* @param array $customAttributes
|
|
|
139 |
* @return array
|
|
|
140 |
*
|
|
|
141 |
* @throws \Illuminate\Validation\ValidationException
|
|
|
142 |
*/
|
|
|
143 |
public function validate(array $data, array $rules, array $messages = [], array $customAttributes = [])
|
|
|
144 |
{
|
|
|
145 |
return $this->make($data, $rules, $messages, $customAttributes)->validate();
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Resolve a new Validator instance.
|
|
|
150 |
*
|
|
|
151 |
* @param array $data
|
|
|
152 |
* @param array $rules
|
|
|
153 |
* @param array $messages
|
|
|
154 |
* @param array $customAttributes
|
|
|
155 |
* @return \Illuminate\Validation\Validator
|
|
|
156 |
*/
|
|
|
157 |
protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
|
|
|
158 |
{
|
|
|
159 |
if (is_null($this->resolver)) {
|
|
|
160 |
return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Add the extensions to a validator instance.
|
|
|
168 |
*
|
|
|
169 |
* @param \Illuminate\Validation\Validator $validator
|
|
|
170 |
* @return void
|
|
|
171 |
*/
|
|
|
172 |
protected function addExtensions(Validator $validator)
|
|
|
173 |
{
|
|
|
174 |
$validator->addExtensions($this->extensions);
|
|
|
175 |
|
|
|
176 |
// Next, we will add the implicit extensions, which are similar to the required
|
|
|
177 |
// and accepted rule in that they're run even if the attributes aren't in an
|
|
|
178 |
// array of data which is given to a validator instance via instantiation.
|
|
|
179 |
$validator->addImplicitExtensions($this->implicitExtensions);
|
|
|
180 |
|
|
|
181 |
$validator->addDependentExtensions($this->dependentExtensions);
|
|
|
182 |
|
|
|
183 |
$validator->addReplacers($this->replacers);
|
|
|
184 |
|
|
|
185 |
$validator->setFallbackMessages($this->fallbackMessages);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Register a custom validator extension.
|
|
|
190 |
*
|
|
|
191 |
* @param string $rule
|
|
|
192 |
* @param \Closure|string $extension
|
|
|
193 |
* @param string|null $message
|
|
|
194 |
* @return void
|
|
|
195 |
*/
|
|
|
196 |
public function extend($rule, $extension, $message = null)
|
|
|
197 |
{
|
|
|
198 |
$this->extensions[$rule] = $extension;
|
|
|
199 |
|
|
|
200 |
if ($message) {
|
|
|
201 |
$this->fallbackMessages[Str::snake($rule)] = $message;
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Register a custom implicit validator extension.
|
|
|
207 |
*
|
|
|
208 |
* @param string $rule
|
|
|
209 |
* @param \Closure|string $extension
|
|
|
210 |
* @param string|null $message
|
|
|
211 |
* @return void
|
|
|
212 |
*/
|
|
|
213 |
public function extendImplicit($rule, $extension, $message = null)
|
|
|
214 |
{
|
|
|
215 |
$this->implicitExtensions[$rule] = $extension;
|
|
|
216 |
|
|
|
217 |
if ($message) {
|
|
|
218 |
$this->fallbackMessages[Str::snake($rule)] = $message;
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Register a custom dependent validator extension.
|
|
|
224 |
*
|
|
|
225 |
* @param string $rule
|
|
|
226 |
* @param \Closure|string $extension
|
|
|
227 |
* @param string|null $message
|
|
|
228 |
* @return void
|
|
|
229 |
*/
|
|
|
230 |
public function extendDependent($rule, $extension, $message = null)
|
|
|
231 |
{
|
|
|
232 |
$this->dependentExtensions[$rule] = $extension;
|
|
|
233 |
|
|
|
234 |
if ($message) {
|
|
|
235 |
$this->fallbackMessages[Str::snake($rule)] = $message;
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Register a custom validator message replacer.
|
|
|
241 |
*
|
|
|
242 |
* @param string $rule
|
|
|
243 |
* @param \Closure|string $replacer
|
|
|
244 |
* @return void
|
|
|
245 |
*/
|
|
|
246 |
public function replacer($rule, $replacer)
|
|
|
247 |
{
|
|
|
248 |
$this->replacers[$rule] = $replacer;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Indicate that unvalidated array keys should be included in validated data when the parent array is validated.
|
|
|
253 |
*
|
|
|
254 |
* @return void
|
|
|
255 |
*/
|
|
|
256 |
public function includeUnvalidatedArrayKeys()
|
|
|
257 |
{
|
|
|
258 |
$this->excludeUnvalidatedArrayKeys = false;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Indicate that unvalidated array keys should be excluded from the validated data, even if the parent array was validated.
|
|
|
263 |
*
|
|
|
264 |
* @return void
|
|
|
265 |
*/
|
|
|
266 |
public function excludeUnvalidatedArrayKeys()
|
|
|
267 |
{
|
|
|
268 |
$this->excludeUnvalidatedArrayKeys = true;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Set the Validator instance resolver.
|
|
|
273 |
*
|
|
|
274 |
* @param \Closure $resolver
|
|
|
275 |
* @return void
|
|
|
276 |
*/
|
|
|
277 |
public function resolver(Closure $resolver)
|
|
|
278 |
{
|
|
|
279 |
$this->resolver = $resolver;
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Get the Translator implementation.
|
|
|
284 |
*
|
|
|
285 |
* @return \Illuminate\Contracts\Translation\Translator
|
|
|
286 |
*/
|
|
|
287 |
public function getTranslator()
|
|
|
288 |
{
|
|
|
289 |
return $this->translator;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* Get the Presence Verifier implementation.
|
|
|
294 |
*
|
|
|
295 |
* @return \Illuminate\Validation\PresenceVerifierInterface
|
|
|
296 |
*/
|
|
|
297 |
public function getPresenceVerifier()
|
|
|
298 |
{
|
|
|
299 |
return $this->verifier;
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Set the Presence Verifier implementation.
|
|
|
304 |
*
|
|
|
305 |
* @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
|
|
|
306 |
* @return void
|
|
|
307 |
*/
|
|
|
308 |
public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
|
|
|
309 |
{
|
|
|
310 |
$this->verifier = $presenceVerifier;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Get the container instance used by the validation factory.
|
|
|
315 |
*
|
|
|
316 |
* @return \Illuminate\Contracts\Container\Container|null
|
|
|
317 |
*/
|
|
|
318 |
public function getContainer()
|
|
|
319 |
{
|
|
|
320 |
return $this->container;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/**
|
|
|
324 |
* Set the container instance used by the validation factory.
|
|
|
325 |
*
|
|
|
326 |
* @param \Illuminate\Contracts\Container\Container $container
|
|
|
327 |
* @return $this
|
|
|
328 |
*/
|
|
|
329 |
public function setContainer(Container $container)
|
|
|
330 |
{
|
|
|
331 |
$this->container = $container;
|
|
|
332 |
|
|
|
333 |
return $this;
|
|
|
334 |
}
|
|
|
335 |
}
|