|
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
|
|
|
$userInfo = $this->getUserInfo(); |
|
71
|
|
|
$port = $this->getPort(); |
|
72
|
|
|
$authority = ($userInfo !== '' ? $userInfo . '@' : ''); |
|
73
|
|
|
$authority .= $this->getHost(); |
|
74
|
|
|
$authority .= ($port !== null ? ':' . $port : ''); |
|
75
|
|
|
|
|
76
|
|
|
return $authority; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getUserInfo() |
|
83
|
|
|
{ |
|
84
|
|
|
if (! $this->user) { |
|
85
|
|
|
return ''; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->user . ($this->password ? ":{$this->password}" : ''); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getHost() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->host; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getPort() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->port; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getPath() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->path; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getQuery() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->query; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getFragment() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->fragment; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return an instance with the specified scheme. |
|
133
|
|
|
* |
|
134
|
|
|
* This method MUST retain the state of the current instance, and return |
|
135
|
|
|
* an instance that contains the specified scheme. |
|
136
|
|
|
* |
|
137
|
|
|
* Implementations MUST support the schemes "http" and "https" case |
|
138
|
|
|
* insensitively, and MAY accommodate other schemes if required. |
|
139
|
|
|
* |
|
140
|
|
|
* An empty scheme is equivalent to removing the scheme. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $scheme The scheme to use with the new instance. |
|
143
|
|
|
* @return static A new instance with the specified scheme. |
|
144
|
|
|
* @throws \InvalidArgumentException for invalid or unsupported schemes. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function withScheme($scheme) |
|
147
|
|
|
{ |
|
148
|
|
|
if (preg_match('/^[a-zA-Z0-9+-.]+$/', $scheme) === 0) { |
|
149
|
|
|
throw new \InvalidArgumentException( |
|
150
|
|
|
"Invalid scheme {$scheme}; please reference RFC3986 for additional details" |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$instance = clone $this; |
|
155
|
|
|
$instance->scheme = strtolower($scheme); |
|
156
|
|
|
|
|
157
|
|
|
return $instance; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritdoc} |
|
162
|
|
|
*/ |
|
163
|
|
|
public function withUserInfo($user, $password = null) |
|
164
|
|
|
{ |
|
165
|
|
|
$instance = clone $this; |
|
166
|
|
|
$instance->user = $user; |
|
167
|
|
|
if ($password) { |
|
|
|
|
|
|
168
|
|
|
$instance->password = $password; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $instance; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
|
|
public function withHost($host) |
|
178
|
|
|
{ |
|
179
|
|
|
if (!is_string($host)) { |
|
180
|
|
|
throw new \InvalidArgumentException("Invalid host: {$host}"); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
184
|
|
|
$host = "[{$host}]"; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$instance = clone $this; |
|
188
|
|
|
$instance->host = $host; |
|
189
|
|
|
|
|
190
|
|
|
return $instance; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritdoc} |
|
195
|
|
|
*/ |
|
196
|
|
|
public function withPort($port) |
|
197
|
|
|
{ |
|
198
|
|
|
if (!is_int($port)) { |
|
199
|
|
|
throw new \InvalidArgumentException("Invalid port: {$port}"); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$instance = clone $this; |
|
203
|
|
|
$instance->port = $port; |
|
204
|
|
|
|
|
205
|
|
|
return $instance; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritdoc} |
|
210
|
|
|
*/ |
|
211
|
|
|
public function withPath($path) |
|
212
|
|
|
{ |
|
213
|
|
|
if (!is_string($path)) { |
|
214
|
|
|
throw new \InvalidArgumentException("Invalid path: {$path}"); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$instance = clone $this; |
|
218
|
|
|
$instance->path = $path; |
|
219
|
|
|
|
|
220
|
|
|
return $instance; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
|
View Code Duplication |
public function withQuery($query) |
|
|
|
|
|
|
227
|
|
|
{ |
|
228
|
|
|
if (!is_string($query)) { |
|
229
|
|
|
throw new \InvalidArgumentException("Invalid query: {$query}"); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$instance = clone $this; |
|
233
|
|
|
$instance->query = ltrim($query, '?'); |
|
234
|
|
|
|
|
235
|
|
|
return $instance; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* {@inheritdoc} |
|
240
|
|
|
*/ |
|
241
|
|
View Code Duplication |
public function withFragment($fragment) |
|
|
|
|
|
|
242
|
|
|
{ |
|
243
|
|
|
if (!is_string($fragment)) { |
|
244
|
|
|
throw new \InvalidArgumentException("Invalid fragment: {$fragment}"); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$instance = clone $this; |
|
248
|
|
|
$instance->fragment = ltrim($fragment, '#'); |
|
249
|
|
|
|
|
250
|
|
|
return $instance; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* {@inheritdoc} |
|
255
|
|
|
*/ |
|
256
|
|
|
public function __toString() |
|
257
|
|
|
{ |
|
258
|
|
|
$scheme = $this->getScheme(); |
|
259
|
|
|
$authority = $this->getAuthority(); |
|
260
|
|
|
$path = $this->getPath(); |
|
261
|
|
|
$query = $this->getQuery(); |
|
262
|
|
|
$fragment = $this->getFragment(); |
|
263
|
|
|
$uri = $scheme ? "{$scheme}:" : ''; |
|
264
|
|
|
$uri .= $authority ? "//{$authority}" : ''; |
|
265
|
|
|
|
|
266
|
|
|
if ($path) { |
|
267
|
|
|
if ($authority && strlen(ltrim($path, '/')) === 0) { |
|
268
|
|
|
$uri .= '/'; |
|
269
|
|
|
} elseif (!$authority && strpos($path, '//') === 0) { |
|
270
|
|
|
$uri .= '/' . ltrim($path, '/'); |
|
271
|
|
|
} else { |
|
272
|
|
|
$uri .= $path; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$uri .= $query ? '?' . ltrim($query, '?') : ''; |
|
277
|
|
|
$uri .= $fragment ? '#' . ltrim($fragment, '#') : ''; |
|
278
|
|
|
|
|
279
|
|
|
return $uri; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: