Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
BaseFormatter | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
18 | |
100.00% |
1 / 1 |
filterArrayValues | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
16 | |||
resetArrayIndices | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Base formatter for API responses |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) The National Library of Finland 2015-2016. |
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 API_Formatter |
25 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development:plugins:controllers Wiki |
28 | */ |
29 | |
30 | namespace VuFindApi\Formatter; |
31 | |
32 | use function count; |
33 | use function is_array; |
34 | use function is_bool; |
35 | |
36 | /** |
37 | * Base formatter for API responses |
38 | * |
39 | * @category VuFind |
40 | * @package API_Formatter |
41 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org/wiki/development:plugins:controllers Wiki |
44 | */ |
45 | class BaseFormatter |
46 | { |
47 | /** |
48 | * Recursive function to filter array fields: |
49 | * - remove empty values |
50 | * - convert boolean values to 0/1 |
51 | * - force numerically indexed (non-associative) arrays to have numeric keys. |
52 | * |
53 | * @param array $array Array to check |
54 | * |
55 | * @return void |
56 | */ |
57 | protected function filterArrayValues(&$array) |
58 | { |
59 | foreach ($array as $key => &$value) { |
60 | if (is_array($value) && !empty($value)) { |
61 | $this->filterArrayValues($value); |
62 | $this->resetArrayIndices($value); |
63 | } |
64 | |
65 | // We don't want to return empty values -- unless it's an empty array |
66 | // with a non-numeric key, since the key could be significant (e.g. in |
67 | // the case of an author name => roles array with no assigned roles). |
68 | if ( |
69 | (is_numeric($key) && is_array($value) && empty($value)) |
70 | || (is_bool($value) && !$value) |
71 | || $value === null || $value === '' |
72 | ) { |
73 | unset($array[$key]); |
74 | } elseif (is_bool($value) || $value === 'true' || $value === 'false') { |
75 | $array[$key] = $value === true || $value === 'true' ? 1 : 0; |
76 | } |
77 | } |
78 | $this->resetArrayIndices($array); |
79 | } |
80 | |
81 | /** |
82 | * Reset numerical array indices. |
83 | * |
84 | * @param array $array Array |
85 | * |
86 | * @return void |
87 | */ |
88 | protected function resetArrayIndices(&$array) |
89 | { |
90 | $isNumeric = count(array_filter(array_keys($array), 'is_string')) === 0; |
91 | if ($isNumeric) { |
92 | $array = array_values($array); |
93 | } |
94 | } |
95 | } |