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 | /** |
||
18 | * Class UrlHelper |
||
19 | * |
||
20 | * @author Timo Hund <[email protected]> |
||
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)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
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 $port |
||
111 | 4 | * @return UrlHelper |
|
112 | 4 | */ |
|
113 | 4 | public function setPort(string $port) |
|
114 | 4 | { |
|
115 | 4 | $this->setUrlPart('port', $port); |
|
116 | 4 | return $this; |
|
117 | 4 | } |
|
118 | 4 | ||
119 | 4 | /** |
|
120 | 4 | * @return string |
|
121 | */ |
||
122 | public function getPort(): string |
||
123 | { |
||
124 | return $this->getUrlPart('port'); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param string $scheme |
||
129 | * @return UrlHelper |
||
130 | */ |
||
131 | public function setScheme(string $scheme) |
||
132 | { |
||
133 | $this->setUrlPart('scheme', $scheme); |
||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getScheme(): string |
||
141 | { |
||
142 | return $this->getUrlPart('scheme'); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $path |
||
147 | * @return UrlHelper |
||
148 | */ |
||
149 | public function setPath($path) |
||
150 | { |
||
151 | $this->setUrlPart('path', $path); |
||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getPath(): string |
||
159 | { |
||
160 | return $this->getUrlPart('path'); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $parameterName |
||
165 | * @throws \InvalidArgumentException |
||
166 | * @return UrlHelper |
||
167 | */ |
||
168 | public function removeQueryParameter(string $parameterName): UrlHelper |
||
169 | { |
||
170 | unset($this->queryParts[$parameterName]); |
||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string $parameterName |
||
176 | * @param mixed $value |
||
177 | * @throws \InvalidArgumentException |
||
178 | * @return UrlHelper |
||
179 | */ |
||
180 | public function addQueryParameter(string $parameterName, $value): UrlHelper |
||
181 | { |
||
182 | $this->queryParts[$parameterName] = $value; |
||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getUrl(): string |
||
190 | { |
||
191 | $this->urlParts['query'] = http_build_query($this->queryParts); |
||
192 | return $this->unparseUrl(); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | protected function unparseUrl(): string |
||
199 | { |
||
200 | $scheme = isset($this->urlParts['scheme']) ? $this->urlParts['scheme'] . '://' : ''; |
||
201 | $host = $this->urlParts['host'] ?? ''; |
||
202 | $port = $this->urlParts['port'] ? ':' . $this->urlParts['port'] : ''; |
||
203 | $user = $this->urlParts['user'] ?? ''; |
||
204 | $user = $this->urlParts['pass'] ? $user . ':' : $user; |
||
205 | $pass = $this->urlParts['pass'] ?? ''; |
||
206 | $pass = ($user || $pass) ? "$pass@" : ''; |
||
207 | $path = $this->urlParts['path'] ?? ''; |
||
208 | $query = isset($this->urlParts['query']) && !empty($this->urlParts['query']) ? '?' . $this->urlParts['query'] : ''; |
||
209 | $fragment = isset($this->urlParts['fragment']) ? '#' . $this->urlParts['fragment'] : ''; |
||
210 | return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
||
211 | } |
||
212 | } |
||
213 |