1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Exception; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use RuntimeException; |
18
|
|
|
use Throwable; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* MethodNotAllowedException |
23
|
|
|
*/ |
24
|
|
|
class MethodNotAllowedException extends RuntimeException implements HttpExceptionInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Server Request instance |
29
|
|
|
* |
30
|
|
|
* @var ServerRequestInterface |
31
|
|
|
*/ |
32
|
|
|
protected $request; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Allowed HTTP methods |
36
|
|
|
* |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
protected $allowedMethods; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor of the class |
43
|
|
|
* |
44
|
|
|
* @param ServerRequestInterface $request |
45
|
|
|
* @param string[] $allowedMethods |
46
|
|
|
* @param int $code |
47
|
|
|
* @param null|Throwable $previous |
48
|
|
|
*/ |
49
|
3 |
|
public function __construct(ServerRequestInterface $request, array $allowedMethods, int $code = 0, Throwable $previous = null) |
50
|
|
|
{ |
51
|
3 |
|
$this->request = $request; |
52
|
3 |
|
$this->allowedMethods = $allowedMethods; |
53
|
|
|
|
54
|
3 |
|
parent::__construct('The requested resource is not available for the HTTP method', $code, $previous); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function getRequest() : ServerRequestInterface |
61
|
|
|
{ |
62
|
1 |
|
return $this->request; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets allowed HTTP methods |
67
|
|
|
* |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
2 |
|
public function getAllowedMethods() : array |
71
|
|
|
{ |
72
|
2 |
|
return $this->allowedMethods; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Gets allowed HTTP methods as a string |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
public function getAllowedMethodsAsString() : string |
81
|
|
|
{ |
82
|
1 |
|
return \implode(',', $this->allowedMethods); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|