1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenEngine\Mika\Core\Components\Http\Message\Uri\Traits; |
4
|
|
|
|
5
|
|
|
trait HostTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private $defaultPorts = [ |
11
|
|
|
'ftp' => 21, |
12
|
|
|
'http' => 80, |
13
|
|
|
'https' => 443, |
14
|
|
|
'imap' => 143, |
15
|
|
|
'ldap' => 389, |
16
|
|
|
'nntp' => 119, |
17
|
|
|
'pop' => 110, |
18
|
|
|
'telnet' => 23, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $scheme; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $user; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $password; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $host; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int|null |
43
|
|
|
*/ |
44
|
|
|
private $port; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieve the scheme component of the URI. |
48
|
|
|
* |
49
|
|
|
* If no scheme is present, this method MUST return an empty string. |
50
|
|
|
* |
51
|
|
|
* The value returned MUST be normalized to lowercase, per RFC 3986 |
52
|
|
|
* Section 3.1. |
53
|
|
|
* |
54
|
|
|
* The trailing ":" character is not part of the scheme and MUST NOT be |
55
|
|
|
* added. |
56
|
|
|
* |
57
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.1 |
58
|
|
|
* @return string The URI scheme. |
59
|
|
|
*/ |
60
|
|
|
public function getScheme(): string |
61
|
|
|
{ |
62
|
|
|
return $this->scheme; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve the authority component of the URI. |
67
|
|
|
* |
68
|
|
|
* If no authority information is present, this method MUST return an empty |
69
|
|
|
* string. |
70
|
|
|
* |
71
|
|
|
* The authority syntax of the URI is: |
72
|
|
|
* |
73
|
|
|
* <pre> |
74
|
|
|
* [user-info@]host[:port] |
75
|
|
|
* </pre> |
76
|
|
|
* |
77
|
|
|
* If the port component is not set or is the standard port for the current |
78
|
|
|
* scheme, it SHOULD NOT be included. |
79
|
|
|
* |
80
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.2 |
81
|
|
|
* @return string The URI authority, in "[user-info@]host[:port]" format. |
82
|
|
|
*/ |
83
|
|
|
public function getAuthority(): string |
84
|
|
|
{ |
85
|
|
|
$result = ''; |
86
|
|
|
|
87
|
|
|
if (!empty($this->getUserInfo())) { |
88
|
|
|
$result .= $this->getUserInfo() . '@'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$result .= $this->getHost(); |
92
|
|
|
$result .= $this->getPort() === null ? '' : ':' . $this->getPort(); |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieve the user information component of the URI. |
99
|
|
|
* |
100
|
|
|
* If no user information is present, this method MUST return an empty |
101
|
|
|
* string. |
102
|
|
|
* |
103
|
|
|
* If a user is present in the URI, this will return that value; |
104
|
|
|
* additionally, if the password is also present, it will be appended to the |
105
|
|
|
* user value, with a colon (":") separating the values. |
106
|
|
|
* |
107
|
|
|
* The trailing "@" character is not part of the user information and MUST |
108
|
|
|
* NOT be added. |
109
|
|
|
* |
110
|
|
|
* @return string The URI user information, in "username[:password]" format. |
111
|
|
|
*/ |
112
|
|
|
public function getUserInfo(): string |
113
|
|
|
{ |
114
|
|
|
$result = $this->user; |
115
|
|
|
|
116
|
|
|
if (!empty($this->getPassword())) { |
117
|
|
|
$result .= ':' . $this->getPassword(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve the host component of the URI. |
125
|
|
|
* |
126
|
|
|
* If no host is present, this method MUST return an empty string. |
127
|
|
|
* |
128
|
|
|
* The value returned MUST be normalized to lowercase, per RFC 3986 |
129
|
|
|
* Section 3.2.2. |
130
|
|
|
* |
131
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
132
|
|
|
* @return string The URI host. |
133
|
|
|
*/ |
134
|
|
|
public function getHost(): string |
135
|
|
|
{ |
136
|
|
|
return $this->host; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Retrieve the port component of the URI. |
141
|
|
|
* |
142
|
|
|
* If a port is present, and it is non-standard for the current scheme, |
143
|
|
|
* this method MUST return it as an integer. If the port is the standard port |
144
|
|
|
* used with the current scheme, this method SHOULD return null. |
145
|
|
|
* |
146
|
|
|
* If no port is present, and no scheme is present, this method MUST return |
147
|
|
|
* a null value. |
148
|
|
|
* |
149
|
|
|
* If no port is present, but a scheme is present, this method MAY return |
150
|
|
|
* the standard port for that scheme, but SHOULD return null. |
151
|
|
|
* |
152
|
|
|
* @return null|int The URI port. |
153
|
|
|
*/ |
154
|
|
|
public function getPort(): ?int |
155
|
|
|
{ |
156
|
|
|
if ($this->isDefaultPort()) { |
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->port; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
private function getPassword(): string |
167
|
|
|
{ |
168
|
|
|
return $this->password; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $scheme |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
private function setScheme(?string $scheme): self |
176
|
|
|
{ |
177
|
|
|
if ($scheme !== null) { |
178
|
|
|
$this->scheme = strtolower($scheme); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $user |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
private function setUser(?string $user): self |
189
|
|
|
{ |
190
|
|
|
$this->user = $user ?? ''; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param null|string $password |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
private function setPassword(?string $password): self |
199
|
|
|
{ |
200
|
|
|
$this->password = $password ?? ''; |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $host |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
private function setHost(?string $host): self |
209
|
|
|
{ |
210
|
|
|
$this->host = $host ?? ''; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param int|null $port |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
private function setPort(?int $port): self |
219
|
|
|
{ |
220
|
|
|
$this->port = $port; |
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
private function isDefaultPort(): bool |
228
|
|
|
{ |
229
|
|
|
if ($this->port === null) { |
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return |
234
|
|
|
isset($this->defaultPorts[$this->getScheme()]) && |
235
|
|
|
$this->port === $this->defaultPorts[$this->getScheme()]; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|