|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Url; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Url\Exceptions\InvalidUrl; |
|
6
|
|
|
|
|
7
|
|
|
class ParsedUrl |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var null|string */ |
|
10
|
|
|
private $scheme; |
|
11
|
|
|
|
|
12
|
|
|
/** @var null|string */ |
|
13
|
|
|
private $host; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string|null */ |
|
16
|
|
|
private $port; |
|
17
|
|
|
|
|
18
|
|
|
/** @var null|string */ |
|
19
|
|
|
private $path; |
|
20
|
|
|
|
|
21
|
|
|
/** @var null|string */ |
|
22
|
|
|
private $query; |
|
23
|
|
|
|
|
24
|
|
|
/** @var null|string */ |
|
25
|
|
|
private $hash; |
|
26
|
|
|
|
|
27
|
16 |
|
public function __construct(?string $scheme = null, ?string $host = null, ?string $port = null, ?string $path = null, ?string $query = null, ?string $hash = null) |
|
28
|
|
|
{ |
|
29
|
16 |
|
$this->scheme = $scheme; |
|
30
|
16 |
|
$this->host = $host; |
|
31
|
16 |
|
$this->port = $port; |
|
32
|
16 |
|
$this->path = $path; |
|
33
|
16 |
|
$this->query = $query; |
|
34
|
16 |
|
$this->hash = $hash; |
|
35
|
16 |
|
} |
|
36
|
|
|
|
|
37
|
17 |
|
public static function fromUrlString(string $url) |
|
38
|
|
|
{ |
|
39
|
17 |
|
return new static(...array_values(static::parse($url))); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
11 |
|
public function get(): string |
|
43
|
|
|
{ |
|
44
|
11 |
|
return $this->reassembleUrl(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
5 |
|
public function replaceScheme(string $scheme): self |
|
48
|
|
|
{ |
|
49
|
5 |
|
return new static( |
|
50
|
5 |
|
$scheme, |
|
51
|
5 |
|
$this->host, |
|
52
|
5 |
|
$this->port, |
|
53
|
5 |
|
$this->path, |
|
54
|
5 |
|
$this->query, |
|
55
|
5 |
|
$this->hash |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
public function replacePath(string $path): self |
|
60
|
|
|
{ |
|
61
|
8 |
|
return new static( |
|
62
|
8 |
|
$this->scheme, |
|
63
|
8 |
|
$this->host, |
|
64
|
8 |
|
$this->port, |
|
65
|
8 |
|
$path, |
|
66
|
8 |
|
$this->query, |
|
67
|
8 |
|
$this->hash |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
17 |
|
private static function parse(string $url): array |
|
72
|
|
|
{ |
|
73
|
|
|
// Specific case where we accept double slashes and convert it to a relative url. |
|
74
|
|
|
// This would otherwise not be able to be parsed. |
|
75
|
17 |
|
if ($url == '//') { |
|
76
|
1 |
|
$url = '/'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
17 |
|
$parsed = parse_url($url); |
|
80
|
|
|
|
|
81
|
17 |
|
if (false === $parsed) { |
|
82
|
1 |
|
throw new InvalidUrl('Failed to parse url. Invalid url ['.$url.'] passed as parameter.'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// If a schemeless url is passed, parse_url will ignore this and strip the first tags |
|
86
|
|
|
// so we need to explicitly reassemble the 'anonymous scheme' manually |
|
87
|
16 |
|
$hasAnonymousScheme = (0 === strpos($url, '//') && isset($parsed['host'])); |
|
88
|
|
|
|
|
89
|
|
|
return [ |
|
90
|
16 |
|
'scheme' => $parsed['scheme'] ?? ($hasAnonymousScheme ? '//' : null), |
|
91
|
16 |
|
'host' => $parsed['host'] ?? null, |
|
92
|
16 |
|
'port' => $parsed['port'] ?? null, |
|
93
|
16 |
|
'path' => $parsed['path'] ?? null, |
|
94
|
16 |
|
'query' => $parsed['query'] ?? null, |
|
95
|
16 |
|
'hash' => $parsed['fragment'] ?? null, |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
11 |
|
private function reassembleUrl(): string |
|
100
|
|
|
{ |
|
101
|
11 |
|
return $this->assembleScheme() . |
|
102
|
11 |
|
$this->host() . |
|
103
|
11 |
|
($this->port() ? ':' . $this->port() : '') . |
|
104
|
11 |
|
$this->path() . |
|
105
|
11 |
|
($this->query() ? '?' . $this->query() : '') . |
|
106
|
11 |
|
($this->hash() ? '#' . $this->hash() : ''); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
11 |
|
private function assembleScheme(): string |
|
110
|
|
|
{ |
|
111
|
11 |
|
if(!$this->hasScheme()) return ''; |
|
112
|
|
|
|
|
113
|
|
|
// Anonymous scheme already ends with double slashes |
|
114
|
10 |
|
if($this->scheme() == '//') return $this->scheme(); |
|
115
|
|
|
|
|
116
|
10 |
|
return $this->scheme() .'://'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
10 |
|
public function scheme(): ?string |
|
120
|
|
|
{ |
|
121
|
10 |
|
return $this->scheme; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
11 |
|
public function host(): ?string |
|
125
|
|
|
{ |
|
126
|
11 |
|
return $this->host; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
11 |
|
public function port(): ?string |
|
130
|
|
|
{ |
|
131
|
11 |
|
return $this->port; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
14 |
|
public function path(): ?string |
|
135
|
|
|
{ |
|
136
|
14 |
|
return $this->path; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
11 |
|
public function query(): ?string |
|
140
|
|
|
{ |
|
141
|
11 |
|
return $this->query; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
11 |
|
public function hash(): ?string |
|
145
|
|
|
{ |
|
146
|
11 |
|
return $this->hash; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
11 |
|
public function hasScheme(): bool |
|
150
|
|
|
{ |
|
151
|
11 |
|
return !!$this->scheme; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function hasHost(): bool |
|
155
|
|
|
{ |
|
156
|
1 |
|
return !!$this->host; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function hasPort(): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return !!$this->port; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
9 |
|
public function hasPath(): bool |
|
165
|
|
|
{ |
|
166
|
9 |
|
return !!$this->path; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
3 |
|
public function hasQuery(): bool |
|
170
|
|
|
{ |
|
171
|
3 |
|
return !!$this->query; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
3 |
|
public function hasHash(): bool |
|
175
|
|
|
{ |
|
176
|
3 |
|
return !!$this->hash; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|