1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Url; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class UrlHelper |
18
|
|
|
* |
19
|
|
|
* @author Timo Hund <[email protected]> |
20
|
|
|
* @package ApacheSolrForTypo3\Solr\System\Url |
21
|
|
|
*/ |
22
|
|
|
class UrlHelper { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $initialUrl; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $urlParts = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $queryParts = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $wasParsed = false; |
43
|
4 |
|
|
44
|
|
|
/** |
45
|
4 |
|
* UrlHelper constructor. |
46
|
4 |
|
* @param string $url |
47
|
|
|
*/ |
48
|
|
|
public function __construct($url) |
49
|
|
|
{ |
50
|
|
|
$this->initialUrl = $url; |
51
|
4 |
|
$this->parseInitialUrl(); |
52
|
|
|
} |
53
|
4 |
|
|
54
|
3 |
|
/** |
55
|
|
|
* @return void |
56
|
4 |
|
*/ |
57
|
4 |
|
protected function parseInitialUrl() |
58
|
|
|
{ |
59
|
|
|
if ($this->wasParsed) { |
60
|
4 |
|
return; |
61
|
4 |
|
} |
62
|
4 |
|
$parts = parse_url($this->initialUrl); |
63
|
|
|
if (!is_array($parts)) { |
|
|
|
|
64
|
|
|
throw new \InvalidArgumentException("Non parseable url passed to UrlHelper", 1498751529); |
65
|
|
|
} |
66
|
|
|
$this->urlParts = $parts; |
67
|
|
|
|
68
|
|
|
parse_str($this->urlParts['query'], $this->queryParts); |
69
|
1 |
|
|
70
|
|
|
$this->wasParsed = true; |
71
|
1 |
|
} |
72
|
1 |
|
|
73
|
1 |
|
/** |
74
|
1 |
|
* @param string $part |
75
|
1 |
|
* @param mixed $value |
76
|
|
|
*/ |
77
|
1 |
|
protected function setUrlPart($part, $value) |
78
|
|
|
{ |
79
|
|
|
$this->urlParts[$part] = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $path |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
4 |
|
protected function getUrlPart($path) |
87
|
|
|
{ |
88
|
4 |
|
return $this->urlParts[$path]; |
89
|
4 |
|
} |
90
|
4 |
|
|
91
|
4 |
|
/** |
92
|
4 |
|
* @param string $host |
93
|
|
|
* @return UrlHelper |
94
|
4 |
|
*/ |
95
|
|
|
public function setHost(string $host) |
96
|
|
|
{ |
97
|
|
|
$this->setUrlPart('host', $host); |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
4 |
|
|
101
|
|
|
/** |
102
|
4 |
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getHost(): string |
105
|
|
|
{ |
106
|
|
|
return $this->getUrlPart('host'); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
/** |
110
|
|
|
* @param string $scheme |
111
|
4 |
|
* @return UrlHelper |
112
|
4 |
|
*/ |
113
|
4 |
|
public function setScheme(string $scheme) |
114
|
4 |
|
{ |
115
|
4 |
|
$this->setUrlPart('scheme', $scheme); |
116
|
4 |
|
return $this; |
117
|
4 |
|
} |
118
|
4 |
|
|
119
|
4 |
|
/** |
120
|
4 |
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getScheme(): string |
123
|
|
|
{ |
124
|
|
|
return $this->getUrlPart('scheme'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $path |
129
|
|
|
* @return UrlHelper |
130
|
|
|
*/ |
131
|
|
|
public function setPath($path) |
132
|
|
|
{ |
133
|
|
|
$this->setUrlPart('path', $path); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getPath(): string |
141
|
|
|
{ |
142
|
|
|
return $this->getUrlPart('path'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $parameterName |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
* @return UrlHelper |
149
|
|
|
*/ |
150
|
|
|
public function removeQueryParameter(string $parameterName): UrlHelper |
151
|
|
|
{ |
152
|
|
|
unset($this->queryParts[$parameterName]); |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $parameterName |
158
|
|
|
* @param mixed $value |
159
|
|
|
* @throws \InvalidArgumentException |
160
|
|
|
* @return UrlHelper |
161
|
|
|
*/ |
162
|
|
|
public function addQueryParameter(string $parameterName, $value): UrlHelper |
163
|
|
|
{ |
164
|
|
|
$this->queryParts[$parameterName] = $value; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getUrl(): string |
172
|
|
|
{ |
173
|
|
|
$this->urlParts['query'] = http_build_query($this->queryParts); |
174
|
|
|
return $this->unparseUrl(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function unparseUrl(): string |
181
|
|
|
{ |
182
|
|
|
$scheme = isset($this->urlParts['scheme']) ? $this->urlParts['scheme'] . '://' : ''; |
183
|
|
|
$host = $this->urlParts['host'] ?? ''; |
184
|
|
|
$port = $this->urlParts['port'] ?? ''; |
185
|
|
|
$user = $this->urlParts['user'] ?? ''; |
186
|
|
|
$pass = $this->urlParts['pass'] ?? ''; |
187
|
|
|
$pass = ($user || $pass) ? "$pass@" : ''; |
188
|
|
|
$path = $this->urlParts['path'] ?? ''; |
189
|
|
|
$query = isset($this->urlParts['query']) && !empty($this->urlParts['query']) ? '?' . $this->urlParts['query'] : ''; |
190
|
|
|
$fragment = isset($this->urlParts['fragment']) ? '#' . $this->urlParts['fragment'] : ''; |
191
|
|
|
return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
192
|
|
|
} |
193
|
|
|
} |