1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zapheus\Bridge\Psr; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\UriInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* URI |
9
|
|
|
* |
10
|
|
|
* @package Zapheus |
11
|
|
|
* @author Rougin Gutib <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Uri implements UriInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $fragment = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $host = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $path = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var integer|null |
32
|
|
|
*/ |
33
|
|
|
protected $port = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $query = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $scheme = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $uri = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $user = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initializes the URI instance. |
57
|
|
|
* |
58
|
|
|
* @param string $uri |
59
|
|
|
*/ |
60
|
108 |
|
public function __construct($uri = '') |
61
|
|
|
{ |
62
|
108 |
|
$parts = parse_url($uri) ?: array(); |
63
|
|
|
|
64
|
108 |
|
foreach ($parts as $key => $value) { |
65
|
108 |
|
$key === 'user' && $this->user = $value; |
66
|
|
|
|
67
|
108 |
|
$this->$key = $value; |
68
|
36 |
|
} |
69
|
|
|
|
70
|
108 |
|
$this->uri = (string) $uri; |
71
|
108 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return the string representation as a URI reference. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
57 |
|
public function __toString() |
79
|
|
|
{ |
80
|
57 |
|
return $this->uri; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieves the authority component of the URI. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
57 |
|
public function getAuthority() |
89
|
|
|
{ |
90
|
57 |
|
$authority = $this->host; |
91
|
|
|
|
92
|
57 |
|
if ($this->host !== '' && $this->user !== null) { |
93
|
57 |
|
$authority = $this->user . '@' . $authority; |
94
|
|
|
|
95
|
57 |
|
$authority = $authority . ':' . $this->port; |
96
|
19 |
|
} |
97
|
|
|
|
98
|
57 |
|
return $authority; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieves the fragment component of the URI. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
60 |
|
public function getFragment() |
107
|
|
|
{ |
108
|
60 |
|
return $this->fragment; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieves the host component of the URI. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
63 |
|
public function getHost() |
117
|
|
|
{ |
118
|
63 |
|
return $this->host; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieves the path component of the URI. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
60 |
|
public function getPath() |
127
|
|
|
{ |
128
|
60 |
|
return $this->path; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieves the port component of the URI. |
133
|
|
|
* |
134
|
|
|
* @return null|integer |
135
|
|
|
*/ |
136
|
63 |
|
public function getPort() |
137
|
|
|
{ |
138
|
63 |
|
return $this->port; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Retrieves the query string of the URI. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
60 |
|
public function getQuery() |
147
|
|
|
{ |
148
|
60 |
|
return $this->query; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Retrieves the scheme component of the URI. |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
60 |
|
public function getScheme() |
157
|
|
|
{ |
158
|
60 |
|
return $this->scheme; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieves the user information component of the URI. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
60 |
|
public function getUserInfo() |
167
|
|
|
{ |
168
|
60 |
|
return $this->user; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns an instance with the specified URI fragment. |
173
|
|
|
* |
174
|
|
|
* @param string $fragment |
175
|
|
|
* @return static |
176
|
|
|
*/ |
177
|
3 |
|
public function withFragment($fragment) |
178
|
|
|
{ |
179
|
3 |
|
$new = clone $this; |
180
|
|
|
|
181
|
3 |
|
$new->fragment = $fragment; |
182
|
|
|
|
183
|
3 |
|
return $new; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns an instance with the specified host. |
188
|
|
|
* |
189
|
|
|
* @param string $host |
190
|
|
|
* @return static |
191
|
|
|
* |
192
|
|
|
* @throws \InvalidArgumentException |
193
|
|
|
*/ |
194
|
3 |
|
public function withHost($host) |
195
|
|
|
{ |
196
|
|
|
// TODO: Add \InvalidArgumentException |
197
|
|
|
|
198
|
3 |
|
$new = clone $this; |
199
|
|
|
|
200
|
3 |
|
$new->host = $host; |
201
|
|
|
|
202
|
3 |
|
return $new; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns an instance with the specified path. |
207
|
|
|
* |
208
|
|
|
* @param string $path |
209
|
|
|
* @return static |
210
|
|
|
* |
211
|
|
|
* @throws \InvalidArgumentException |
212
|
|
|
*/ |
213
|
3 |
|
public function withPath($path) |
214
|
|
|
{ |
215
|
|
|
// TODO: Add \InvalidArgumentException |
216
|
|
|
|
217
|
3 |
|
$new = clone $this; |
218
|
|
|
|
219
|
3 |
|
$new->path = $path; |
220
|
|
|
|
221
|
3 |
|
return $new; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns an instance with the specified port. |
226
|
|
|
* |
227
|
|
|
* @param null|integer $port |
228
|
|
|
* @return static |
229
|
|
|
* |
230
|
|
|
* @throws \InvalidArgumentException |
231
|
|
|
*/ |
232
|
3 |
|
public function withPort($port) |
233
|
|
|
{ |
234
|
|
|
// TODO: Add \InvalidArgumentException |
235
|
|
|
|
236
|
3 |
|
$new = clone $this; |
237
|
|
|
|
238
|
3 |
|
$new->port = $port; |
239
|
|
|
|
240
|
3 |
|
return $new; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns an instance with the specified query string. |
245
|
|
|
* |
246
|
|
|
* @param string $query |
247
|
|
|
* @return static |
248
|
|
|
* |
249
|
|
|
* @throws \InvalidArgumentException |
250
|
|
|
*/ |
251
|
3 |
|
public function withQuery($query) |
252
|
|
|
{ |
253
|
|
|
// TODO: Add \InvalidArgumentException |
254
|
|
|
|
255
|
3 |
|
$new = clone $this; |
256
|
|
|
|
257
|
3 |
|
$new->query = $query; |
258
|
|
|
|
259
|
3 |
|
return $new; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns an instance with the specified scheme. |
264
|
|
|
* |
265
|
|
|
* @param string $scheme |
266
|
|
|
* @return static |
267
|
|
|
* |
268
|
|
|
* @throws \InvalidArgumentException |
269
|
|
|
*/ |
270
|
3 |
|
public function withScheme($scheme) |
271
|
|
|
{ |
272
|
|
|
// TODO: Add \InvalidArgumentException |
273
|
|
|
|
274
|
3 |
|
$new = clone $this; |
275
|
|
|
|
276
|
3 |
|
$new->scheme = $scheme; |
277
|
|
|
|
278
|
3 |
|
return $new; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Returns an instance with the specified user information. |
283
|
|
|
* |
284
|
|
|
* @param string $user |
285
|
|
|
* @param null|string $password |
286
|
|
|
* @return static |
287
|
|
|
*/ |
288
|
3 |
|
public function withUserInfo($user, $password = null) |
289
|
|
|
{ |
290
|
3 |
|
$new = clone $this; |
291
|
|
|
|
292
|
3 |
|
$new->user = $user . ':' . $password; |
293
|
|
|
|
294
|
3 |
|
return $new; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Generates a \Psr\Http\Message\UriInterface from server variables. |
299
|
|
|
* |
300
|
|
|
* @param array $server |
301
|
|
|
* @return \Psr\Http\Message\UriInterface |
302
|
|
|
*/ |
303
|
81 |
|
public static function instance(array $server) |
304
|
|
|
{ |
305
|
81 |
|
$secure = isset($server['HTTPS']) ? $server['HTTPS'] : 'off'; |
306
|
|
|
|
307
|
81 |
|
$http = $secure === 'off' ? 'http' : 'https'; |
308
|
|
|
|
309
|
81 |
|
$url = $http . '://' . $server['SERVER_NAME']; |
310
|
|
|
|
311
|
81 |
|
$url .= (string) $server['SERVER_PORT']; |
312
|
|
|
|
313
|
81 |
|
return new Uri($url . $server['REQUEST_URI']); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|