1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Url; |
5
|
|
|
|
6
|
|
|
use JetBrains\PhpStorm\Pure; |
7
|
|
|
|
8
|
|
|
class Url |
9
|
|
|
{ |
10
|
19 |
|
private ParsedUrl $parsedUrl; |
11
|
|
|
|
12
|
19 |
|
private function __construct(ParsedUrl $parsedUrl) |
13
|
19 |
|
{ |
14
|
|
|
$this->parsedUrl = $parsedUrl; |
15
|
20 |
|
} |
16
|
|
|
|
17
|
20 |
|
public static function fromString(string $url): self |
18
|
|
|
{ |
19
|
|
|
return new static(ParsedUrl::fromString($url)); |
20
|
4 |
|
} |
21
|
|
|
|
22
|
4 |
|
public function setCustomRoot(Root $root): self |
23
|
|
|
{ |
24
|
4 |
|
$this->parsedUrl = $this->parsedUrl->replaceRoot($root); |
25
|
|
|
|
26
|
|
|
return $this; |
27
|
5 |
|
} |
28
|
|
|
|
29
|
5 |
|
public function secure(): self |
30
|
|
|
{ |
31
|
|
|
return $this->scheme(); |
32
|
3 |
|
} |
33
|
|
|
|
34
|
3 |
|
public function nonSecure(): self |
35
|
|
|
{ |
36
|
|
|
return $this->scheme(false); |
37
|
7 |
|
} |
38
|
|
|
|
39
|
7 |
|
private function scheme(bool $secure = true): self |
40
|
|
|
{ |
41
|
7 |
|
$this->parsedUrl = $this->parsedUrl->replaceScheme($secure ? 'https' : 'http'); |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
16 |
|
} |
45
|
|
|
|
46
|
16 |
|
public function get(): string |
47
|
|
|
{ |
48
|
|
|
return $this->parsedUrl->get(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
1 |
|
public function getScheme(): ?string |
52
|
|
|
{ |
53
|
|
|
return $this->parsedUrl->scheme(); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
|
public function getHost(): ?string |
57
|
|
|
{ |
58
|
|
|
return $this->parsedUrl->host(); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function getPort(): ?string |
62
|
|
|
{ |
63
|
|
|
return $this->parsedUrl->port(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
#[Pure] |
67
|
|
|
public function getPath(): ?string |
68
|
|
|
{ |
69
|
1 |
|
return $this->parsedUrl->path(); |
70
|
|
|
} |
71
|
1 |
|
|
72
|
|
|
#[Pure] |
73
|
|
|
public function getQuery(): ?string |
74
|
1 |
|
{ |
75
|
|
|
return $this->parsedUrl->query(); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
#[Pure] |
79
|
1 |
|
public function getHash(): ?string |
80
|
|
|
{ |
81
|
1 |
|
return $this->parsedUrl->hash(); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function hasScheme(): bool |
85
|
|
|
{ |
86
|
1 |
|
return $this->parsedUrl->hasScheme(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function hasHost(): bool |
90
|
|
|
{ |
91
|
1 |
|
return $this->parsedUrl->hasHost(); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function hasPort(): bool |
95
|
|
|
{ |
96
|
1 |
|
return $this->parsedUrl->hasPort(); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
#[Pure] |
100
|
|
|
public function hasPath(): bool |
101
|
1 |
|
{ |
102
|
|
|
return $this->parsedUrl->hasPath(); |
103
|
|
|
} |
104
|
1 |
|
|
105
|
|
|
#[Pure] |
106
|
1 |
|
public function hasQuery(): bool |
107
|
|
|
{ |
108
|
|
|
return $this->parsedUrl->hasQuery(); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
#[Pure] |
112
|
|
|
public function hasHash(): bool |
113
|
|
|
{ |
114
|
9 |
|
return $this->parsedUrl->hasHash(); |
115
|
|
|
} |
116
|
9 |
|
|
117
|
9 |
|
public function isAbsolute(): bool |
118
|
|
|
{ |
119
|
|
|
return $this->parsedUrl->hasHost(); |
120
|
9 |
|
} |
121
|
|
|
|
122
|
9 |
|
public function localize(string $localeSegment = null, array $available_locales = []): self |
123
|
|
|
{ |
124
|
|
|
$localizedPath = str_replace( |
125
|
9 |
|
'//', |
126
|
|
|
'/', |
127
|
9 |
|
rtrim('/'.trim($localeSegment.$this->delocalizePath($available_locales), '/'), '/') |
128
|
3 |
|
); |
129
|
|
|
|
130
|
|
|
$this->parsedUrl = $this->parsedUrl->replacePath($localizedPath); |
131
|
9 |
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
9 |
|
|
135
|
1 |
|
private function delocalizePath(array $available_locales): string |
136
|
|
|
{ |
137
|
|
|
if (! $this->parsedUrl->hasPath()) { |
138
|
9 |
|
return ''; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
$path_segments = explode('/', trim($this->parsedUrl->path(), '/')); |
142
|
|
|
|
143
|
1 |
|
// Remove the locale segment if present |
144
|
|
|
if (in_array($path_segments[0], array_keys($available_locales))) { |
145
|
|
|
unset($path_segments[0]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return '/'.implode('/', $path_segments); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function __toString(): string |
152
|
|
|
{ |
153
|
|
|
return $this->get(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|