|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yeelight\Http\Controllers\Api\Foundation\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Dingo\Api\Exception\ResourceException; |
|
6
|
|
|
use Illuminate\Contracts\Validation\Factory; |
|
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
8
|
|
|
use Illuminate\Http\JsonResponse; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Routing\UrlGenerator; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait ValidatesRequests |
|
14
|
|
|
* |
|
15
|
|
|
* @category Yeelight |
|
16
|
|
|
* |
|
17
|
|
|
* @package Yeelight\Http\Controllers\Api\Foundation\Validation |
|
18
|
|
|
* |
|
19
|
|
|
* @author Sheldon Lee <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
22
|
|
|
* |
|
23
|
|
|
* @link https://www.yeelight.com |
|
24
|
|
|
*/ |
|
25
|
|
|
trait ValidatesRequests |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* The default error bag. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $validatesRequestErrorBag; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Run the validation routine against the given validator. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Illuminate\Contracts\Validation\Validator|array $validator Validator |
|
38
|
|
|
* @param \Illuminate\Http\Request|null $request Request |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function validateWith($validator, Request $request = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$request = $request ?: app('request'); |
|
45
|
|
|
|
|
46
|
|
|
if (is_array($validator)) { |
|
47
|
|
|
$validator = $this->getValidationFactory()->make($request->all(), $validator); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($validator->fails()) { |
|
51
|
|
|
$this->throwValidationException($request, $validator); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Validate the given request with the given rules. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \Illuminate\Http\Request $request Request |
|
59
|
|
|
* @param array $rules rules |
|
60
|
|
|
* @param array $messages messages |
|
61
|
|
|
* @param array $customAttributes customAttributes |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = []) |
|
66
|
|
|
{ |
|
67
|
|
|
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); |
|
68
|
|
|
|
|
69
|
|
|
if ($validator->fails()) { |
|
70
|
|
|
$this->throwValidationException($request, $validator); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Validate the given request with the given rules. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \Illuminate\Http\Request $request Request |
|
78
|
|
|
* @param array $data data |
|
79
|
|
|
* @param array $rules rules |
|
80
|
|
|
* @param array $messages messages |
|
81
|
|
|
* @param array $customAttributes customAttributes |
|
82
|
|
|
*/ |
|
83
|
|
|
public function validateData(Request $request, array $data, array $rules, array $messages = [], array $customAttributes = []) |
|
84
|
|
|
{ |
|
85
|
|
|
$validator = $this->getValidationFactory()->make($data, $rules, $messages, $customAttributes); |
|
86
|
|
|
|
|
87
|
|
|
if ($validator->fails()) { |
|
88
|
|
|
$this->throwValidationException($request, $validator); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Validate the given request with the given rules. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $errorBag errorBag |
|
96
|
|
|
* @param \Illuminate\Http\Request $request Request |
|
97
|
|
|
* @param array $rules rules |
|
98
|
|
|
* @param array $messages messages |
|
99
|
|
|
* @param array $customAttributes customAttributes |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Illuminate\Foundation\Validation\ValidationException |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = []) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->withErrorBag($errorBag, function () use ($request, $rules, $messages, $customAttributes) { |
|
108
|
|
|
$this->validate($request, $rules, $messages, $customAttributes); |
|
109
|
|
|
}); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Throw the failed validation exception. |
|
114
|
|
|
* |
|
115
|
|
|
* @param \Illuminate\Http\Request $request Request |
|
116
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator Validator |
|
117
|
|
|
* |
|
118
|
|
|
* @throws \Illuminate\Foundation\Validation\ValidationException |
|
119
|
|
|
* |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function throwValidationException(Request $request, $validator) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
throw new ResourceException($validator->errors()->first(), $validator->errors()); |
|
125
|
|
|
// throw new ValidationException($validator, $this->buildFailedValidationResponse( |
|
126
|
|
|
// $request, $this->formatValidationErrors($validator) |
|
127
|
|
|
// )); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Create the response for when a request fails validation. |
|
132
|
|
|
* |
|
133
|
|
|
* @param \Illuminate\Http\Request $request Request |
|
134
|
|
|
* @param array $errors errors |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Illuminate\Http\Response |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function buildFailedValidationResponse(Request $request, array $errors) |
|
139
|
|
|
{ |
|
140
|
|
|
if (($request->ajax() && !$request->pjax()) || $request->wantsJson()) { |
|
141
|
|
|
return new JsonResponse($errors, 422); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return redirect()->to($this->getRedirectUrl()) |
|
|
|
|
|
|
145
|
|
|
->withInput($request->input()) |
|
146
|
|
|
->withErrors($errors, $this->errorBag()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Format the validation errors to be returned. |
|
151
|
|
|
* |
|
152
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator Validator |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function formatValidationErrors(Validator $validator) |
|
157
|
|
|
{ |
|
158
|
|
|
return $validator->errors()->getMessages(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get the URL we should redirect to. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getRedirectUrl() |
|
167
|
|
|
{ |
|
168
|
|
|
return app(UrlGenerator::class)->previous(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Get a validation factory instance. |
|
173
|
|
|
* |
|
174
|
|
|
* @return \Illuminate\Contracts\Validation\Factory |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function getValidationFactory() |
|
177
|
|
|
{ |
|
178
|
|
|
return app(Factory::class); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Execute a Closure within with a given error bag set as the default bag. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $errorBag errorBag |
|
185
|
|
|
* @param callable $callback callback |
|
186
|
|
|
* |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function withErrorBag($errorBag, callable $callback) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->validatesRequestErrorBag = $errorBag; |
|
192
|
|
|
|
|
193
|
|
|
call_user_func($callback); |
|
194
|
|
|
|
|
195
|
|
|
$this->validatesRequestErrorBag = null; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get the key to be used for the view error bag. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function errorBag() |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->validatesRequestErrorBag ?: 'default'; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.