1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* PHP Domain Parser: Public Suffix List based URL parsing. |
7
|
|
|
* |
8
|
|
|
* @link http://github.com/jeremykendall/php-domain-parser for the canonical source repository |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall) |
11
|
|
|
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Pdp\Uri; |
15
|
|
|
|
16
|
|
|
use Pdp\Parser; |
17
|
|
|
use Pdp\Uri\Url\Host; |
18
|
|
|
use voku\helper\UTF8; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* An object representation of a Url. |
22
|
|
|
*/ |
23
|
|
|
class Url |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string scheme |
27
|
|
|
*/ |
28
|
|
|
protected $scheme; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Host Host object |
32
|
|
|
*/ |
33
|
|
|
protected $host; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int port |
37
|
|
|
*/ |
38
|
|
|
protected $port; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string user |
42
|
|
|
*/ |
43
|
|
|
protected $user; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string pass |
47
|
|
|
*/ |
48
|
|
|
protected $pass; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string path |
52
|
|
|
*/ |
53
|
|
|
protected $path; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string query |
57
|
|
|
*/ |
58
|
|
|
protected $query; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string fragment |
62
|
|
|
*/ |
63
|
|
|
protected $fragment; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Public constructor. |
67
|
|
|
* |
68
|
|
|
* @param string $scheme The URL scheme (e.g. `http`). |
69
|
|
|
* @param string $user The username. |
70
|
|
|
* @param string $pass The password. |
71
|
|
|
* @param Host $host The host elements. |
72
|
|
|
* @param int $port The port number. |
73
|
|
|
* @param string $path The path elements, including format. |
74
|
|
|
* @param string $query The query elements. |
75
|
|
|
* @param string $fragment The fragment. |
76
|
|
|
*/ |
77
|
10 |
|
public function __construct( |
78
|
|
|
$scheme, |
79
|
|
|
$user, |
80
|
|
|
$pass, |
81
|
|
|
Host $host, |
82
|
|
|
$port, |
83
|
|
|
$path, |
84
|
|
|
$query, |
85
|
|
|
$fragment |
86
|
|
|
) |
87
|
|
|
{ |
88
|
|
|
// Ensure scheme is either a legit scheme or null, never an empty string. |
89
|
|
|
// @see https://github.com/jeremykendall/php-domain-parser/issues/53 |
90
|
10 |
|
$this->scheme = UTF8::strtolower($scheme) ?: null; |
91
|
10 |
|
$this->user = $user; |
92
|
10 |
|
$this->pass = $pass; |
93
|
10 |
|
$this->host = $host; |
94
|
10 |
|
$this->port = $port; |
95
|
10 |
|
$this->path = $path; |
96
|
10 |
|
$this->query = $query; |
97
|
10 |
|
$this->fragment = $fragment; |
98
|
10 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets schemeless url. |
102
|
|
|
* |
103
|
|
|
* @return string Url without scheme |
104
|
|
|
*/ |
105
|
1 |
|
public function getSchemeless(): string |
106
|
|
|
{ |
107
|
1 |
|
return preg_replace(Parser::SCHEME_PATTERN, '//', (string)$this, 1); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Converts the URI object to a string and returns it. |
112
|
|
|
* |
113
|
|
|
* @return string The full URI this object represents. |
114
|
|
|
*/ |
115
|
7 |
|
public function __toString() |
116
|
|
|
{ |
117
|
7 |
|
$url = ''; |
118
|
|
|
|
119
|
7 |
|
if ($this->scheme) { |
120
|
5 |
|
$url .= $this->scheme . '://'; |
121
|
|
|
} |
122
|
|
|
|
123
|
7 |
|
if ($this->user) { |
124
|
2 |
|
$url .= urlencode($this->user); |
125
|
2 |
|
if ($this->pass) { |
126
|
2 |
|
$url .= ':' . urlencode($this->pass); |
127
|
|
|
} |
128
|
2 |
|
$url .= '@'; |
129
|
|
|
} |
130
|
|
|
|
131
|
7 |
|
$host = (string)$this->host; |
132
|
|
|
|
133
|
7 |
|
if ($host) { |
134
|
7 |
|
$url .= $host; |
135
|
|
|
} |
136
|
|
|
|
137
|
7 |
|
if ($this->port) { |
138
|
2 |
|
$url .= ':' . (int)$this->port; |
139
|
|
|
} |
140
|
|
|
|
141
|
7 |
|
if ($this->path) { |
142
|
2 |
|
$url .= $this->path; |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
if ($this->query) { |
146
|
2 |
|
$url .= '?' . $this->query; |
147
|
|
|
} |
148
|
|
|
|
149
|
7 |
|
if ($this->fragment) { |
150
|
2 |
|
$url .= '#' . urlencode($this->fragment); |
151
|
|
|
} |
152
|
|
|
|
153
|
7 |
|
return $url; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Converts the URI object to an array and returns it. |
158
|
|
|
* |
159
|
|
|
* @return array Array of URI component parts |
160
|
|
|
*/ |
161
|
1 |
|
public function toArray(): array |
162
|
|
|
{ |
163
|
|
|
return [ |
164
|
1 |
|
'scheme' => $this->getScheme(), |
165
|
1 |
|
'user' => $this->getUser(), |
166
|
1 |
|
'pass' => $this->getPass(), |
167
|
1 |
|
'host' => (string)$this->getHost(), |
168
|
1 |
|
'subdomain' => $this->getHost()->getSubdomain(), |
169
|
1 |
|
'registrableDomain' => $this->getHost()->getRegistrableDomain(), |
170
|
1 |
|
'publicSuffix' => $this->getHost()->getPublicSuffix(), |
171
|
1 |
|
'port' => $this->getPort(), |
172
|
1 |
|
'path' => $this->getPath(), |
173
|
1 |
|
'query' => $this->getQuery(), |
174
|
1 |
|
'fragment' => $this->getFragment(), |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get Scheme. |
180
|
|
|
* |
181
|
|
|
* @return string|null |
182
|
|
|
*/ |
183
|
2 |
|
public function getScheme() |
184
|
|
|
{ |
185
|
2 |
|
return $this->scheme; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get User. |
190
|
|
|
* |
191
|
|
|
* @return string|null |
192
|
|
|
*/ |
193
|
1 |
|
public function getUser() |
194
|
|
|
{ |
195
|
1 |
|
return $this->user; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get Pass. |
200
|
|
|
* |
201
|
|
|
* @return string|null |
202
|
|
|
*/ |
203
|
1 |
|
public function getPass() |
204
|
|
|
{ |
205
|
1 |
|
return $this->pass; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get Host object. |
210
|
|
|
* |
211
|
|
|
* @return Host |
212
|
|
|
*/ |
213
|
1 |
|
public function getHost(): Host |
214
|
|
|
{ |
215
|
1 |
|
return $this->host; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get Port. |
220
|
|
|
* |
221
|
|
|
* @return int|null |
222
|
|
|
*/ |
223
|
1 |
|
public function getPort() |
224
|
|
|
{ |
225
|
1 |
|
return $this->port; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get Path. |
230
|
|
|
* |
231
|
|
|
* @return string|null |
232
|
|
|
*/ |
233
|
1 |
|
public function getPath() |
234
|
|
|
{ |
235
|
1 |
|
return $this->path; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get Query. |
240
|
|
|
* |
241
|
|
|
* @return string|null |
242
|
|
|
*/ |
243
|
1 |
|
public function getQuery() |
244
|
|
|
{ |
245
|
1 |
|
return $this->query; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get Fragment. |
250
|
|
|
* |
251
|
|
|
* @return string|null |
252
|
|
|
*/ |
253
|
1 |
|
public function getFragment() |
254
|
|
|
{ |
255
|
1 |
|
return $this->fragment; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|