Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.80% |
44 / 49 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
RecordFormatter | |
89.80% |
44 / 49 |
|
63.64% |
7 / 11 |
25.66 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDedupIds | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
getExtendedSubjectHeadings | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
getFullRecord | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getRawData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getRecordPage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getURLs | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getFields | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
6.01 | |||
getRecordFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRecordFieldSpec | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
format | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Record 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 Laminas\View\HelperPluginManager; |
33 | use VuFind\I18n\TranslatableString; |
34 | |
35 | use function is_object; |
36 | |
37 | /** |
38 | * Record formatter for API responses |
39 | * |
40 | * @category VuFind |
41 | * @package API_Formatter |
42 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
43 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
44 | * @link https://vufind.org/wiki/development:plugins:controllers Wiki |
45 | */ |
46 | class RecordFormatter extends BaseFormatter |
47 | { |
48 | /** |
49 | * Record field definitions |
50 | * |
51 | * @var array |
52 | */ |
53 | protected $recordFields; |
54 | |
55 | /** |
56 | * View helper plugin manager |
57 | * |
58 | * @var HelperPluginManager |
59 | */ |
60 | protected $helperManager; |
61 | |
62 | /** |
63 | * Constructor |
64 | * |
65 | * @param array $recordFields Record field definitions |
66 | * @param HelperPluginManager $helperManager View helper plugin manager |
67 | */ |
68 | public function __construct( |
69 | $recordFields, |
70 | HelperPluginManager $helperManager |
71 | ) { |
72 | $this->recordFields = $recordFields; |
73 | $this->helperManager = $helperManager; |
74 | } |
75 | |
76 | /** |
77 | * Get dedup IDs |
78 | * |
79 | * @param \VuFind\RecordDriver\AbstractBase $record Record driver |
80 | * |
81 | * @return array|null |
82 | */ |
83 | protected function getDedupIds($record) |
84 | { |
85 | if (!($dedupData = $record->tryMethod('getDedupData'))) { |
86 | return null; |
87 | } |
88 | $result = []; |
89 | foreach ($dedupData as $item) { |
90 | $result[] = $item['id']; |
91 | } |
92 | return $result ? $result : null; |
93 | } |
94 | |
95 | /** |
96 | * Get extended subject headings |
97 | * |
98 | * @param \VuFind\RecordDriver\SolrDefault $record Record driver |
99 | * |
100 | * @return array|null |
101 | */ |
102 | protected function getExtendedSubjectHeadings($record) |
103 | { |
104 | $result = $record->getAllSubjectHeadings(true); |
105 | // Make sure that the record driver returned the additional information and |
106 | // return data only if it did |
107 | return $result && isset($result[0]['heading']) ? $result : null; |
108 | } |
109 | |
110 | /** |
111 | * Get full record for a record as XML |
112 | * |
113 | * @param \VuFind\RecordDriver\AbstractBase $record Record driver |
114 | * |
115 | * @return string|null |
116 | */ |
117 | protected function getFullRecord($record) |
118 | { |
119 | if ($xml = $record->tryMethod('getFilteredXML')) { |
120 | return $xml; |
121 | } |
122 | $rawData = $record->tryMethod('getRawData'); |
123 | return $rawData['fullrecord'] ?? null; |
124 | } |
125 | |
126 | /** |
127 | * Get raw data for a record as an array |
128 | * |
129 | * @param \VuFind\RecordDriver\AbstractBase $record Record driver |
130 | * |
131 | * @return array |
132 | */ |
133 | protected function getRawData($record) |
134 | { |
135 | $rawData = $record->tryMethod('getRawData'); |
136 | |
137 | // Leave out spelling data |
138 | unset($rawData['spelling']); |
139 | |
140 | return $rawData; |
141 | } |
142 | |
143 | /** |
144 | * Get (relative) link to record page |
145 | * |
146 | * @param \VuFind\RecordDriver\AbstractBase $record Record driver |
147 | * |
148 | * @return string |
149 | */ |
150 | protected function getRecordPage($record) |
151 | { |
152 | $urlHelper = $this->helperManager->get('recordLinker'); |
153 | return $urlHelper->getUrl($record); |
154 | } |
155 | |
156 | /** |
157 | * Get URLs |
158 | * |
159 | * @param \VuFind\RecordDriver\AbstractBase $record Record driver |
160 | * |
161 | * @return array |
162 | */ |
163 | protected function getURLs($record) |
164 | { |
165 | $recordHelper = $this->helperManager->get('record'); |
166 | return $recordHelper($record)->getLinkDetails(); |
167 | } |
168 | |
169 | /** |
170 | * Get fields from a record as an array |
171 | * |
172 | * @param \VuFind\RecordDriver\AbstractBase $record Record driver |
173 | * @param array $fields Fields to get |
174 | * |
175 | * @return array |
176 | */ |
177 | protected function getFields($record, $fields) |
178 | { |
179 | $result = []; |
180 | foreach ($fields as $field) { |
181 | if (!isset($this->recordFields[$field])) { |
182 | continue; |
183 | } |
184 | $method = $this->recordFields[$field]['vufind.method']; |
185 | if (strncmp($method, 'Formatter::', 11) == 0) { |
186 | $value = $this->{substr($method, 11)}($record); |
187 | } else { |
188 | $value = $record->tryMethod($method); |
189 | } |
190 | $result[$field] = $value; |
191 | } |
192 | // Convert any translation aware string classes to strings |
193 | $translator = $this->helperManager->get('translate'); |
194 | array_walk_recursive( |
195 | $result, |
196 | function (&$value) use ($translator) { |
197 | if (is_object($value)) { |
198 | if ($value instanceof TranslatableString) { |
199 | $value = [ |
200 | 'value' => (string)$value, |
201 | 'translated' => $translator->translate($value), |
202 | ]; |
203 | } else { |
204 | $value = (string)$value; |
205 | } |
206 | } |
207 | } |
208 | ); |
209 | |
210 | return $result; |
211 | } |
212 | |
213 | /** |
214 | * Get record field definitions. |
215 | * |
216 | * @return array |
217 | */ |
218 | public function getRecordFields() |
219 | { |
220 | return $this->recordFields; |
221 | } |
222 | |
223 | /** |
224 | * Return record field specs for the API specification |
225 | * |
226 | * @return array |
227 | */ |
228 | public function getRecordFieldSpec() |
229 | { |
230 | $fields = array_map( |
231 | function ($item) { |
232 | foreach (array_keys($item) as $key) { |
233 | if (strncmp($key, 'vufind.', 7) == 0) { |
234 | unset($item[$key]); |
235 | } |
236 | } |
237 | return $item; |
238 | }, |
239 | $this->recordFields |
240 | ); |
241 | return $fields; |
242 | } |
243 | |
244 | /** |
245 | * Format the results. |
246 | * |
247 | * @param array $results Results to process (array of record drivers) |
248 | * @param array $requestedFields Fields to include in response |
249 | * |
250 | * @return array |
251 | */ |
252 | public function format($results, $requestedFields) |
253 | { |
254 | $records = []; |
255 | foreach ($results as $result) { |
256 | $records[] = $this->getFields($result, $requestedFields); |
257 | } |
258 | |
259 | $this->filterArrayValues($records); |
260 | |
261 | return $records; |
262 | } |
263 | } |