1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenEngine\Mika\Core\Components\Http\Message\Uri; |
4
|
|
|
|
5
|
|
|
use OpenEngine\Mika\Core\Components\Http\Message\Uri\Traits\CloneableHostTrait; |
6
|
|
|
use OpenEngine\Mika\Core\Components\Http\Message\Uri\Traits\CloneableQueryTrait; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
class Uri implements UriInterface |
10
|
|
|
{ |
11
|
|
|
use CloneableHostTrait; |
12
|
|
|
use CloneableQueryTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Uri constructor. |
16
|
|
|
* @param null|string $uri |
17
|
|
|
*/ |
18
|
|
|
public function __construct(?string $uri = null) |
19
|
|
|
{ |
20
|
|
|
if ($uri === null) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this |
25
|
|
|
->setScheme(parse_url($uri, PHP_URL_SCHEME)) |
26
|
|
|
->setUser(parse_url($uri, PHP_URL_USER)) |
27
|
|
|
->setPassword(parse_url($uri, PHP_URL_PASS)) |
28
|
|
|
->setHost(parse_url($uri, PHP_URL_HOST)) |
29
|
|
|
->setPort(parse_url($uri, PHP_URL_PORT)) |
|
|
|
|
30
|
|
|
->setPath(parse_url($uri, PHP_URL_PATH)) |
31
|
|
|
->setQuery(parse_url($uri, PHP_URL_QUERY)) |
32
|
|
|
->setFragment(parse_url($uri, PHP_URL_FRAGMENT)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return the string representation as a URI reference. |
37
|
|
|
* |
38
|
|
|
* Depending on which components of the URI are present, the resulting |
39
|
|
|
* string is either a full URI or relative reference according to RFC 3986, |
40
|
|
|
* Section 4.1. The method concatenates the various components of the URI, |
41
|
|
|
* using the appropriate delimiters: |
42
|
|
|
* |
43
|
|
|
* - If a scheme is present, it MUST be suffixed by ":". |
44
|
|
|
* - If an authority is present, it MUST be prefixed by "//". |
45
|
|
|
* - The path can be concatenated without delimiters. But there are two |
46
|
|
|
* cases where the path has to be adjusted to make the URI reference |
47
|
|
|
* valid as PHP does not allow to throw an exception in __toString(): |
48
|
|
|
* - If the path is rootless and an authority is present, the path MUST |
49
|
|
|
* be prefixed by "/". |
50
|
|
|
* - If the path is starting with more than one "/" and no authority is |
51
|
|
|
* present, the starting slashes MUST be reduced to one. |
52
|
|
|
* - If a query is present, it MUST be prefixed by "?". |
53
|
|
|
* - If a fragment is present, it MUST be prefixed by "#". |
54
|
|
|
* |
55
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-4.1 |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function __toString() |
59
|
|
|
{ |
60
|
|
|
return |
61
|
|
|
$this->getBasePart() . |
62
|
|
|
$this->getTailPart(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return an empty string or [scheme://][user:password@]host[:port] |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
private function getBasePart(): string |
71
|
|
|
{ |
72
|
|
|
$scheme = empty($this->getScheme()) ? '' : $this->getScheme() . ':'; |
73
|
|
|
$authority = empty($this->getAuthority()) ? '' : '//' . $this->getAuthority(); |
74
|
|
|
|
75
|
|
|
return $scheme . $authority; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return an empty string or /[path][?query][#fragment] |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
private function getTailPart(): string |
84
|
|
|
{ |
85
|
|
|
$path = '/'; |
86
|
|
|
|
87
|
|
|
if (!empty($this->getPath())) { |
88
|
|
|
$path = strpos($this->getPath(), '/') === 0 ? '' : '/'; |
89
|
|
|
$path .= $this->getPath(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$query = empty($this->getQuery()) ? '' : '?' . $this->getQuery(); |
93
|
|
|
$fragment = empty($this->getFragment()) ? '' : '#' . $this->getFragment(); |
94
|
|
|
|
95
|
|
|
return $path . $query . $fragment; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|