|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace ReRoute; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* @package ReRoute |
|
10
|
|
|
*/ |
|
11
|
|
|
class RequestContext { |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var |
|
16
|
|
|
*/ |
|
17
|
|
|
private $path; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var |
|
22
|
|
|
*/ |
|
23
|
|
|
private $method; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var |
|
28
|
|
|
*/ |
|
29
|
|
|
private $host; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var |
|
34
|
|
|
*/ |
|
35
|
|
|
private $scheme; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $parameters = []; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $method The HTTP method |
|
48
|
|
|
* @param string $host The HTTP host name |
|
49
|
|
|
* @param string $scheme The HTTP scheme |
|
50
|
|
|
* @param string $path The path |
|
51
|
|
|
* @param string $queryString The query string |
|
52
|
|
|
* |
|
53
|
|
|
* @api |
|
54
|
|
|
*/ |
|
55
|
105 |
|
public function __construct($method = 'GET', $host = 'localhost', $scheme = 'http', $path = '/', $queryString = '') { |
|
56
|
105 |
|
$this->setMethod($method); |
|
57
|
105 |
|
$this->setHost($host); |
|
58
|
105 |
|
$this->setScheme($scheme); |
|
59
|
105 |
|
$this->setPath($path); |
|
60
|
105 |
|
$this->setQueryString($queryString); |
|
61
|
105 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets the query string. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $queryString The query string (after "?") |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
* |
|
71
|
|
|
* @api |
|
72
|
|
|
*/ |
|
73
|
105 |
|
public function setQueryString($queryString) { |
|
74
|
|
|
// string cast to be fault-tolerant, accepting null |
|
75
|
105 |
|
parse_str($queryString, $this->parameters); |
|
76
|
|
|
|
|
77
|
105 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Gets the path info. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string The path info |
|
85
|
|
|
*/ |
|
86
|
93 |
|
public function getPath() { |
|
87
|
93 |
|
return $this->path; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Sets the path info. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $path The path info |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
105 |
|
public function setPath($path) { |
|
99
|
105 |
|
$this->path = $path; |
|
100
|
105 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Gets the HTTP method. |
|
106
|
|
|
* |
|
107
|
|
|
* The method is always an uppercased string. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string The HTTP method |
|
110
|
|
|
*/ |
|
111
|
27 |
|
public function getMethod() { |
|
112
|
27 |
|
return $this->method; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets the HTTP method. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $method The HTTP method |
|
120
|
|
|
* |
|
121
|
|
|
* @return $this |
|
122
|
|
|
* |
|
123
|
|
|
* @api |
|
124
|
|
|
*/ |
|
125
|
105 |
|
public function setMethod($method) { |
|
126
|
105 |
|
$this->method = strtolower($method); |
|
127
|
|
|
|
|
128
|
105 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Gets the HTTP host. |
|
134
|
|
|
* |
|
135
|
|
|
* The host is always lowercased because it must be treated case-insensitive. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string The HTTP host |
|
138
|
|
|
*/ |
|
139
|
87 |
|
public function getHost() { |
|
140
|
87 |
|
return $this->host; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Sets the HTTP host. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $host The HTTP host |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
* |
|
151
|
|
|
* @api |
|
152
|
|
|
*/ |
|
153
|
105 |
|
public function setHost($host) { |
|
154
|
105 |
|
$this->host = strtolower($host); |
|
155
|
|
|
|
|
156
|
105 |
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Gets the HTTP scheme. |
|
162
|
|
|
* |
|
163
|
|
|
* @return string The HTTP scheme |
|
164
|
|
|
*/ |
|
165
|
42 |
|
public function getScheme() { |
|
166
|
42 |
|
return $this->scheme; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Sets the HTTP scheme. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $scheme The HTTP scheme |
|
174
|
|
|
* |
|
175
|
|
|
* @return $this |
|
176
|
|
|
* |
|
177
|
|
|
* @api |
|
178
|
|
|
*/ |
|
179
|
105 |
|
public function setScheme($scheme) { |
|
180
|
105 |
|
$this->scheme = strtolower($scheme); |
|
181
|
|
|
|
|
182
|
105 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Gets the query string. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string The query string without the "?" |
|
190
|
|
|
*/ |
|
191
|
3 |
|
public function getQueryString() { |
|
192
|
3 |
|
return http_build_query($this->parameters); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns the parameters. |
|
198
|
|
|
* |
|
199
|
|
|
* @return array The parameters |
|
200
|
|
|
*/ |
|
201
|
6 |
|
public function getParameters() { |
|
202
|
6 |
|
return $this->parameters; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Sets the parameters. |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $parameters The parameters |
|
210
|
|
|
* |
|
211
|
|
|
* @return $this |
|
212
|
|
|
*/ |
|
213
|
3 |
|
public function setParameters(array $parameters) { |
|
214
|
3 |
|
$this->parameters = $parameters; |
|
215
|
3 |
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Gets a parameter value. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $name A parameter name |
|
223
|
|
|
* |
|
224
|
|
|
* @return mixed The parameter value or null if nonexistent |
|
225
|
|
|
*/ |
|
226
|
9 |
|
public function getParameter($name) { |
|
227
|
9 |
|
return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Checks if a parameter value is set for the given parameter. |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $name A parameter name |
|
235
|
|
|
* |
|
236
|
|
|
* @return bool True if the parameter value is set, false otherwise |
|
237
|
|
|
*/ |
|
238
|
3 |
|
public function hasParameter($name) { |
|
239
|
3 |
|
return array_key_exists($name, $this->parameters); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Sets a parameter value. |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $name A parameter name |
|
247
|
|
|
* @param mixed $parameter The parameter value |
|
248
|
|
|
* |
|
249
|
|
|
* @return $this |
|
250
|
|
|
* |
|
251
|
|
|
* @api |
|
252
|
|
|
*/ |
|
253
|
3 |
|
public function setParameter($name, $parameter) { |
|
254
|
3 |
|
$this->parameters[$name] = $parameter; |
|
255
|
3 |
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Removes a parameter |
|
261
|
|
|
* |
|
262
|
|
|
* @param string $name |
|
263
|
|
|
* |
|
264
|
|
|
* @return $this |
|
265
|
|
|
*/ |
|
266
|
3 |
|
public function removeParameter($name) { |
|
267
|
3 |
|
unset($this->parameters[$name]); |
|
268
|
3 |
|
return $this; |
|
269
|
|
|
} |
|
270
|
|
|
} |