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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class VerifyJsonRequest |
13
|
|
|
* |
14
|
|
|
* @package Arcanedev\Support\Middleware |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class VerifyJsonRequest extends Middleware |
18
|
|
|
{ |
19
|
|
|
/* ------------------------------------------------------------------------------------------------ |
20
|
|
|
| Properties |
21
|
|
|
| ------------------------------------------------------------------------------------------------ |
22
|
|
|
*/ |
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']; |
28
|
|
|
|
29
|
|
|
/* ------------------------------------------------------------------------------------------------ |
30
|
|
|
| Main Functions |
31
|
|
|
| ------------------------------------------------------------------------------------------------ |
32
|
|
|
*/ |
33
|
|
|
/** |
34
|
|
|
* Handle an incoming request. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Http\Request $request |
37
|
|
|
* @param \Closure $next |
38
|
|
|
* @param string|array|null $methods |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
18 |
|
public function handle(Request $request, Closure $next, $methods = null) |
43
|
|
|
{ |
44
|
18 |
|
if ($this->isJsonRequestValid($request, $methods)) { |
45
|
12 |
|
return $next($request); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
return response()->json([ |
|
|
|
|
49
|
6 |
|
'status' => 'error', |
50
|
|
|
'code' => 400, |
51
|
|
|
'message' => 'Request must be json', |
52
|
6 |
|
], 400); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/* ------------------------------------------------------------------------------------------------ |
56
|
|
|
| Check Functions |
57
|
|
|
| ------------------------------------------------------------------------------------------------ |
58
|
|
|
*/ |
59
|
|
|
/** |
60
|
|
|
* Validate json Request. |
61
|
|
|
* |
62
|
|
|
* @param Request $request |
63
|
|
|
* @param string|array|null $methods |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
18 |
|
private function isJsonRequestValid(Request $request, $methods) |
68
|
|
|
{ |
69
|
18 |
|
$methods = $this->getMethods($methods); |
70
|
|
|
|
71
|
18 |
|
if ( ! in_array($request->method(), $methods)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
18 |
|
return $request->isJson(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get request methods. |
80
|
|
|
* |
81
|
|
|
* @param string|array|null $methods |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
18 |
|
private function getMethods($methods) |
86
|
|
|
{ |
87
|
18 |
|
$methods = $methods ?? $this->methods; |
88
|
|
|
|
89
|
18 |
|
if (is_string($methods)) |
90
|
12 |
|
$methods = (array) $methods; |
91
|
|
|
|
92
|
18 |
|
return is_array($methods) ? array_map('strtoupper', $methods) : []; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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: