|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of questocat/lumen-request package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) questocat <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Questocat\LumenRequest; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Contracts\Container\Container; |
|
15
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory; |
|
16
|
|
|
use Illuminate\Contracts\Validation\ValidatesWhenResolved; |
|
17
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
18
|
|
|
use Illuminate\Http\JsonResponse; |
|
19
|
|
|
use Illuminate\Http\Request; |
|
20
|
|
|
use Illuminate\Validation\ValidatesWhenResolvedTrait; |
|
21
|
|
|
use Illuminate\Validation\ValidationException; |
|
22
|
|
|
|
|
23
|
|
|
abstract class InputRequest extends Request implements ValidatesWhenResolved |
|
24
|
|
|
{ |
|
25
|
|
|
use ValidatesWhenResolvedTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The container instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Illuminate\Contracts\Container\Container |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $container; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the validation rules that apply to the request. |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract public function rules(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get custom messages for validator errors. |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function messages() |
|
47
|
|
|
{ |
|
48
|
|
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get custom attributes for validator errors. |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function attributes() |
|
57
|
|
|
{ |
|
58
|
3 |
|
return []; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set the container implementation. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Illuminate\Contracts\Container\Container $container |
|
65
|
|
|
* |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function setContainer(Container $container) |
|
69
|
|
|
{ |
|
70
|
3 |
|
$this->container = $container; |
|
71
|
|
|
|
|
72
|
3 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the validator instance for the request. |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
|
79
|
|
|
*/ |
|
80
|
3 |
|
protected function getValidatorInstance() |
|
81
|
|
|
{ |
|
82
|
3 |
|
$factory = $this->container->make(ValidationFactory::class); |
|
83
|
|
|
|
|
84
|
3 |
|
if (method_exists($this, 'validator')) { |
|
85
|
|
|
$validator = $this->container->call([$this, 'validator'], compact('factory')); |
|
86
|
|
|
} else { |
|
87
|
3 |
|
$validator = $this->createDefaultValidator($factory); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
if (method_exists($this, 'withValidator')) { |
|
91
|
|
|
$this->withValidator($validator); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
return $validator; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Create the default validator instance. |
|
99
|
|
|
* |
|
100
|
|
|
* @param \Illuminate\Contracts\Validation\Factory $factory |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
|
103
|
|
|
*/ |
|
104
|
3 |
|
protected function createDefaultValidator(ValidationFactory $factory) |
|
105
|
|
|
{ |
|
106
|
3 |
|
return $factory->make( |
|
107
|
3 |
|
$this->validationData(), $this->container->call([$this, 'rules']), |
|
108
|
3 |
|
$this->messages(), $this->attributes() |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get data to be validated from the request. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
3 |
|
protected function validationData() |
|
118
|
|
|
{ |
|
119
|
3 |
|
return $this->all(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Handle a failed validation attempt. |
|
124
|
|
|
* |
|
125
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
|
128
|
|
|
*/ |
|
129
|
1 |
|
protected function failedValidation(Validator $validator) |
|
130
|
|
|
{ |
|
131
|
1 |
|
throw new ValidationException($validator, $this->buildFailedValidationResponse( |
|
132
|
1 |
|
$this->formatValidationErrors($validator) |
|
133
|
|
|
)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Build a failed validation response. |
|
138
|
|
|
* |
|
139
|
|
|
* @param $errors |
|
140
|
|
|
* |
|
141
|
|
|
* @return JsonResponse |
|
142
|
|
|
*/ |
|
143
|
1 |
|
protected function buildFailedValidationResponse($errors) |
|
144
|
|
|
{ |
|
145
|
1 |
|
return new JsonResponse($errors, 422); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Format the errors from the given Validator instance. |
|
150
|
|
|
* |
|
151
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
1 |
|
protected function formatValidationErrors(Validator $validator) |
|
156
|
|
|
{ |
|
157
|
1 |
|
return $validator->errors()->getMessages(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: