Completed
Push — master ( a21bcd...546ba5 )
by Derek
02:04
created

Uri::explodeHierParts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.3142
cc 2
eloc 12
nc 2
nop 1
crap 2
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http;
3
4
use Psr\Http\Message\UriInterface;
5
use Subreality\Dilmun\Anshar\Utils\ArrayHelper;
6
use Subreality\Dilmun\Anshar\Utils\StringHelper;
7
8
class Uri implements UriInterface
9
{
10
    use SchemePortsTrait;
11
12
    protected $uri_parts = array(
13
        "scheme"    => "",
14
        "hier_part" => "",
15
        "authority" => "",
16
        "user_info" => "",
17
        "host"      => "",
18
        "port"      => null,
19
        "path"      => "",
20
        "query"     => "",
21
        "fragment"  => "",
22
    );
23
24
    protected $sub_delims = array(
25
        "!",
26
        "$",
27
        "&",
28
        "'",
29
        "(",
30
        ")",
31
        "*",
32
        "+",
33
        ",",
34
        ";",
35
        "=",
36
    );
37
38
    protected $pchar_unencoded = array(
39
        ":",
40
        "@",
41
    );
42
43
    /**
44
     * Uri constructor.  Accepts a string representing a URI and parses the string into the URI's component parts.
45
     *
46
     * @throws \InvalidArgumentException    Throws an \InvalidArgumentException when its parameter is not a string
47
     * @param string $uri
48
     */
49 34
    public function __construct($uri)
50
    {
51 34
        if (!is_string($uri)) {
52 6
            throw new \InvalidArgumentException("New Uri objects must be constructed with a string URI");
53
        }
54
55 28
        $this->explodeUri($uri);
56 28
    }
57
58
    /**
59
     * Retrieve the parsed components of the URI string.
60
     *
61
     * If the class was provided an invalid URI string, URI components will be empty strings, except port, which will
62
     * be null
63
     *
64
     * @return mixed[]
65
     */
66 11
    public function getParsedUri()
67
    {
68 11
        return $this->uri_parts;
69
    }
70
71
    /**
72
     * Retrieve the scheme component of the URI.
73
     *
74
     * If no scheme is present, this method MUST return an empty string.
75
     *
76
     * The value returned MUST be normalized to lowercase, per RFC 3986
77
     * Section 3.1.
78
     *
79
     * The trailing ":" character is not part of the scheme and MUST NOT be
80
     * added.
81
     *
82
     * @see https://tools.ietf.org/html/rfc3986#section-3.1
83
     * @return string The URI scheme.
84
     */
85 3
    public function getScheme()
86
    {
87 3
        return strtolower($this->uri_parts["scheme"]);
88
    }
89
90
    /**
91
     * Retrieve the authority component of the URI.
92
     *
93
     * If no authority information is present, this method MUST return an empty
94
     * string.
95
     *
96
     * The authority syntax of the URI is:
97
     *
98
     * <pre>
99
     * [user-info@]host[:port]
100
     * </pre>
101
     *
102
     * If the port component is not set or is the standard port for the current
103
     * scheme, it SHOULD NOT be included.
104
     *
105
     * @see https://tools.ietf.org/html/rfc3986#section-3.2
106
     * @return string The URI authority, in "[user-info@]host[:port]" format.
107
     */
108 4
    public function getAuthority()
109
    {
110 4
        $normalized_authority = $this->uri_parts["host"];
111
112 4
        if (!empty($this->uri_parts["user_info"])) {
113 4
            $normalized_authority = $this->uri_parts["user_info"] . "@" . $normalized_authority;
114 4
        }
115
116 4
        $normalized_port = $this->normalizePort();
117
118 4
        if (!is_null($normalized_port)) {
119 2
            $normalized_authority = $normalized_authority . ":" . $normalized_port;
120 2
        }
121
122 4
        return $normalized_authority;
123
    }
124
125
    /**
126
     * Retrieve the user information component of the URI.
127
     *
128
     * If no user information is present, this method MUST return an empty
129
     * string.
130
     *
131
     * If a user is present in the URI, this will return that value;
132
     * additionally, if the password is also present, it will be appended to the
133
     * user value, with a colon (":") separating the values.
134
     *
135
     * The trailing "@" character is not part of the user information and MUST
136
     * NOT be added.
137
     *
138
     * @return string The URI user information, in "username[:password]" format.
139
     */
140 2
    public function getUserInfo()
141
    {
142 2
        return $this->uri_parts["user_info"];
143
    }
144
145
    /**
146
     * Retrieve the host component of the URI.
147
     *
148
     * If no host is present, this method MUST return an empty string.
149
     *
150
     * The value returned MUST be normalized to lowercase, per RFC 3986
151
     * Section 3.2.2.
152
     *
153
     * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
154
     * @return string The URI host.
155
     */
156 3
    public function getHost()
157
    {
158 3
        return strtolower($this->uri_parts["host"]);
159
    }
160
161
    /**
162
     * Retrieve the port component of the URI.
163
     *
164
     * If a port is present, and it is non-standard for the current scheme,
165
     * this method MUST return it as an integer. If the port is the standard port
166
     * used with the current scheme, this method SHOULD return null.
167
     *
168
     * If no port is present, and no scheme is present, this method MUST return
169
     * a null value.
170
     *
171
     * If no port is present, but a scheme is present, this method MAY return
172
     * the standard port for that scheme, but SHOULD return null.
173
     *
174
     * @return null|int The URI port.
175
     */
176 4
    public function getPort()
177
    {
178 4
        $normalized_port = $this->normalizePort();
179
180 4
        return $normalized_port;
181
    }
182
183
    /**
184
     * Retrieve the path component of the URI.
185
     *
186
     * The path can either be empty or absolute (starting with a slash) or
187
     * rootless (not starting with a slash). Implementations MUST support all
188
     * three syntaxes.
189
     *
190
     * Normally, the empty path "" and absolute path "/" are considered equal as
191
     * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
192
     * do this normalization because in contexts with a trimmed base path, e.g.
193
     * the front controller, this difference becomes significant. It's the task
194
     * of the user to handle both "" and "/".
195
     *
196
     * The value returned MUST be percent-encoded, but MUST NOT double-encode
197
     * any characters. To determine what characters to encode, please refer to
198
     * RFC 3986, Sections 2 and 3.3.
199
     *
200
     * As an example, if the value should include a slash ("/") not intended as
201
     * delimiter between path segments, that value MUST be passed in encoded
202
     * form (e.g., "%2F") to the instance.
203
     *
204
     * @see https://tools.ietf.org/html/rfc3986#section-2
205
     * @see https://tools.ietf.org/html/rfc3986#section-3.3
206
     * @return string The URI path.
207
     */
208 4
    public function getPath()
209
    {
210 4
        $path_unencoded = array("/");
211
212 4
        $encoded_string = $this->encodeComponent($this->uri_parts["path"], $path_unencoded);
213
214 4
        return $encoded_string;
215
    }
216
217
    /**
218
     * Retrieve the query string of the URI.
219
     *
220
     * If no query string is present, this method MUST return an empty string.
221
     *
222
     * The leading "?" character is not part of the query and MUST NOT be
223
     * added.
224
     *
225
     * The value returned MUST be percent-encoded, but MUST NOT double-encode
226
     * any characters. To determine what characters to encode, please refer to
227
     * RFC 3986, Sections 2 and 3.4.
228
     *
229
     * As an example, if a value in a key/value pair of the query string should
230
     * include an ampersand ("&") not intended as a delimiter between values,
231
     * that value MUST be passed in encoded form (e.g., "%26") to the instance.
232
     *
233
     * @see https://tools.ietf.org/html/rfc3986#section-2
234
     * @see https://tools.ietf.org/html/rfc3986#section-3.4
235
     * @return string The URI query string.
236
     */
237 4
    public function getQuery()
238
    {
239 4
        $query_unencoded = array("/", "?");
240
241 4
        $encoded_string = $this->encodeComponent($this->uri_parts["query"], $query_unencoded);
242
243 4
        return $encoded_string;
244
    }
245
246
    /**
247
     * Retrieve the fragment component of the URI.
248
     *
249
     * If no fragment is present, this method MUST return an empty string.
250
     *
251
     * The leading "#" character is not part of the fragment and MUST NOT be
252
     * added.
253
     *
254
     * The value returned MUST be percent-encoded, but MUST NOT double-encode
255
     * any characters. To determine what characters to encode, please refer to
256
     * RFC 3986, Sections 2 and 3.5.
257
     *
258
     * @see https://tools.ietf.org/html/rfc3986#section-2
259
     * @see https://tools.ietf.org/html/rfc3986#section-3.5
260
     * @return string The URI fragment.
261
     */
262
    public function getFragment()
263
    {
264
        return $this->uri_parts["fragment"];
265
    }
266
267
    /**
268
     * Return an instance with the specified scheme.
269
     *
270
     * This method MUST retain the state of the current instance, and return
271
     * an instance that contains the specified scheme.
272
     *
273
     * Implementations MUST support the schemes "http" and "https" case
274
     * insensitively, and MAY accommodate other schemes if required.
275
     *
276
     * An empty scheme is equivalent to removing the scheme.
277
     *
278
     * @param string $scheme The scheme to use with the new instance.
279
     * @return static A new instance with the specified scheme.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
280
     * @throws \InvalidArgumentException for invalid or unsupported schemes.
281
     */
282
    public function withScheme($scheme)
283
    {
284
        // TODO: Implement withScheme() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
285
    }
286
287
    /**
288
     * Return an instance with the specified user information.
289
     *
290
     * This method MUST retain the state of the current instance, and return
291
     * an instance that contains the specified user information.
292
     *
293
     * Password is optional, but the user information MUST include the
294
     * user; an empty string for the user is equivalent to removing user
295
     * information.
296
     *
297
     * @param string $user The user name to use for authority.
298
     * @param null|string $password The password associated with $user.
299
     * @return static A new instance with the specified user information.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
300
     */
301
    public function withUserInfo($user, $password = null)
302
    {
303
        // TODO: Implement withUserInfo() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
304
    }
305
306
    /**
307
     * Return an instance with the specified host.
308
     *
309
     * This method MUST retain the state of the current instance, and return
310
     * an instance that contains the specified host.
311
     *
312
     * An empty host value is equivalent to removing the host.
313
     *
314
     * @param string $host The hostname to use with the new instance.
315
     * @return static A new instance with the specified host.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
316
     * @throws \InvalidArgumentException for invalid hostnames.
317
     */
318
    public function withHost($host)
319
    {
320
        // TODO: Implement withHost() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
321
    }
322
323
    /**
324
     * Return an instance with the specified port.
325
     *
326
     * This method MUST retain the state of the current instance, and return
327
     * an instance that contains the specified port.
328
     *
329
     * Implementations MUST raise an exception for ports outside the
330
     * established TCP and UDP port ranges.
331
     *
332
     * A null value provided for the port is equivalent to removing the port
333
     * information.
334
     *
335
     * @param null|int $port The port to use with the new instance; a null value
336
     *     removes the port information.
337
     * @return static A new instance with the specified port.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
338
     * @throws \InvalidArgumentException for invalid ports.
339
     */
340
    public function withPort($port)
341
    {
342
        // TODO: Implement withPort() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
343
    }
344
345
    /**
346
     * Return an instance with the specified path.
347
     *
348
     * This method MUST retain the state of the current instance, and return
349
     * an instance that contains the specified path.
350
     *
351
     * The path can either be empty or absolute (starting with a slash) or
352
     * rootless (not starting with a slash). Implementations MUST support all
353
     * three syntaxes.
354
     *
355
     * If the path is intended to be domain-relative rather than path relative then
356
     * it must begin with a slash ("/"). Paths not starting with a slash ("/")
357
     * are assumed to be relative to some base path known to the application or
358
     * consumer.
359
     *
360
     * Users can provide both encoded and decoded path characters.
361
     * Implementations ensure the correct encoding as outlined in getPath().
362
     *
363
     * @param string $path The path to use with the new instance.
364
     * @return static A new instance with the specified path.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
365
     * @throws \InvalidArgumentException for invalid paths.
366
     */
367
    public function withPath($path)
368
    {
369
        // TODO: Implement withPath() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
370
    }
371
372
    /**
373
     * Return an instance with the specified query string.
374
     *
375
     * This method MUST retain the state of the current instance, and return
376
     * an instance that contains the specified query string.
377
     *
378
     * Users can provide both encoded and decoded query characters.
379
     * Implementations ensure the correct encoding as outlined in getQuery().
380
     *
381
     * An empty query string value is equivalent to removing the query string.
382
     *
383
     * @param string $query The query string to use with the new instance.
384
     * @return static A new instance with the specified query string.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
385
     * @throws \InvalidArgumentException for invalid query strings.
386
     */
387
    public function withQuery($query)
388
    {
389
        // TODO: Implement withQuery() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
390
    }
391
392
    /**
393
     * Return an instance with the specified URI fragment.
394
     *
395
     * This method MUST retain the state of the current instance, and return
396
     * an instance that contains the specified URI fragment.
397
     *
398
     * Users can provide both encoded and decoded fragment characters.
399
     * Implementations ensure the correct encoding as outlined in getFragment().
400
     *
401
     * An empty fragment value is equivalent to removing the fragment.
402
     *
403
     * @param string $fragment The fragment to use with the new instance.
404
     * @return static A new instance with the specified fragment.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
405
     */
406
    public function withFragment($fragment)
407
    {
408
        // TODO: Implement withFragment() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
409
    }
410
411
    /**
412
     * Return the string representation as a URI reference.
413
     *
414
     * Depending on which components of the URI are present, the resulting
415
     * string is either a full URI or relative reference according to RFC 3986,
416
     * Section 4.1. The method concatenates the various components of the URI,
417
     * using the appropriate delimiters:
418
     *
419
     * - If a scheme is present, it MUST be suffixed by ":".
420
     * - If an authority is present, it MUST be prefixed by "//".
421
     * - The path can be concatenated without delimiters. But there are two
422
     *   cases where the path has to be adjusted to make the URI reference
423
     *   valid as PHP does not allow to throw an exception in __toString():
424
     *     - If the path is rootless and an authority is present, the path MUST
425
     *       be prefixed by "/".
426
     *     - If the path is starting with more than one "/" and no authority is
427
     *       present, the starting slashes MUST be reduced to one.
428
     * - If a query is present, it MUST be prefixed by "?".
429
     * - If a fragment is present, it MUST be prefixed by "#".
430
     *
431
     * @see http://tools.ietf.org/html/rfc3986#section-4.1
432
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
433
     */
434
    public function __toString()
435
    {
436
        // TODO: Implement __toString() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
437
    }
438
439
    /**
440
     * Splits a string URI into its component parts, returning true if the URI string matches a valid URI's syntax
441
     * and false if the URI string does not
442
     *
443
     * @param string $uri   The URI string to be decomposed
444
     * @return bool         Returns true if the URI string matches a valid URI's syntax
445
     *                      Returns false otherwise
446
     */
447 28
    private function explodeUri($uri)
448
    {
449 28
        $reg_start        = '/^';
450 28
        $scheme_part      = '(?P<scheme>.[^:]+)';
451 28
        $scheme_separator = ':';
452 28
        $hier_part        = '(?<hier_part>.[^\?#]+)';
453 28
        $query_part       = '(?:\?(?P<query>.[^#]+))?';
454 28
        $fragment_part    = '(?:#(?P<fragment>.+))?';
455 28
        $reg_end          = '/';
456
457 28
        $uri_syntax = $reg_start . $scheme_part . $scheme_separator . $hier_part . $query_part . $fragment_part .
458 28
            $reg_end;
459
460 28
        $uri_valid = preg_match($uri_syntax, $uri, $parts);
461
462 28
        $this->uri_parts = array_merge($this->uri_parts, $parts); //overwriting default values with matches
463
464 28
        $this->explodeHierParts($this->uri_parts["hier_part"]);
465
466 28
        $this->sanitizeUriPartsArray();
467
468 28
        return (bool) $uri_valid;
469
    }
470
471
    /**
472
     * Splits URI hierarchy data into authority and path data.
473
     *
474
     * @param string $hier_part     The hierarchy part of a URI to be decomposed
475
     * @return void
476
     */
477 28
    private function explodeHierParts($hier_part)
478
    {
479 28
        $authority_parts = array();
480
481 28
        $reg_start      = '/^';
482 28
        $authority_part = '(?:(?:\/\/)(?P<authority>.[^\/]+))?';
483 28
        $path_part      = '(?P<path>.+)?';
484 28
        $reg_end        = '/';
485
486 28
        $hier_part_syntax = $reg_start . $authority_part . $path_part . $reg_end;
487
488 28
        preg_match($hier_part_syntax, $hier_part, $hier_parts);
489
490 28
        if (isset($hier_parts["authority"])) {
491 24
            $authority_parts = $this->explodeAuthority($hier_parts["authority"]);
492 24
        }
493
494 28
        $hier_parts = array_merge($hier_parts, $authority_parts);
495
496 28
        $this->uri_parts = array_merge($this->uri_parts, $hier_parts);
497 28
    }
498
499
    /**
500
     * Splits URI authority data into user info, host, and port data, returning an array with named keys.
501
     *
502
     * For the host component, it will capture everything within brackets to support ipv6 or match all characters until
503
     * it finds a colon indicating the start of the port component.
504
     *
505
     * @param string $authority     The authority part of a URI to be decomposed
506
     * @return mixed[]              An array with named keys containing the component parts of the supplied
0 ignored issues
show
Documentation introduced by
Should the return type not be array<*,string|integer|null>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
507
     *                              authority
508
     */
509 24
    private function explodeAuthority($authority)
510
    {
511 24
        $reg_start      = '/^';
512 24
        $user_info_part = '(?:(?P<user_info>.+)@)?';
513 24
        $host_part      = '(?P<host>\[.+\]|.[^:]+)';
514 24
        $port_part      = '(?::(?P<port>[0-9]+))?';
515 24
        $reg_end        = '/';
516
517 24
        $authority_syntax = $reg_start . $user_info_part . $host_part . $port_part . $reg_end;
518
519 24
        preg_match($authority_syntax, $authority, $authority_parts);
520
521 24
        if (isset($authority_parts["port"])) {
522 10
            $authority_parts["port"] = (int) $authority_parts["port"];
523 10
        }
524
525 24
        return $authority_parts;
526
    }
527
528
    /**
529
     * Normalizes a port string based on whether the URI's port is standard for its scheme
530
     *
531
     * @return int|null     Returns null if the port is standard for the scheme
532
     *                      Returns the port prepended with a colon if the port is not standard for the scheme
533
     */
534 8
    private function normalizePort()
535
    {
536 8
        $scheme_port_array = new ArrayHelper($this->scheme_ports);
537
538 8
        $standard_port = $scheme_port_array->valueLookup($this->uri_parts["scheme"]);
539
540 8
        if ($this->uri_parts["port"] == $standard_port) {
541 5
            $normalized_port = null;
542 5
        } else {
543 3
            $normalized_port = $this->uri_parts["port"];
544
        }
545
546 8
        return $normalized_port;
547
    }
548
549
    /**
550
     * Sanitizes the URI component array by removing redundant key/value pairs
551
     *
552
     * @return void
553
     */
554 28
    private function sanitizeUriPartsArray()
555
    {
556 28
        $uri_part_array = new ArrayHelper($this->uri_parts);
557
558 28
        $this->uri_parts = $uri_part_array->removeNumericKeys();
559 28
    }
560
561
    /**
562
     * Percent encodes a component string except for sub-delims and unencoded pchar characters as defined by RFC 3986
563
     * in addition to any component-specific unencoded characters
564
     *
565
     * @param string $component_string          The string representing a URI component
566
     * @param string[] $component_unencoded     [OPTIONAL] Any additional unencoded characters specific to the component
567
     *
568
     * @return string                           The string with appropriate characters percent-encoded
569
     */
570 8
    private function encodeComponent($component_string, array $component_unencoded = array())
571
    {
572 8
        $uri_unencoded = array_merge($component_unencoded, $this->sub_delims, $this->pchar_unencoded);
573
574 8
        $string_helper = new StringHelper($component_string);
575
576 8
        $encoded_string = $string_helper->affectChunks("rawurlencode", ...$uri_unencoded);
577
578 8
        return $encoded_string;
579
    }
580
}
581