|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale\Values; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Locale\Exceptions\InvalidUrl; |
|
6
|
|
|
|
|
7
|
|
|
class Root |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
private $valid = false; |
|
10
|
|
|
|
|
11
|
|
|
private $scheme; |
|
12
|
|
|
private $schemeless = false; |
|
13
|
|
|
private $defaultScheme = 'http'; |
|
14
|
|
|
private $secure = false; |
|
15
|
|
|
|
|
16
|
|
|
private $host; |
|
17
|
|
|
private $port; |
|
18
|
|
|
|
|
19
|
80 |
|
private function __construct(string $host) |
|
20
|
|
|
{ |
|
21
|
80 |
|
$this->parse($host); |
|
22
|
|
|
|
|
23
|
79 |
|
if (false !== filter_var($this->get(), FILTER_VALIDATE_URL)) { |
|
24
|
79 |
|
$this->valid = true; |
|
25
|
|
|
} |
|
26
|
79 |
|
} |
|
27
|
|
|
|
|
28
|
80 |
|
public static function fromString(string $host) |
|
29
|
|
|
{ |
|
30
|
80 |
|
return new self($host); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
79 |
|
public function get() |
|
34
|
|
|
{ |
|
35
|
79 |
|
$scheme = (!is_null($this->scheme)) ? $this->scheme.'://' : ($this->schemeless ? '//' : $this->defaultScheme.'://'); |
|
36
|
79 |
|
$port = (!is_null($this->port)) ? ':'.$this->port : null; |
|
37
|
|
|
|
|
38
|
79 |
|
if ($scheme == 'http://' && $this->secure) { |
|
39
|
|
|
$scheme = 'https://'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
79 |
|
return $scheme.$this->host.$port; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function valid(): bool |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->valid; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
13 |
|
public function secure(): self |
|
51
|
|
|
{ |
|
52
|
13 |
|
$this->secure = true; |
|
53
|
13 |
|
$this->scheme = 'https'; |
|
54
|
|
|
|
|
55
|
13 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
10 |
|
public function host(): string |
|
59
|
|
|
{ |
|
60
|
10 |
|
return $this->host; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
10 |
|
public function scheme(): ?string |
|
64
|
|
|
{ |
|
65
|
10 |
|
return $this->scheme; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
80 |
|
private function parse(string $host) |
|
69
|
|
|
{ |
|
70
|
|
|
// Sanitize url input a bit to remove double slashes, but do not remove first slashes |
|
71
|
80 |
|
if ($host == '//') { |
|
72
|
1 |
|
$host = '/'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
80 |
|
$parsed = parse_url($host); |
|
76
|
|
|
|
|
77
|
80 |
|
if (false === $parsed) { |
|
78
|
1 |
|
throw new InvalidUrl('Failed to parse url. Invalid url ['.$host.'] passed as parameter.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// If a schemeless url is passed, parse_url will ignore this and strip the first tags |
|
82
|
|
|
// so we keep a reminder to explicitly reassemble the 'anonymous scheme' manually |
|
83
|
79 |
|
$this->schemeless = !isset($parsed['scheme']) && (0 === strpos($host, '//') && isset($parsed['host'])); |
|
84
|
|
|
|
|
85
|
79 |
|
$this->scheme = $parsed['scheme'] ?? null; |
|
86
|
79 |
|
if ($this->scheme == 'https') { |
|
87
|
13 |
|
$this->secure(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
79 |
|
$this->host = $this->parseHost($parsed); |
|
91
|
79 |
|
$this->port = $parsed['port'] ?? null; |
|
92
|
79 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function __toString(): string |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->get(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
79 |
|
private function parseHost(array $parsed): string |
|
100
|
|
|
{ |
|
101
|
79 |
|
if (isset($parsed['host'])) { |
|
102
|
76 |
|
return $parsed['host']; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
15 |
|
if (!isset($parsed['path'])) { |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
15 |
|
return (0 < strpos($parsed['path'], '/')) |
|
110
|
1 |
|
? substr($parsed['path'], 0, strpos($parsed['path'], '/')) |
|
111
|
15 |
|
: $parsed['path']; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|