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
|
|
|
use TYPO3\CMS\Core\Http\Uri; |
18
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class UrlHelper |
22
|
|
|
* |
23
|
|
|
* @author Timo Hund <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class UrlHelper extends Uri |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param string $host |
29
|
|
|
* @return UrlHelper |
30
|
|
|
* @deprecated Will be removed with v12. Use withHost instead. |
31
|
|
|
* @see Uri::withHost() |
32
|
|
|
*/ |
33
|
|
|
public function setHost(string $host) |
34
|
|
|
{ |
35
|
|
|
$this->host = $host; |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $port |
41
|
|
|
* @return UrlHelper |
42
|
|
|
* @deprecated Will be removed with v12. Use withPort instead. |
43
|
|
|
* @see Uri::withPort() |
44
|
|
|
*/ |
45
|
|
|
public function setPort(string $port) |
46
|
|
|
{ |
47
|
|
|
if ($port !== '') { |
48
|
|
|
if (MathUtility::canBeInterpretedAsInteger($port) === false) { |
49
|
|
|
$argumentType = is_object($port) ? get_class($port) : gettype($port); |
|
|
|
|
50
|
|
|
throw new \InvalidArgumentException('Invalid port "' . $argumentType . '" specified, must be an integer.', 1436717324); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$port = (int)$port; |
54
|
|
|
if ($port < 1 || $port > 65535) { |
55
|
|
|
throw new \InvalidArgumentException('Invalid port "' . $port . '" specified, must be a valid TCP/UDP port.', 1436717326); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->port = $port; |
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $scheme |
65
|
|
|
* @return UrlHelper |
66
|
|
|
* @deprecated Will be removed with v12. Use Uri::withScheme instead. |
67
|
|
|
* @see Uri::withScheme() |
68
|
|
|
*/ |
69
|
|
|
public function setScheme(string $scheme) |
70
|
|
|
{ |
71
|
|
|
$this->scheme = $this->sanitizeScheme($scheme); |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $path |
77
|
|
|
* @return UrlHelper |
78
|
|
|
* @deprecated Will be removed with v12. Use withPath instead. |
79
|
|
|
* @see Uri::withPath() |
80
|
|
|
*/ |
81
|
|
|
public function setPath($path) |
82
|
|
|
{ |
83
|
|
|
if (!is_string($path)) { |
|
|
|
|
84
|
|
|
throw new \InvalidArgumentException('Invalid path provided. Must be of type string.', 1436717328); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (strpos($path, '?') !== false) { |
88
|
|
|
throw new \InvalidArgumentException('Invalid path provided. Must not contain a query string.', 1436717330); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (strpos($path, '#') !== false) { |
92
|
|
|
throw new \InvalidArgumentException('Invalid path provided; must not contain a URI fragment', 1436717332); |
93
|
|
|
} |
94
|
|
|
$this->path = $this->sanitizePath($path); |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Remove a given parameter from the query and create a new instance. |
100
|
|
|
* |
101
|
|
|
* @param string $parameterName |
102
|
|
|
* @return UrlHelper |
103
|
|
|
*/ |
104
|
|
|
public function withoutQueryParameter(string $parameterName): UrlHelper |
105
|
|
|
{ |
106
|
|
|
parse_str($this->query, $parameters); |
107
|
|
|
if (isset($parameters[$parameterName])) { |
108
|
|
|
unset($parameters[$parameterName]); |
109
|
|
|
} |
110
|
|
|
$query = ''; |
111
|
|
|
if (!empty($parameters)) { |
112
|
|
|
$query = http_build_query($parameters); |
113
|
|
|
} |
114
|
|
|
$query = $this->sanitizeQuery($query); |
115
|
|
|
$clonedObject = clone $this; |
116
|
|
|
$clonedObject->query = $query; |
117
|
|
|
return $clonedObject; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $parameterName |
122
|
|
|
* @throws \InvalidArgumentException |
123
|
|
|
* @return UrlHelper |
124
|
|
|
* @deprecated Will be removed with v12. Use withoutQueryParameter instead. |
125
|
|
|
*/ |
126
|
36 |
|
public function removeQueryParameter(string $parameterName): UrlHelper |
127
|
|
|
{ |
128
|
36 |
|
parse_str($this->query, $parameters); |
129
|
36 |
|
if (isset($parameters[$parameterName])) { |
130
|
|
|
unset($parameters[$parameterName]); |
131
|
|
|
} |
132
|
36 |
|
$query = ''; |
133
|
36 |
|
if (!empty($parameters)) { |
134
|
36 |
|
$query = http_build_query($parameters); |
135
|
|
|
} |
136
|
36 |
|
$this->query = $this->sanitizeQuery($query); |
137
|
|
|
|
138
|
36 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Add a given parameter with value to the query and create a new instance. |
143
|
|
|
* |
144
|
|
|
* @param string $parameterName |
145
|
|
|
* @param mixed $value |
146
|
|
|
* @return UrlHelper |
147
|
|
|
*/ |
148
|
|
|
public function withQueryParameter(string $parameterName, $value): UrlHelper |
149
|
|
|
{ |
150
|
|
|
parse_str($this->query, $parameters); |
151
|
|
|
$parameters[$parameterName] = $value; |
152
|
|
|
$query = ''; |
153
|
|
|
if (!empty($parameters)) { |
154
|
|
|
$query = http_build_query($parameters); |
155
|
|
|
} |
156
|
|
|
$query = $this->sanitizeQuery($query); |
157
|
|
|
$clonedObject = clone $this; |
158
|
|
|
$clonedObject->query = $query; |
159
|
|
|
return $clonedObject; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $parameterName |
164
|
|
|
* @param mixed $value |
165
|
|
|
* @throws \InvalidArgumentException |
166
|
|
|
* @return UrlHelper |
167
|
|
|
* @deprecated Will be removed with v12. Use withQueryParameter instead. |
168
|
|
|
*/ |
169
|
|
|
public function addQueryParameter(string $parameterName, $value): UrlHelper |
170
|
|
|
{ |
171
|
|
|
$parameters = $this->query; |
172
|
|
|
parse_str($this->query, $parameters); |
|
|
|
|
173
|
|
|
if (empty($parameters)) { |
174
|
|
|
$parameters = []; |
175
|
|
|
} |
176
|
|
|
$parameters[$parameterName] = $value; |
177
|
|
|
$query = ''; |
178
|
|
|
if (!empty($parameters)) { |
179
|
|
|
$query = http_build_query($parameters); |
180
|
|
|
} |
181
|
|
|
$this->query = $this->sanitizeQuery($query); |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return string |
187
|
|
|
* @deprecated Will be removed with v12. Use __toString() instead. |
188
|
|
|
*/ |
189
|
36 |
|
public function getUrl(): string |
190
|
|
|
{ |
191
|
36 |
|
return $this->__toString(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|