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 $parts = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $wasParsed = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* UrlHelper constructor. |
41
|
|
|
* @param string $url |
42
|
|
|
*/ |
43
|
|
|
public function __construct($url) |
44
|
|
|
{ |
45
|
|
|
$this->initialUrl = $url; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function parseInitialUrl() |
52
|
|
|
{ |
53
|
|
|
if ($this->wasParsed) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
$parts = parse_url($this->initialUrl); |
57
|
|
|
if (!is_array($parts)) { |
58
|
|
|
throw new \InvalidArgumentException("Non parseable url passed to UrlHelper", 1498751529); |
59
|
|
|
} |
60
|
|
|
$this->parts = $parts; |
61
|
|
|
$this->wasParsed = true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $parameterName |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
* @return UrlHelper |
68
|
|
|
*/ |
69
|
|
|
public function removeQueryParameter(string $parameterName): UrlHelper |
70
|
|
|
{ |
71
|
|
|
$this->parseInitialUrl(); |
72
|
|
|
$queryParts = []; |
73
|
|
|
parse_str($this->parts['query'], $queryParts); |
74
|
|
|
unset($queryParts[$parameterName]); |
75
|
|
|
$this->parts['query'] = http_build_query($queryParts); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $parameterName |
82
|
|
|
* @param mixed $value |
83
|
|
|
* @throws \InvalidArgumentException |
84
|
|
|
* @return UrlHelper |
85
|
|
|
*/ |
86
|
|
|
public function addQueryParameter(string $parameterName, $value): UrlHelper |
87
|
|
|
{ |
88
|
|
|
$this->parseInitialUrl(); |
89
|
|
|
$queryParts = []; |
90
|
|
|
parse_str($this->parts['query'], $queryParts); |
91
|
|
|
$queryParts[$parameterName] = $value; |
92
|
|
|
$this->parts['query'] = http_build_query($queryParts); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getUrl(): string |
101
|
|
|
{ |
102
|
|
|
return $this->unparse_url($this->parts); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $parsed_url |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function unparse_url(array $parsed_url): string |
110
|
|
|
{ |
111
|
|
|
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
112
|
|
|
$host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
113
|
|
|
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
114
|
|
|
$user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
115
|
|
|
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; |
116
|
|
|
$pass = ($user || $pass) ? "$pass@" : ''; |
117
|
|
|
$path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
118
|
|
|
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; |
119
|
|
|
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; |
120
|
|
|
return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |