1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Validation;
|
4
|
|
5
|
use Illuminate\Contracts\Validation\UnauthorizedException;
|
6
|
use Illuminate\Contracts\Validation\ValidationException as ValidationExceptionContract;
|
7
|
|
8
|
/**
|
9
|
* Provides default implementation of ValidatesWhenResolved contract.
|
10
|
*/
|
11
|
trait ValidatesWhenResolvedTrait
|
12
|
{
|
13
|
/**
|
14
|
* Validate the class instance.
|
15
|
*
|
16
|
* @return void
|
17
|
*/
|
18
|
public function validate()
|
19
|
{
|
20
|
$instance = $this->getValidatorInstance();
|
21
|
|
22
|
if (! $this->passesAuthorization()) {
|
23
|
$this->failedAuthorization();
|
24
|
} elseif (! $instance->passes()) {
|
25
|
$this->failedValidation($instance);
|
26
|
}
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Get the validator instance for the request.
|
31
|
*
|
32
|
* @return \Illuminate\Validation\Validator
|
33
|
*/
|
34
|
protected function getValidatorInstance()
|
35
|
{
|
36
|
return $this->validator();
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Handle a failed validation attempt.
|
41
|
*
|
42
|
* @param \Illuminate\Validation\Validator $validator
|
43
|
* @return void
|
44
|
*
|
45
|
* @throws \Illuminate\Contracts\Validation\ValidationException
|
46
|
*/
|
47
|
protected function failedValidation(Validator $validator)
|
48
|
{
|
49
|
throw new ValidationExceptionContract($validator);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Determine if the request passes the authorization check.
|
54
|
*
|
55
|
* @return bool
|
56
|
*/
|
57
|
protected function passesAuthorization()
|
58
|
{
|
59
|
if (method_exists($this, 'authorize')) {
|
60
|
return $this->authorize();
|
61
|
}
|
62
|
|
63
|
return true;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Handle a failed authorization attempt.
|
68
|
*
|
69
|
* @return void
|
70
|
*
|
71
|
* @throws \Illuminate\Contracts\Validation\UnauthorizedException
|
72
|
*/
|
73
|
protected function failedAuthorization()
|
74
|
{
|
75
|
throw new UnauthorizedException;
|
76
|
}
|
77
|
}
|