8
8
9
9
class UriHelper
10
10
{
11
+ private static $ defaultSchemePorts = [
12
+ 'http ' => [80 ],
13
+ 'https ' => [443 ],
14
+ ];
15
+
11
16
# region Private methods
12
17
private static function mixUrlEncodedParams (
13
18
string $ encoded_string ,
@@ -209,9 +214,9 @@ public static function buildStringFromParts(array $uriParts): string
209
214
{
210
215
$ join [] = $ scheme = $ uriParts ['scheme ' ] ?? 'http ' ;
211
216
$ join [] = ':// ' ;
212
- if (isset ($ uriParts ['user ' ])) {
217
+ if (! empty ($ uriParts ['user ' ])) {
213
218
$ join [] = $ uriParts ['user ' ];
214
- if (isset ($ uriParts ['pass ' ])) {
219
+ if (! empty ($ uriParts ['pass ' ])) {
215
220
$ join [] = ": {$ uriParts ['pass ' ]}" ;
216
221
}
217
222
$ join [] = '@ ' ;
@@ -220,20 +225,20 @@ public static function buildStringFromParts(array $uriParts): string
220
225
if (isset ($ uriParts ['port ' ])) {
221
226
$ port = $ uriParts ['port ' ];
222
227
if (
223
- $ scheme == ' http ' && $ port != 80 ||
224
- $ scheme == ' https ' && $ port != 443
228
+ isset ( static :: $ defaultSchemePorts [ $ scheme]) &&
229
+ ! in_array ( $ port, static :: $ defaultSchemePorts [ $ scheme ])
225
230
) {
226
231
$ join [] = ": {$ port }" ;
227
232
}
228
233
}
229
234
$ join [] = '/ ' ;
230
- if (isset ($ uriParts ['path ' ])) {
235
+ if (! empty ($ uriParts ['path ' ])) {
231
236
$ join [] = ltrim ($ uriParts ['path ' ], '/ ' );
232
237
}
233
- if (isset ($ uriParts ['query ' ])) {
238
+ if (! empty ($ uriParts ['query ' ])) {
234
239
$ join [] = "? {$ uriParts ['query ' ]}" ;
235
240
}
236
- if (isset ($ uriParts ['fragment ' ])) {
241
+ if (! empty ($ uriParts ['fragment ' ])) {
237
242
$ join [] .= "# {$ uriParts ['fragment ' ]}" ;
238
243
}
239
244
return join ('' , $ join );
@@ -290,14 +295,13 @@ public static function getCurrentString(array $sapiVars = [], bool $cached = fal
290
295
}
291
296
$ uriParts ['host ' ] = $ sapiVars ['HTTP_HOST ' ] ?? $ sapiVars ['SERVER_NAME ' ] ?? null ;
292
297
$ uriParts ['path ' ] = '/ ' . ltrim ($ sapiVars ['REQUEST_URI ' ], '/ ' );
298
+ $ uriParts ['query ' ] = $ _SERVER ['QUERY_STRING ' ] ?? null ;
293
299
return static ::buildStringFromParts ($ uriParts );
294
300
}
295
301
296
302
public static function getCurrent (UriFactoryInterface $ uriFactory ): UriInterface
297
303
{
298
- return $ uriFactory
299
- ->createUri (static ::getCurrentString ())
300
- ->withQuery ($ _SERVER ['QUERY_STRING ' ] ?? '' );
304
+ return $ uriFactory ->createUri (static ::getCurrentString ());
301
305
}
302
306
303
307
private static function mapReplaceString (string $ uri , array $ replaces , bool $ encode_values = true ): string
0 commit comments