Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractBase
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 getGenericData
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 getMappedData
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3/**
4 * Metadata vocabulary base class
5 * (provides results from available RecordDriver methods in a standardized form)
6 *
7 * PHP version 8
8 *
9 * Copyright (C) University of Tübingen 2019.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Metadata_Vocabularies
26 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\MetadataVocabulary;
32
33use function is_array;
34
35/**
36 * Metadata vocabulary base class
37 * (provides results from available RecordDriver methods in a standardized form)
38 *
39 * @category VuFind
40 * @package  Metadata_Vocabularies
41 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45abstract class AbstractBase implements MetadataVocabularyInterface
46{
47    /**
48     * This variable can be overwritten by child classes
49     * to define which custom field is filled by which generic fields.
50     *
51     * @var array
52     */
53    protected $vocabFieldToGenericFieldsMap = [];
54
55    /**
56     * Generate standardized data from available RecordDriver methods
57     *
58     * @param \VuFind\RecordDriver\AbstractBase $driver Record driver
59     *
60     * @return array
61     */
62    protected function getGenericData(\VuFind\RecordDriver\AbstractBase $driver)
63    {
64        return [
65            'author' => array_unique(
66                array_merge(
67                    $driver->tryMethod('getPrimaryAuthors') ?? [],
68                    $driver->tryMethod('getSecondaryAuthors') ?? [],
69                    $driver->tryMethod('getCorporateAuthors') ?? []
70                )
71            ),
72            'container_title' => $driver->tryMethod('getContainerTitle'),
73            'date' => $driver->tryMethod('getPublicationDates'),
74            'doi' => $driver->tryMethod('getCleanDOI'),
75            'endpage' => $driver->tryMethod('getContainerEndPage'),
76            'isbn' => $driver->tryMethod('getCleanISBN'),
77            'issn' => $driver->tryMethod('getCleanISSN'),
78            'issue' => $driver->tryMethod('getContainerIssue'),
79            'language' => $driver->tryMethod('getLanguages'),
80            'publisher' => $driver->tryMethod('getPublishers'),
81            'startpage' => $driver->tryMethod('getContainerStartPage'),
82            'title' => $driver->tryMethod('getTitle'),
83            'volume' => $driver->tryMethod('getContainerVolume'),
84        ];
85    }
86
87    /**
88     * Perform mapping from generic data to vocabulary data
89     *
90     * @param \VuFind\RecordDriver\AbstractBase $driver Record driver
91     *
92     * @return array
93     */
94    public function getMappedData(\VuFind\RecordDriver\AbstractBase $driver)
95    {
96        $genericData = $this->getGenericData($driver);
97        $mappedData = [];
98
99        foreach ($this->vocabFieldToGenericFieldsMap as $vocabField => $genFields) {
100            foreach ((array)$genFields as $genericField) {
101                $genericValues = $genericData[$genericField] ?? [];
102                if ($genericValues) {
103                    if (!is_array($genericValues)) {
104                        $genericValues = [$genericValues];
105                    }
106                    foreach ($genericValues as $genericValue) {
107                        if (!isset($mappedData[$vocabField])) {
108                            $mappedData[$vocabField] = [];
109                        }
110                        $mappedData[$vocabField][] = $genericValue;
111                    }
112                }
113            }
114        }
115
116        return $mappedData;
117    }
118}