|
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; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Exception thrown when none route is matched, but a similar |
|
15
|
|
|
* route is found in another http method. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Alex Rohleder <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
class MethodNotAllowedException extends BadRouteException |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The HTTP method from request. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
public $requestedMethod; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The requested URi. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
public $requestedUri; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* All the allowed HTTP methods and routes for the request. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
public $allowedMethods; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Exception constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $requestedMethod The request HTTP method. |
|
51
|
|
|
* @param string $requestedUri The request URi. |
|
52
|
|
|
* @param array $allowedMethods All the allowed HTTP methods and routes for the request. |
|
53
|
|
|
* @param string $message The exception error message. |
|
54
|
|
|
* @param integer $code The exception error code. |
|
55
|
|
|
*/ |
|
56
|
|
|
|
|
57
|
5 |
|
public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$this->requestedMethod = $requestedMethod; |
|
60
|
5 |
|
$this->requestedUri = $requestedUri; |
|
61
|
5 |
|
$this->allowedMethods = $allowedMethods; |
|
62
|
5 |
|
parent::__construct($message, $code); |
|
63
|
5 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Verify if the given HTTP method is allowed for the request. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $method An HTTP method |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
1 |
|
public function can($method) |
|
73
|
|
|
{ |
|
74
|
1 |
|
return in_array(strtolower($method), $this->allowedMethods); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The HTTP specification requires that a 405 Method Not Allowed response include the |
|
79
|
|
|
* Allow: header to detail available methods for the requested resource. |
|
80
|
|
|
* |
|
81
|
|
|
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7 |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
|
|
85
|
1 |
|
public function allowed() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return implode(', ', $this->allowedMethods); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|