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