Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
67 / 67 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
HandlerMap | |
100.00% |
67 / 67 |
|
100.00% |
9 / 9 |
25 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setHandlerMap | |
100.00% |
42 / 42 |
|
100.00% |
1 / 1 |
11 | |||
getHandler | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
getInvariants | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDefaults | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getAppends | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setParameters | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getParameters | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * SOLR backend handler map. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Search |
25 | * @author David Maus <maus@hab.de> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org |
28 | */ |
29 | |
30 | namespace VuFindSearch\Backend\Solr; |
31 | |
32 | use InvalidArgumentException; |
33 | use RuntimeException; |
34 | use VuFindSearch\Backend\AbstractHandlerMap; |
35 | use VuFindSearch\ParamBag; |
36 | |
37 | /** |
38 | * SOLR backend handler map. |
39 | * |
40 | * @category VuFind |
41 | * @package Search |
42 | * @author David Maus <maus@hab.de> |
43 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
44 | * @link https://vufind.org |
45 | */ |
46 | class HandlerMap extends AbstractHandlerMap |
47 | { |
48 | /** |
49 | * Search handlers, indexed by function. |
50 | * |
51 | * @var array |
52 | */ |
53 | protected $handlers; |
54 | |
55 | /** |
56 | * Query defaults/appends/invariants, indexed by handler. |
57 | * |
58 | * @var array |
59 | */ |
60 | protected $parameters; |
61 | |
62 | /** |
63 | * Fallback handler, if any. |
64 | * |
65 | * @var string |
66 | */ |
67 | protected $fallback; |
68 | |
69 | /** |
70 | * Constructor. |
71 | * |
72 | * @param array $map Handler map |
73 | * |
74 | * @return void |
75 | */ |
76 | public function __construct(array $map = []) |
77 | { |
78 | $this->handlers = []; |
79 | $this->parameters = []; |
80 | $this->setHandlerMap($map); |
81 | } |
82 | |
83 | /** |
84 | * Set the handler map. |
85 | * |
86 | * @param array $map Handler map |
87 | * |
88 | * @return void |
89 | * |
90 | * @throws InvalidArgumentException Duplicate fallback handler |
91 | * @throws InvalidArgumentException Duplicate function handler definition |
92 | */ |
93 | public function setHandlerMap(array $map) |
94 | { |
95 | $fallback = null; |
96 | foreach ($map as $handler => $definition) { |
97 | if (isset($definition['fallback']) && $definition['fallback']) { |
98 | if ($fallback) { |
99 | throw new InvalidArgumentException( |
100 | sprintf( |
101 | 'Duplicate fallback handler definition: %s, %s', |
102 | $handler, |
103 | $fallback |
104 | ) |
105 | ); |
106 | } |
107 | $fallback = $handler; |
108 | } |
109 | if (isset($definition['functions'])) { |
110 | foreach ((array)$definition['functions'] as $function) { |
111 | if (isset($this->handlers[$function])) { |
112 | throw new InvalidArgumentException( |
113 | sprintf( |
114 | 'Handler for function already defined: %s, %s', |
115 | $function, |
116 | $handler |
117 | ) |
118 | ); |
119 | } |
120 | $this->handlers[$function] = $handler; |
121 | } |
122 | } |
123 | if (isset($definition['invariants'])) { |
124 | $this->setParameters( |
125 | $handler, |
126 | 'invariants', |
127 | (array)$definition['invariants'] |
128 | ); |
129 | } |
130 | if (isset($definition['defaults'])) { |
131 | $this->setParameters( |
132 | $handler, |
133 | 'defaults', |
134 | (array)$definition['defaults'] |
135 | ); |
136 | } |
137 | if (isset($definition['appends'])) { |
138 | $this->setParameters( |
139 | $handler, |
140 | 'appends', |
141 | (array)$definition['appends'] |
142 | ); |
143 | } |
144 | } |
145 | $this->fallback = $fallback; |
146 | } |
147 | |
148 | /** |
149 | * Return function handler. |
150 | * |
151 | * @param string $function Name of search function |
152 | * |
153 | * @return string Handler name |
154 | * |
155 | * @throws RuntimeException Undefined function handler |
156 | */ |
157 | public function getHandler($function) |
158 | { |
159 | if (!isset($this->handlers[$function])) { |
160 | if (!$this->fallback) { |
161 | throw new RuntimeException( |
162 | sprintf('Undefined function handler: %s', $function) |
163 | ); |
164 | } |
165 | return $this->fallback; |
166 | } |
167 | return $this->handlers[$function]; |
168 | } |
169 | |
170 | /** |
171 | * Return query invariants for search function. |
172 | * |
173 | * @param string $function Name of search function |
174 | * |
175 | * @return array Query invariants |
176 | */ |
177 | public function getInvariants($function) |
178 | { |
179 | $handler = $this->getHandler($function); |
180 | return $this->getParameters($handler, 'invariants'); |
181 | } |
182 | |
183 | /** |
184 | * Return query defaults for search function. |
185 | * |
186 | * @param string $function Name of search function |
187 | * |
188 | * @return array Query defaults |
189 | */ |
190 | public function getDefaults($function) |
191 | { |
192 | $handler = $this->getHandler($function); |
193 | return $this->getParameters($handler, 'defaults'); |
194 | } |
195 | |
196 | /** |
197 | * Return query appends for search function. |
198 | * |
199 | * @param string $function Name of search function |
200 | * |
201 | * @return array Query appends |
202 | */ |
203 | public function getAppends($function) |
204 | { |
205 | $handler = $this->getHandler($function); |
206 | return $this->getParameters($handler, 'appends'); |
207 | } |
208 | |
209 | /** |
210 | * Add handler default, append, or invariant. |
211 | * |
212 | * @param string $handler Request handler |
213 | * @param string $type Parameter type, one of 'defaults', 'appends', |
214 | * or 'invariants' |
215 | * @param string $name Parameter name |
216 | * @param string $value Parameter value |
217 | * |
218 | * @return void |
219 | */ |
220 | public function addParameter($handler, $type, $name, $value) |
221 | { |
222 | $this->getParameters($handler, $type)->add($name, $value); |
223 | } |
224 | |
225 | /** |
226 | * Set handler defaults, appends, or invariants. |
227 | * |
228 | * @param string $handler Request handler |
229 | * @param string $type Parameter type, one of 'defaults', 'appends', |
230 | * or 'invariants' |
231 | * @param array $parameters Parameters |
232 | * |
233 | * @return void |
234 | */ |
235 | public function setParameters($handler, $type, array $parameters) |
236 | { |
237 | if ($type != 'invariants' && $type != 'appends' && $type != 'defaults') { |
238 | throw new InvalidArgumentException( |
239 | sprintf('Invalid parameter key: %s', $type) |
240 | ); |
241 | } |
242 | $this->parameters[$handler][$type] = new ParamBag($parameters); |
243 | } |
244 | |
245 | /** |
246 | * Return handler defaults, appends, or invariants. |
247 | * |
248 | * @param string $handler Request handler |
249 | * @param string $type Parameter type, one of 'defaults', 'appends', |
250 | * or 'invariants' |
251 | * |
252 | * @return ParamBag |
253 | * |
254 | * @throws InvalidArgumentException Invalid parameter key |
255 | */ |
256 | public function getParameters($handler, $type) |
257 | { |
258 | // Create ParamBag if not already present; this also handles validation |
259 | // of the $type parameter. |
260 | if (!isset($this->parameters[$handler][$type])) { |
261 | $this->setParameters($handler, $type, []); |
262 | } |
263 | return $this->parameters[$handler][$type]; |
264 | } |
265 | } |