1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | */ |
||
5 | |||
6 | namespace Mvc5\Route\Match; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
7 | |||
8 | use Mvc5\Http\Error\MethodNotAllowed; |
||
9 | use Mvc5\Http\Request; |
||
10 | use Mvc5\Route\Route; |
||
11 | |||
12 | use function in_array; |
||
13 | |||
14 | use const Mvc5\{ HTTP_GET, HTTP_HEAD, METHOD }; |
||
15 | |||
16 | final class Method |
||
17 | { |
||
18 | /** |
||
19 | * |
||
20 | */ |
||
21 | use Plugin\Optional; |
||
22 | |||
23 | /** |
||
24 | * @param string $method |
||
25 | * @param array $methods |
||
26 | * @return bool |
||
27 | */ |
||
28 | 14 | protected function match(string $method, array $methods) : bool |
|
29 | { |
||
30 | 14 | return !$methods || in_array($method, $methods) || (HTTP_HEAD === $method && in_array(HTTP_GET, $methods)); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param Route $route |
||
35 | * @param Request $request |
||
36 | * @param callable $next |
||
37 | * @return MethodNotAllowed|Request|mixed |
||
38 | */ |
||
39 | 14 | function __invoke(Route $route, Request $request, callable $next) |
|
40 | { |
||
41 | 14 | return $this->match($request->method(), (array) $route->method()) ? $next($route, $request) : ( |
|
42 | 14 | $this->optional($route, METHOD) ? null : new MethodNotAllowed |
|
43 | ); |
||
44 | } |
||
45 | } |
||
46 |