1 | <?php |
||
15 | class VerifyJsonRequest |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** |
||
23 | * Supported request method verbs. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']; |
||
28 | |||
29 | /* ----------------------------------------------------------------- |
||
30 | | Main Methods |
||
31 | | ----------------------------------------------------------------- |
||
32 | */ |
||
33 | |||
34 | /** |
||
35 | * Handle an incoming request. |
||
36 | * |
||
37 | * @param \Illuminate\Http\Request $request |
||
38 | * @param \Closure $next |
||
39 | * @param string|array|null $methods |
||
40 | * |
||
41 | * @return mixed |
||
42 | */ |
||
43 | 18 | public function handle(Request $request, Closure $next, $methods = null) |
|
51 | |||
52 | /* ----------------------------------------------------------------- |
||
53 | | Check Methods |
||
54 | | ----------------------------------------------------------------- |
||
55 | */ |
||
56 | |||
57 | /** |
||
58 | * Validate json Request. |
||
59 | * |
||
60 | * @param \Illuminate\Http\Request $request |
||
61 | * @param string|array|null $methods |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | 18 | protected function isJsonRequestValid(Request $request, $methods) |
|
75 | |||
76 | /* ----------------------------------------------------------------- |
||
77 | | Other Methods |
||
78 | | ----------------------------------------------------------------- |
||
79 | */ |
||
80 | |||
81 | /** |
||
82 | * Get the error as json response. |
||
83 | * |
||
84 | * @return \Illuminate\Http\JsonResponse |
||
85 | */ |
||
86 | 6 | protected function jsonErrorResponse() |
|
87 | { |
||
88 | 2 | $data = [ |
|
89 | 6 | 'status' => 'error', |
|
90 | 6 | 'code' => $statusCode = Response::HTTP_BAD_REQUEST, |
|
91 | 6 | 'message' => 'Request must be JSON', |
|
92 | ]; |
||
93 | |||
94 | 6 | return new JsonResponse($data, $statusCode); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get request methods. |
||
99 | * |
||
100 | * @param string|array|null $methods |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | 18 | protected function getMethods($methods): array |
|
114 | } |
||
115 |