1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Patoui\Router; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
class Uri implements UriInterface |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $scheme; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $host; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $port; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $user; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $password; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $path; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $query; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $fragment; |
34
|
|
|
|
35
|
|
|
public function __construct(string $uri) |
36
|
|
|
{ |
37
|
|
|
$scheme = parse_url($uri, PHP_URL_SCHEME); |
38
|
|
|
$host = parse_url($uri, PHP_URL_HOST); |
39
|
|
|
$port = parse_url($uri, PHP_URL_PORT); |
40
|
|
|
$port = $port ? (int) $port : 80; |
41
|
|
|
$user = parse_url($uri, PHP_URL_USER); |
42
|
|
|
$password = parse_url($uri, PHP_URL_PASS); |
43
|
|
|
$path = parse_url($uri, PHP_URL_PATH); |
44
|
|
|
$query = parse_url($uri, PHP_URL_QUERY); |
45
|
|
|
$fragment = parse_url($uri, PHP_URL_FRAGMENT); |
46
|
|
|
|
47
|
|
|
$this->scheme = $scheme ?: ''; |
48
|
|
|
$this->host = $host ?: ''; |
49
|
|
|
$this->port = $port; |
50
|
|
|
$this->user = $user ?: ''; |
51
|
|
|
$this->path = $path ?: ''; |
52
|
|
|
$this->password = $password ?: ''; |
53
|
|
|
$this->query = $query ?: ''; |
54
|
|
|
$this->fragment = $fragment ?: ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getScheme() |
61
|
|
|
{ |
62
|
|
|
return $this->scheme; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function getAuthority() |
69
|
|
|
{ |
70
|
|
|
return "{$this->user}@{$this->host}" . ($this->port ? ":{$this->port}" : ''); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getUserInfo() |
77
|
|
|
{ |
78
|
|
|
if (! $this->user) { |
79
|
|
|
return ''; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->user . ($this->password ? ":{$this->password}" : ''); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getHost() |
89
|
|
|
{ |
90
|
|
|
return $this->host; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getPort() |
97
|
|
|
{ |
98
|
|
|
return $this->port; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieve the path component of the URI. |
103
|
|
|
* |
104
|
|
|
* The path can either be empty or absolute (starting with a slash) or |
105
|
|
|
* rootless (not starting with a slash). Implementations MUST support all |
106
|
|
|
* three syntaxes. |
107
|
|
|
* |
108
|
|
|
* Normally, the empty path "" and absolute path "/" are considered equal as |
109
|
|
|
* defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
110
|
|
|
* do this normalization because in contexts with a trimmed base path, e.g. |
111
|
|
|
* the front controller, this difference becomes significant. It's the task |
112
|
|
|
* of the user to handle both "" and "/". |
113
|
|
|
* |
114
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
115
|
|
|
* any characters. To determine what characters to encode, please refer to |
116
|
|
|
* RFC 3986, Sections 2 and 3.3. |
117
|
|
|
* |
118
|
|
|
* As an example, if the value should include a slash ("/") not intended as |
119
|
|
|
* delimiter between path segments, that value MUST be passed in encoded |
120
|
|
|
* form (e.g., "%2F") to the instance. |
121
|
|
|
* |
122
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
123
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.3 |
124
|
|
|
* @return string The URI path. |
125
|
|
|
*/ |
126
|
|
|
public function getPath() |
127
|
|
|
{ |
128
|
|
|
// TODO: Implement getPath() method. |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieve the query string of the URI. |
133
|
|
|
* |
134
|
|
|
* If no query string is present, this method MUST return an empty string. |
135
|
|
|
* |
136
|
|
|
* The leading "?" character is not part of the query and MUST NOT be |
137
|
|
|
* added. |
138
|
|
|
* |
139
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
140
|
|
|
* any characters. To determine what characters to encode, please refer to |
141
|
|
|
* RFC 3986, Sections 2 and 3.4. |
142
|
|
|
* |
143
|
|
|
* As an example, if a value in a key/value pair of the query string should |
144
|
|
|
* include an ampersand ("&") not intended as a delimiter between values, |
145
|
|
|
* that value MUST be passed in encoded form (e.g., "%26") to the instance. |
146
|
|
|
* |
147
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
148
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.4 |
149
|
|
|
* @return string The URI query string. |
150
|
|
|
*/ |
151
|
|
|
public function getQuery() |
152
|
|
|
{ |
153
|
|
|
// TODO: Implement getQuery() method. |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Retrieve the fragment component of the URI. |
158
|
|
|
* |
159
|
|
|
* If no fragment is present, this method MUST return an empty string. |
160
|
|
|
* |
161
|
|
|
* The leading "#" character is not part of the fragment and MUST NOT be |
162
|
|
|
* added. |
163
|
|
|
* |
164
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
165
|
|
|
* any characters. To determine what characters to encode, please refer to |
166
|
|
|
* RFC 3986, Sections 2 and 3.5. |
167
|
|
|
* |
168
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
169
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.5 |
170
|
|
|
* @return string The URI fragment. |
171
|
|
|
*/ |
172
|
|
|
public function getFragment() |
173
|
|
|
{ |
174
|
|
|
// TODO: Implement getFragment() method. |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return an instance with the specified scheme. |
179
|
|
|
* |
180
|
|
|
* This method MUST retain the state of the current instance, and return |
181
|
|
|
* an instance that contains the specified scheme. |
182
|
|
|
* |
183
|
|
|
* Implementations MUST support the schemes "http" and "https" case |
184
|
|
|
* insensitively, and MAY accommodate other schemes if required. |
185
|
|
|
* |
186
|
|
|
* An empty scheme is equivalent to removing the scheme. |
187
|
|
|
* |
188
|
|
|
* @param string $scheme The scheme to use with the new instance. |
189
|
|
|
* @return static A new instance with the specified scheme. |
190
|
|
|
* @throws \InvalidArgumentException for invalid or unsupported schemes. |
191
|
|
|
*/ |
192
|
|
|
public function withScheme($scheme) |
193
|
|
|
{ |
194
|
|
|
// TODO: Implement withScheme() method. |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return an instance with the specified user information. |
199
|
|
|
* |
200
|
|
|
* This method MUST retain the state of the current instance, and return |
201
|
|
|
* an instance that contains the specified user information. |
202
|
|
|
* |
203
|
|
|
* Password is optional, but the user information MUST include the |
204
|
|
|
* user; an empty string for the user is equivalent to removing user |
205
|
|
|
* information. |
206
|
|
|
* |
207
|
|
|
* @param string $user The user name to use for authority. |
208
|
|
|
* @param null|string $password The password associated with $user. |
209
|
|
|
* @return static A new instance with the specified user information. |
210
|
|
|
*/ |
211
|
|
|
public function withUserInfo($user, $password = null) |
212
|
|
|
{ |
213
|
|
|
// TODO: Implement withUserInfo() method. |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return an instance with the specified host. |
218
|
|
|
* |
219
|
|
|
* This method MUST retain the state of the current instance, and return |
220
|
|
|
* an instance that contains the specified host. |
221
|
|
|
* |
222
|
|
|
* An empty host value is equivalent to removing the host. |
223
|
|
|
* |
224
|
|
|
* @param string $host The hostname to use with the new instance. |
225
|
|
|
* @return static A new instance with the specified host. |
226
|
|
|
* @throws \InvalidArgumentException for invalid hostnames. |
227
|
|
|
*/ |
228
|
|
|
public function withHost($host) |
229
|
|
|
{ |
230
|
|
|
// TODO: Implement withHost() method. |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return an instance with the specified port. |
235
|
|
|
* |
236
|
|
|
* This method MUST retain the state of the current instance, and return |
237
|
|
|
* an instance that contains the specified port. |
238
|
|
|
* |
239
|
|
|
* Implementations MUST raise an exception for ports outside the |
240
|
|
|
* established TCP and UDP port ranges. |
241
|
|
|
* |
242
|
|
|
* A null value provided for the port is equivalent to removing the port |
243
|
|
|
* information. |
244
|
|
|
* |
245
|
|
|
* @param null|int $port The port to use with the new instance; a null value |
246
|
|
|
* removes the port information. |
247
|
|
|
* @return static A new instance with the specified port. |
248
|
|
|
* @throws \InvalidArgumentException for invalid ports. |
249
|
|
|
*/ |
250
|
|
|
public function withPort($port) |
251
|
|
|
{ |
252
|
|
|
// TODO: Implement withPort() method. |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Return an instance with the specified path. |
257
|
|
|
* |
258
|
|
|
* This method MUST retain the state of the current instance, and return |
259
|
|
|
* an instance that contains the specified path. |
260
|
|
|
* |
261
|
|
|
* The path can either be empty or absolute (starting with a slash) or |
262
|
|
|
* rootless (not starting with a slash). Implementations MUST support all |
263
|
|
|
* three syntaxes. |
264
|
|
|
* |
265
|
|
|
* If the path is intended to be domain-relative rather than path relative then |
266
|
|
|
* it must begin with a slash ("/"). Paths not starting with a slash ("/") |
267
|
|
|
* are assumed to be relative to some base path known to the application or |
268
|
|
|
* consumer. |
269
|
|
|
* |
270
|
|
|
* Users can provide both encoded and decoded path characters. |
271
|
|
|
* Implementations ensure the correct encoding as outlined in getPath(). |
272
|
|
|
* |
273
|
|
|
* @param string $path The path to use with the new instance. |
274
|
|
|
* @return static A new instance with the specified path. |
275
|
|
|
* @throws \InvalidArgumentException for invalid paths. |
276
|
|
|
*/ |
277
|
|
|
public function withPath($path) |
278
|
|
|
{ |
279
|
|
|
// TODO: Implement withPath() method. |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Return an instance with the specified query string. |
284
|
|
|
* |
285
|
|
|
* This method MUST retain the state of the current instance, and return |
286
|
|
|
* an instance that contains the specified query string. |
287
|
|
|
* |
288
|
|
|
* Users can provide both encoded and decoded query characters. |
289
|
|
|
* Implementations ensure the correct encoding as outlined in getQuery(). |
290
|
|
|
* |
291
|
|
|
* An empty query string value is equivalent to removing the query string. |
292
|
|
|
* |
293
|
|
|
* @param string $query The query string to use with the new instance. |
294
|
|
|
* @return static A new instance with the specified query string. |
295
|
|
|
* @throws \InvalidArgumentException for invalid query strings. |
296
|
|
|
*/ |
297
|
|
|
public function withQuery($query) |
298
|
|
|
{ |
299
|
|
|
// TODO: Implement withQuery() method. |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Return an instance with the specified URI fragment. |
304
|
|
|
* |
305
|
|
|
* This method MUST retain the state of the current instance, and return |
306
|
|
|
* an instance that contains the specified URI fragment. |
307
|
|
|
* |
308
|
|
|
* Users can provide both encoded and decoded fragment characters. |
309
|
|
|
* Implementations ensure the correct encoding as outlined in getFragment(). |
310
|
|
|
* |
311
|
|
|
* An empty fragment value is equivalent to removing the fragment. |
312
|
|
|
* |
313
|
|
|
* @param string $fragment The fragment to use with the new instance. |
314
|
|
|
* @return static A new instance with the specified fragment. |
315
|
|
|
*/ |
316
|
|
|
public function withFragment($fragment) |
317
|
|
|
{ |
318
|
|
|
// TODO: Implement withFragment() method. |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Return the string representation as a URI reference. |
323
|
|
|
* |
324
|
|
|
* Depending on which components of the URI are present, the resulting |
325
|
|
|
* string is either a full URI or relative reference according to RFC 3986, |
326
|
|
|
* Section 4.1. The method concatenates the various components of the URI, |
327
|
|
|
* using the appropriate delimiters: |
328
|
|
|
* |
329
|
|
|
* - If a scheme is present, it MUST be suffixed by ":". |
330
|
|
|
* - If an authority is present, it MUST be prefixed by "//". |
331
|
|
|
* - The path can be concatenated without delimiters. But there are two |
332
|
|
|
* cases where the path has to be adjusted to make the URI reference |
333
|
|
|
* valid as PHP does not allow to throw an exception in __toString(): |
334
|
|
|
* - If the path is rootless and an authority is present, the path MUST |
335
|
|
|
* be prefixed by "/". |
336
|
|
|
* - If the path is starting with more than one "/" and no authority is |
337
|
|
|
* present, the starting slashes MUST be reduced to one. |
338
|
|
|
* - If a query is present, it MUST be prefixed by "?". |
339
|
|
|
* - If a fragment is present, it MUST be prefixed by "#". |
340
|
|
|
* |
341
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-4.1 |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
public function __toString() |
345
|
|
|
{ |
346
|
|
|
// TODO: Implement __toString() method. |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|