1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Meraki\Route; |
5
|
|
|
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface as ServerRequest; |
7
|
|
|
use Meraki\Route\Rule; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents the result of a route attempting a match against the incoming request. |
11
|
|
|
* |
12
|
|
|
* @author Nathan Bishop <[email protected]> (https://nathanbishop.name) |
13
|
|
|
* @copyright 2019 Nathan Bishop |
14
|
|
|
* @license The MIT license. |
15
|
|
|
*/ |
16
|
|
|
final class MatchResult |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @const integer [FOUND description] |
20
|
|
|
*/ |
21
|
|
|
const MATCHED = 200; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @const integer [NOT_FOUND description] |
25
|
|
|
*/ |
26
|
|
|
const REQUEST_TARGET_NOT_MATCHED = 404; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @const integer [METHOD_NOT_ALLOWED description] |
30
|
|
|
*/ |
31
|
|
|
const METHOD_NOT_MATCHED = 405; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var integer [$type description] |
35
|
|
|
*/ |
36
|
|
|
private $type; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ServerRequest [$request description] |
40
|
|
|
*/ |
41
|
|
|
private $request; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Rule [$matchedRule description] |
45
|
|
|
*/ |
46
|
|
|
private $matchedRule; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string[] [$allowedMethods description] |
50
|
|
|
*/ |
51
|
|
|
private $allowedMethods; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* [__construct description] |
55
|
|
|
* |
56
|
|
|
* @param integer $type [description] |
57
|
|
|
* @param ServerRequest $request [description] |
58
|
|
|
*/ |
59
|
|
|
private function __construct(int $type, ServerRequest $request) |
60
|
|
|
{ |
61
|
|
|
$this->type = $type; |
62
|
|
|
$this->request = $request; |
63
|
|
|
$this->allowedMethods = []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* [getType description] |
68
|
|
|
* |
69
|
|
|
* @return integer [description] |
70
|
|
|
*/ |
71
|
|
|
public function getType(): int |
72
|
|
|
{ |
73
|
|
|
return $this->type; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* [getRequest description] |
78
|
|
|
* |
79
|
|
|
* @return ServerRequest [description] |
80
|
|
|
*/ |
81
|
|
|
public function getRequest(): ServerRequest |
82
|
|
|
{ |
83
|
|
|
return $this->request; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* [getMatchedRule description] |
88
|
|
|
* |
89
|
|
|
* @return Rule|null [description] |
90
|
|
|
*/ |
91
|
|
|
public function getMatchedRule(): ?Rule |
92
|
|
|
{ |
93
|
|
|
return $this->matchedRule; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* [getAllowedMethods description] |
98
|
|
|
* |
99
|
|
|
* @return string[] [description] |
100
|
|
|
*/ |
101
|
|
|
public function getAllowedMethods(): array |
102
|
|
|
{ |
103
|
|
|
return $this->allowedMethods; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* [isSuccessful description] |
108
|
|
|
* |
109
|
|
|
* @return boolean [description] |
110
|
|
|
*/ |
111
|
|
|
public function isSuccessful(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->type === self::MATCHED; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* [isFailure description] |
118
|
|
|
* |
119
|
|
|
* @return boolean [description] |
120
|
|
|
*/ |
121
|
|
|
public function isFailure(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->type === self::REQUEST_TARGET_NOT_MATCHED |
124
|
|
|
|| $this->type === self::METHOD_NOT_MATCHED; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* [success description] |
129
|
|
|
* |
130
|
|
|
* @param ServerRequest $request [description] |
131
|
|
|
* @param Rule $rule [description] |
132
|
|
|
* @return self [description] |
133
|
|
|
*/ |
134
|
|
|
public static function matched(ServerRequest $request, Rule $rule): self |
135
|
|
|
{ |
136
|
|
|
$result = new self(self::MATCHED, $request); |
137
|
|
|
$result->matchedRule = $rule; |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* [notFound description] |
144
|
|
|
* |
145
|
|
|
* @param ServerRequest $request [description] |
146
|
|
|
* @return self [description] |
147
|
|
|
*/ |
148
|
|
|
public static function requestTargetNotMatched(ServerRequest $request): self |
149
|
|
|
{ |
150
|
|
|
return new self(self::REQUEST_TARGET_NOT_MATCHED, $request); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* [methodNotAllowed description] |
155
|
|
|
* |
156
|
|
|
* @param ServerRequest $request [description] |
157
|
|
|
* @param string[] $allowedMethods [description] |
158
|
|
|
* @return self [description] |
159
|
|
|
*/ |
160
|
|
|
public static function methodNotMatched(ServerRequest $request, array $allowedMethods): self |
161
|
|
|
{ |
162
|
|
|
$result = new self(self::METHOD_NOT_MATCHED, $request); |
163
|
|
|
$result->allowedMethods = $allowedMethods; |
164
|
|
|
|
165
|
|
|
return $result; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|