1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Codeburner Framework. |
5
|
|
|
* |
6
|
|
|
* @author Alex Rohleder <[email protected]> |
7
|
|
|
* @copyright 2016 Alex Rohleder |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Codeburner\Router\Exceptions\Http; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* MethodNotAllowed |
15
|
|
|
* |
16
|
|
|
* @author Alex Rohleder <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class MethodNotAllowedException extends HttpExceptionAbstract |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
protected $requestedMethod; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
protected $requestedUri; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
protected $allowedMethods; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* MethodNotAllowedException constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $requestedMethod |
44
|
|
|
* @param string $requestedUri |
45
|
|
|
* @param string[] $allowedMethods |
46
|
|
|
*/ |
47
|
|
|
|
48
|
5 |
|
public function __construct($requestedMethod, $requestedUri, array $allowedMethods) |
49
|
|
|
{ |
50
|
5 |
|
$this->requestedMethod = $requestedMethod; |
51
|
5 |
|
$this->requestedUri = $requestedUri; |
52
|
5 |
|
$this->allowedMethods = $allowedMethods; |
53
|
5 |
|
$this->code = 405; |
54
|
5 |
|
$this->message = "Method Not Allowed"; |
55
|
5 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Verify if the given HTTP method is allowed for the request. |
59
|
|
|
* |
60
|
|
|
* @param string $method An HTTP method |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
|
64
|
1 |
|
public function can($method) |
65
|
|
|
{ |
66
|
1 |
|
return in_array(strtolower($method), $this->allowedMethods); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The HTTP specification requires that a 405 Method Not Allowed response include the |
71
|
|
|
* Allow: header to detail available methods for the requested resource. |
72
|
|
|
* |
73
|
|
|
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7 |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
|
77
|
1 |
|
public function allowed() |
78
|
|
|
{ |
79
|
1 |
|
return implode(', ', $this->allowedMethods); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|