Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
42 / 42 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
ParamBag | |
100.00% |
42 / 42 |
|
100.00% |
13 / 13 |
26 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasParam | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
contains | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
remove | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
add | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
mergeWith | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
mergeWithAll | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getArrayCopy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
exchangeArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
request | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * Parameter bag. |
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; |
31 | |
32 | use function count; |
33 | use function in_array; |
34 | use function is_array; |
35 | |
36 | /** |
37 | * Lightweight wrapper for request parameters. |
38 | * |
39 | * This class represents the request parameters. Parameters are stored in an |
40 | * associative array with the parameter name as key. Because e.g. SOLR allows |
41 | * repeated query parameters the values are always stored in an array. |
42 | * |
43 | * @category VuFind |
44 | * @package Search |
45 | * @author David Maus <maus@hab.de> |
46 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
47 | * @link https://vufind.org |
48 | */ |
49 | class ParamBag implements \Countable |
50 | { |
51 | /** |
52 | * Parameters |
53 | * |
54 | * @var array |
55 | */ |
56 | protected $params = []; |
57 | |
58 | /** |
59 | * Constructor. |
60 | * |
61 | * @param array $initial Initial parameters |
62 | * |
63 | * @return void |
64 | */ |
65 | public function __construct(array $initial = []) |
66 | { |
67 | foreach ($initial as $name => $value) { |
68 | $this->add($name, $value); |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Return parameter value. |
74 | * |
75 | * @param string $name Parameter name |
76 | * |
77 | * @return mixed|null Parameter value or NULL if not set |
78 | */ |
79 | public function get($name) |
80 | { |
81 | return $this->params[$name] ?? null; |
82 | } |
83 | |
84 | /** |
85 | * Count parameters in internal array. Needed for Countable interface. |
86 | * |
87 | * @return int |
88 | */ |
89 | public function count(): int |
90 | { |
91 | return count($this->params); |
92 | } |
93 | |
94 | /** |
95 | * Return true if the bag contains any value(s) for the specified parameter. |
96 | * |
97 | * @param string $name Parameter name |
98 | * |
99 | * @return bool |
100 | */ |
101 | public function hasParam($name) |
102 | { |
103 | return isset($this->params[$name]); |
104 | } |
105 | |
106 | /** |
107 | * Return true if the bag contains a parameter-value-pair. |
108 | * |
109 | * @param string $name Parameter name |
110 | * @param string $value Parameter value |
111 | * |
112 | * @return bool |
113 | */ |
114 | public function contains($name, $value) |
115 | { |
116 | $haystack = $this->get($name); |
117 | return is_array($haystack) && in_array($value, $haystack); |
118 | } |
119 | |
120 | /** |
121 | * Set a parameter. |
122 | * |
123 | * @param string $name Parameter name |
124 | * @param string $value Parameter value |
125 | * |
126 | * @return void |
127 | */ |
128 | public function set($name, $value) |
129 | { |
130 | if (is_array($value)) { |
131 | $this->params[$name] = $value; |
132 | } else { |
133 | $this->params[$name] = [$value]; |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * Remove a parameter. |
139 | * |
140 | * @param string $name Parameter name |
141 | * |
142 | * @return void |
143 | */ |
144 | public function remove($name) |
145 | { |
146 | if (isset($this->params[$name])) { |
147 | unset($this->params[$name]); |
148 | } |
149 | } |
150 | |
151 | /** |
152 | * Add parameter value. |
153 | * |
154 | * @param string $name Parameter name |
155 | * @param mixed $value Parameter value |
156 | * @param bool $deduplicate Deduplicate parameter values |
157 | * |
158 | * @return void |
159 | */ |
160 | public function add($name, $value, $deduplicate = true) |
161 | { |
162 | if (!isset($this->params[$name])) { |
163 | $this->params[$name] = []; |
164 | } |
165 | if (is_array($value)) { |
166 | $this->params[$name] = array_merge($this->params[$name], $value); |
167 | } else { |
168 | $this->params[$name][] = $value; |
169 | } |
170 | if ($deduplicate) { |
171 | $this->params[$name] = array_values(array_unique($this->params[$name])); |
172 | } |
173 | } |
174 | |
175 | /** |
176 | * Merge with another parameter bag. |
177 | * |
178 | * @param ParamBag $bag Parameter bag to merge with |
179 | * |
180 | * @return void |
181 | */ |
182 | public function mergeWith(ParamBag $bag) |
183 | { |
184 | foreach ($bag->params as $key => $value) { |
185 | if (!empty($value)) { |
186 | $this->add($key, $value); |
187 | } |
188 | } |
189 | } |
190 | |
191 | /** |
192 | * Merge with all supplied parameter bags. |
193 | * |
194 | * @param array $bags Parameter bags to merge with |
195 | * |
196 | * @return void |
197 | */ |
198 | public function mergeWithAll(array $bags) |
199 | { |
200 | foreach ($bags as $bag) { |
201 | $this->mergeWith($bag); |
202 | } |
203 | } |
204 | |
205 | /** |
206 | * Return copy of parameters as array. |
207 | * |
208 | * @return array |
209 | */ |
210 | public function getArrayCopy() |
211 | { |
212 | return $this->params; |
213 | } |
214 | |
215 | /** |
216 | * Exchange the parameter array. |
217 | * |
218 | * @param array $input New parameters |
219 | * |
220 | * @return array Old parameters |
221 | */ |
222 | public function exchangeArray(array $input) |
223 | { |
224 | $current = $this->params; |
225 | $this->params = []; |
226 | foreach ($input as $key => $value) { |
227 | $this->set($key, $value); |
228 | } |
229 | return $current; |
230 | } |
231 | |
232 | /** |
233 | * Return array of params ready to be used in a HTTP request. |
234 | * |
235 | * Returns a numerical array with all request parameters as properly URL |
236 | * encoded key-value pairs. |
237 | * |
238 | * @return array |
239 | */ |
240 | public function request() |
241 | { |
242 | $request = []; |
243 | foreach ($this->params as $name => $values) { |
244 | if (!empty($values)) { |
245 | $request = array_merge( |
246 | $request, |
247 | array_map( |
248 | function ($value) use ($name) { |
249 | return sprintf( |
250 | '%s=%s', |
251 | urlencode($name), |
252 | urlencode($value ?? '') |
253 | ); |
254 | }, |
255 | $values |
256 | ) |
257 | ); |
258 | } |
259 | } |
260 | return $request; |
261 | } |
262 | } |