1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Support\Middleware; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Support\Http\Middleware; |
8
|
|
|
use Closure; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Http\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class VerifyJsonRequest |
14
|
|
|
* |
15
|
|
|
* @package Arcanedev\Support\Middleware |
16
|
|
|
* @author ARCANEDEV <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class VerifyJsonRequest extends Middleware |
19
|
|
|
{ |
20
|
|
|
/* ------------------------------------------------------------------------------------------------ |
21
|
|
|
| Properties |
22
|
|
|
| ------------------------------------------------------------------------------------------------ |
23
|
|
|
*/ |
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']; |
29
|
|
|
|
30
|
|
|
/* ------------------------------------------------------------------------------------------------ |
31
|
|
|
| Main Functions |
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) |
44
|
|
|
{ |
45
|
18 |
|
if ($this->isJsonRequestValid($request, $methods)) { |
46
|
12 |
|
return $next($request); |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
return response()->json([ |
|
|
|
|
50
|
6 |
|
'status' => 'error', |
51
|
|
|
'code' => Response::HTTP_BAD_REQUEST, |
52
|
|
|
'message' => 'Request must be JSON', |
53
|
6 |
|
], Response::HTTP_BAD_REQUEST); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* ------------------------------------------------------------------------------------------------ |
57
|
|
|
| Check Functions |
58
|
|
|
| ------------------------------------------------------------------------------------------------ |
59
|
|
|
*/ |
60
|
|
|
/** |
61
|
|
|
* Validate json Request. |
62
|
|
|
* |
63
|
|
|
* @param Request $request |
64
|
|
|
* @param string|array|null $methods |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
18 |
|
private function isJsonRequestValid(Request $request, $methods) |
69
|
|
|
{ |
70
|
18 |
|
$methods = $this->getMethods($methods); |
71
|
|
|
|
72
|
18 |
|
if ( ! in_array($request->method(), $methods)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
18 |
|
return $request->isJson(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get request methods. |
81
|
|
|
* |
82
|
|
|
* @param string|array|null $methods |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
18 |
|
private function getMethods($methods) |
87
|
|
|
{ |
88
|
18 |
|
$methods = $methods ?? $this->methods; |
89
|
|
|
|
90
|
18 |
|
if (is_string($methods)) |
91
|
12 |
|
$methods = (array) $methods; |
92
|
|
|
|
93
|
18 |
|
return is_array($methods) ? array_map('strtoupper', $methods) : []; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: