1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nord\Lumen\Core\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
trait CreatesHttpResponses |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param mixed $data |
13
|
|
|
* @param array $headers |
14
|
|
|
* |
15
|
|
|
* @return JsonResponse |
16
|
|
|
*/ |
17
|
|
|
private function okResponse($data = null, array $headers = []) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
return $this->successResponse($data, 200, $headers); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param mixed $data |
24
|
|
|
* @param array $headers |
25
|
|
|
* |
26
|
|
|
* @return JsonResponse |
27
|
|
|
*/ |
28
|
|
|
private function createdResponse($data = null, array $headers = []) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
return $this->successResponse($data, 201, $headers); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $message |
35
|
|
|
* @param array $metadata |
36
|
|
|
* @param array $headers |
37
|
|
|
* |
38
|
|
|
* @return JsonResponse |
39
|
|
|
*/ |
40
|
|
|
private function badRequestResponse($message, $metadata = [], array $headers = []) |
41
|
|
|
{ |
42
|
|
|
return $this->errorResponse($message, $metadata, 400, $headers); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $message |
47
|
|
|
* @param array $metadata |
48
|
|
|
* @param array $headers |
49
|
|
|
* |
50
|
|
|
* @throws HttpResponseException |
51
|
|
|
*/ |
52
|
|
|
private function throwBadRequest($message, $metadata = [], array $headers = []) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
throw new HttpResponseException($this->badRequestResponse($message, $metadata, $headers)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $message |
59
|
|
|
* @param array $metadata |
60
|
|
|
* @param array $headers |
61
|
|
|
* |
62
|
|
|
* @return JsonResponse |
63
|
|
|
*/ |
64
|
|
|
private function accessDeniedResponse($message, $metadata = [], array $headers = []) |
65
|
|
|
{ |
66
|
|
|
return $this->errorResponse($message, $metadata, 401, $headers); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $message |
71
|
|
|
* @param array $data |
72
|
|
|
* @param array $headers |
73
|
|
|
* |
74
|
|
|
* @throws HttpResponseException |
75
|
|
|
*/ |
76
|
|
|
private function throwAccessDenied($message, $data = [], array $headers = []) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
throw new HttpResponseException($this->accessDeniedResponse($message, $data, $headers)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $message |
83
|
|
|
* @param array $data |
84
|
|
|
* @param array $headers |
85
|
|
|
* |
86
|
|
|
* @return JsonResponse |
87
|
|
|
*/ |
88
|
|
|
private function forbiddenResponse($message, $data = [], array $headers = []) |
89
|
|
|
{ |
90
|
|
|
return $this->errorResponse($message, $data, 403, $headers); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $message |
95
|
|
|
* @param array $data |
96
|
|
|
* @param array $headers |
97
|
|
|
* |
98
|
|
|
* @throws HttpResponseException |
99
|
|
|
*/ |
100
|
|
|
private function throwForbidden($message, $data = [], array $headers = []) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
throw new HttpResponseException($this->forbiddenResponse($message, $data, $headers)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $message |
107
|
|
|
* @param array $data |
108
|
|
|
* @param array $headers |
109
|
|
|
* |
110
|
|
|
* @return JsonResponse |
111
|
|
|
*/ |
112
|
|
|
private function notFoundResponse($message, $data = [], array $headers = []) |
113
|
|
|
{ |
114
|
|
|
return $this->errorResponse($message, $data, 404, $headers); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $message |
119
|
|
|
* @param array $data |
120
|
|
|
* @param array $headers |
121
|
|
|
* |
122
|
|
|
* @throws HttpResponseException |
123
|
|
|
*/ |
124
|
|
|
private function throwNotFound($message, $data = [], array $headers = []) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
throw new HttpResponseException($this->notFoundResponse($message, $data, $headers)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $message |
131
|
|
|
* @param array $data |
132
|
|
|
* @param array $headers |
133
|
|
|
* |
134
|
|
|
* @return JsonResponse |
135
|
|
|
*/ |
136
|
|
|
private function unprocessableEntityResponse($message, $data = [], array $headers = []) |
137
|
|
|
{ |
138
|
|
|
return $this->errorResponse($message, $data, 422, $headers); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $message |
143
|
|
|
* @param array $data |
144
|
|
|
* @param array $headers |
145
|
|
|
* |
146
|
|
|
* @throws HttpResponseException |
147
|
|
|
*/ |
148
|
|
|
private function throwUnprocessableEntity($message, $data = [], array $headers = []) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
throw new HttpResponseException($this->unprocessableEntityResponse($message, $data, $headers)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $message |
155
|
|
|
* @param array $data |
156
|
|
|
* @param array $headers |
157
|
|
|
* |
158
|
|
|
* @return JsonResponse |
159
|
|
|
*/ |
160
|
|
|
private function fatalErrorResponse($message, $data = [], array $headers = []) |
161
|
|
|
{ |
162
|
|
|
return $this->errorResponse($message, $data, 500, $headers); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $message |
167
|
|
|
* @param array $data |
168
|
|
|
* @param array $headers |
169
|
|
|
* |
170
|
|
|
* @throws HttpResponseException |
171
|
|
|
*/ |
172
|
|
|
private function throwFatalError($message, $data = [], array $headers = []) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
throw new HttpResponseException($this->fatalErrorResponse($message, $data, $headers)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $message |
179
|
|
|
* @param array $errors |
180
|
|
|
* @param array $data |
181
|
|
|
* @param int $status |
182
|
|
|
* @param array $headers |
183
|
|
|
* |
184
|
|
|
* @return JsonResponse |
185
|
|
|
*/ |
186
|
|
|
private function validationFailedResponse( |
187
|
|
|
$message, |
188
|
|
|
array $errors, |
189
|
|
|
array $data, |
190
|
|
|
$status = 422, |
191
|
|
|
array $headers = [] |
192
|
|
|
) { |
193
|
|
|
return $this->errorResponse($message, array_merge($data, ['errors' => $errors]), $status, $headers); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $message |
198
|
|
|
* @param array $errors |
199
|
|
|
* @param array $data |
200
|
|
|
* @param int $status |
201
|
|
|
* @param array $headers |
202
|
|
|
* |
203
|
|
|
* @throws HttpResponseException |
204
|
|
|
*/ |
205
|
|
|
private function throwValidationFailed($message, array $errors, $data = [], $status = 422, array $headers = []) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
throw new HttpResponseException( |
208
|
|
|
$this->validationFailedResponse($message, $errors, $data, $status, $headers)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param array $data |
213
|
|
|
* @param int $status |
214
|
|
|
* @param array $headers |
215
|
|
|
* |
216
|
|
|
* @return JsonResponse |
217
|
|
|
*/ |
218
|
|
|
private function successResponse($data = null, $status = 200, array $headers = []) |
219
|
|
|
{ |
220
|
|
|
return new JsonResponse($data, $status, $headers); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $message |
225
|
|
|
* @param array $data |
226
|
|
|
* @param int $status |
227
|
|
|
* @param array $headers |
228
|
|
|
* |
229
|
|
|
* @return JsonResponse |
230
|
|
|
*/ |
231
|
|
|
private function errorResponse($message, $data = [], $status = 400, array $headers = []) |
232
|
|
|
{ |
233
|
|
|
return new JsonResponse(array_merge(['message' => $message], $data), $status, $headers); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param Request $request |
238
|
|
|
* @param string $key |
239
|
|
|
* |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
private function hasRequestParam(Request $request, $key) |
|
|
|
|
243
|
|
|
{ |
244
|
|
|
return $this->getRequestParam($request, $key) !== null; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param Request $request |
249
|
|
|
* @param string $key |
250
|
|
|
* @param mixed $default |
251
|
|
|
* |
252
|
|
|
* @return mixed |
253
|
|
|
*/ |
254
|
|
|
private function getRequestParam(Request $request, $key, $default = null) |
255
|
|
|
{ |
256
|
|
|
return array_get($request->all(), $key, $default); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|