1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Url; |
4
|
|
|
|
5
|
|
|
class Url |
6
|
|
|
{ |
7
|
|
|
/** @var ParsedUrl */ |
8
|
|
|
private $parsedUrl; |
9
|
|
|
|
10
|
|
|
private $root; |
11
|
|
|
|
12
|
|
|
private $secure; |
13
|
|
|
|
14
|
16 |
|
private function __construct(ParsedUrl $parsedUrl) |
15
|
|
|
{ |
16
|
16 |
|
$this->parsedUrl = $parsedUrl; |
17
|
16 |
|
} |
18
|
|
|
|
19
|
17 |
|
public static function fromString(string $url) |
20
|
|
|
{ |
21
|
17 |
|
return new static( ParsedUrl::fromUrlString($url) ); |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
public function setCustomRoot(Root $root) |
25
|
|
|
{ |
26
|
3 |
|
$this->root = $root; |
27
|
|
|
|
28
|
3 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
public function secure() |
32
|
|
|
{ |
33
|
3 |
|
return $this->scheme(true); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function nonSecure() |
37
|
|
|
{ |
38
|
2 |
|
return $this->scheme(false); |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
private function scheme(bool $secure = true) |
42
|
|
|
{ |
43
|
5 |
|
$this->parsedUrl = $this->parsedUrl->replaceScheme($secure ? 'https' : 'http'); |
44
|
|
|
|
45
|
5 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
14 |
|
public function get() |
49
|
|
|
{ |
50
|
14 |
|
if ($this->root) { |
51
|
3 |
|
if ($this->secure) { |
52
|
|
|
$this->root->secure($this->secure); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Path is reconstructed. Taken care of possible double slashes |
56
|
3 |
|
$path = str_replace('//', '/', '/'.trim($this->reassembleWithoutRoot(), '/')); |
57
|
|
|
|
58
|
3 |
|
return rtrim($this->root->get().$path,'/'); |
59
|
|
|
} |
60
|
|
|
|
61
|
11 |
|
return $this->parsedUrl->get(); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function isAbsolute(): bool |
65
|
|
|
{ |
66
|
1 |
|
return $this->parsedUrl->hasHost(); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
public function localize(string $localeSegment = null, array $available_locales = []) |
70
|
|
|
{ |
71
|
8 |
|
$localizedPath = str_replace('//', '/', |
72
|
8 |
|
rtrim('/'.trim($localeSegment.$this->delocalizePath($available_locales), '/'), '/') |
73
|
|
|
); |
74
|
|
|
|
75
|
8 |
|
$this->parsedUrl = $this->parsedUrl->replacePath($localizedPath); |
76
|
|
|
|
77
|
8 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
private function delocalizePath(array $available_locales) |
81
|
|
|
{ |
82
|
8 |
|
if (!$this->parsedUrl->hasPath()) { |
83
|
3 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
8 |
|
$path_segments = explode('/', trim($this->parsedUrl->path(), '/')); |
87
|
|
|
|
88
|
|
|
// Remove the locale segment if present |
89
|
8 |
|
if (in_array($path_segments[0], array_keys($available_locales))) { |
90
|
1 |
|
unset($path_segments[0]); |
91
|
|
|
} |
92
|
|
|
|
93
|
8 |
|
return '/'.implode('/', $path_segments); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function __toString(): string |
97
|
|
|
{ |
98
|
1 |
|
return $this->get(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Construct a full url with the parsed url elements |
103
|
|
|
* resulted from a parse_url() function call. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
3 |
|
private function reassembleWithoutRoot() |
108
|
|
|
{ |
109
|
|
|
/** |
110
|
|
|
* In some rare conditions the path in interpreted as the host when there is no domain.tld format given. |
111
|
|
|
* This is still considered a valid url, be it with only a tld as indication. |
112
|
|
|
*/ |
113
|
3 |
|
$path = ($this->parsedUrl->hasPath() && $this->parsedUrl->path() != $this->root->host()) |
114
|
3 |
|
? $this->parsedUrl->path() |
115
|
3 |
|
: ''; |
116
|
|
|
|
117
|
|
|
return $path |
118
|
3 |
|
.($this->parsedUrl->hasQuery() ? '?'.$this->parsedUrl->query() : '') |
119
|
3 |
|
.($this->parsedUrl->hasHash() ? '#'.$this->parsedUrl->hash() : ''); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|