|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
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
|
|
|
namespace ApacheSolrForTypo3\Solr\Routing; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Service class to wrap functions to handle facet values within the URL |
|
21
|
|
|
* |
|
22
|
|
|
* @author Lars Tode <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class UrlFacetService |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Location within the URL. |
|
28
|
|
|
* Possible types are: |
|
29
|
|
|
* - path |
|
30
|
|
|
* - query |
|
31
|
|
|
* |
|
32
|
|
|
* This are connected to the configuration |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $uriLocation = 'query'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Mapping of characters |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $characterMap = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Character to separate multi values |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $multiValueSeparator = ','; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Character to replace multi value separator a value contains it. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $multiValueEscapeCharacter = '°'; |
|
58
|
|
|
|
|
59
|
35 |
|
public function __construct(string $uriLocation, array $settings = []) |
|
60
|
|
|
{ |
|
61
|
35 |
|
$uriLocation = strtolower($uriLocation); |
|
62
|
35 |
|
if (in_array($uriLocation, ['query', 'path'])) { |
|
63
|
35 |
|
$this->uriLocation = $uriLocation; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
35 |
|
$this->init($settings); |
|
67
|
35 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Initialize settings |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $settings |
|
73
|
|
|
*/ |
|
74
|
35 |
|
protected function init(array $settings) |
|
75
|
|
|
{ |
|
76
|
35 |
|
if (is_array($settings['facet-' . $this->uriLocation]['replaceCharacters'])) { |
|
77
|
|
|
$this->characterMap = $settings['facet-' . $this->uriLocation]['replaceCharacters']; |
|
78
|
|
|
} |
|
79
|
35 |
|
if (is_array($settings['replaceCharacters'])) { |
|
80
|
|
|
$this->characterMap = $settings['replaceCharacters']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
35 |
|
if (isset($settings['facet-' . $this->uriLocation]['multiValueSeparator'])) { |
|
84
|
|
|
$this->multiValueSeparator = (string)$settings['facet-' . $this->uriLocation]['multiValueSeparator']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Old configuration style |
|
88
|
35 |
|
if (isset($settings[$this->uriLocation]['multiValueSeparator'])) { |
|
89
|
|
|
$this->multiValueSeparator = (string)$settings[$this->uriLocation]['multiValueSeparator']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
35 |
|
if (isset($settings['facet-' . $this->uriLocation]['multiValueEscapeCharacter'])) { |
|
93
|
|
|
$this->multiValueEscapeCharacter = (string)$settings['facet-' . $this->uriLocation]['multiValueEscapeCharacter']; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Old configuration style |
|
97
|
35 |
|
if (isset($settings[$this->uriLocation]['multiValueEscapeCharacter'])) { |
|
98
|
|
|
$this->multiValueEscapeCharacter = (string)$settings[$this->uriLocation]['multiValueEscapeCharacter']; |
|
99
|
|
|
} |
|
100
|
35 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getUriLocation(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->uriLocation; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getCharacterMap(): array |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->characterMap; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getMultiValueSeparator(): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->multiValueSeparator; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getMultiValueEscapeCharacter(): string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->multiValueEscapeCharacter; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Encodes a single value in case it contains the multi value separator |
|
136
|
|
|
* |
|
137
|
|
|
* @see RoutingService::finalizePathQuery |
|
138
|
|
|
* @param $value |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function encodeSingleValue($value): string |
|
142
|
|
|
{ |
|
143
|
|
|
if (mb_strpos($value, $this->multiValueSeparator) !== false) { |
|
144
|
|
|
$value = str_replace($this->multiValueSeparator, $this->multiValueEscapeCharacter, $value); |
|
145
|
|
|
} elseif (mb_strpos($value, urlencode($this->multiValueSeparator)) !== false) { |
|
146
|
|
|
$value = str_replace( |
|
147
|
|
|
urlencode($this->multiValueSeparator), |
|
148
|
|
|
urlencode($this->multiValueEscapeCharacter), |
|
149
|
|
|
$value |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return (string)$value; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Decodes a single value in case it contains the multi value separator |
|
158
|
|
|
* |
|
159
|
|
|
* @see RoutingService::inflateQueryParameter |
|
160
|
|
|
* @param $value |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function decodeSingleValue($value): string |
|
164
|
|
|
{ |
|
165
|
|
|
if (mb_strpos($value, $this->multiValueEscapeCharacter) !== false) { |
|
166
|
|
|
$value = str_replace($this->multiValueEscapeCharacter, $this->multiValueSeparator, $value); |
|
167
|
|
|
} elseif (mb_strpos($value, urlencode($this->multiValueEscapeCharacter)) !== false) { |
|
168
|
|
|
$value = str_replace( |
|
169
|
|
|
urlencode($this->multiValueEscapeCharacter), |
|
170
|
|
|
urlencode($this->multiValueSeparator), |
|
171
|
|
|
$value |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return (string)$value; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Encode a string for path segment |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $string |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
public function applyCharacterMap(string $string): string |
|
185
|
|
|
{ |
|
186
|
|
|
$string = urldecode($string); |
|
187
|
|
|
|
|
188
|
|
|
if (!empty($this->characterMap)) { |
|
189
|
|
|
foreach ($this->characterMap as $search => $replace) { |
|
190
|
|
|
$string = str_replace($replace, $search, $string); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $string; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|