Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Pazpar2
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 10
812
0.00% covered (danger)
0.00%
0 / 1
 setRawData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 xmlToArray
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
132
 getUniqueId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPrimaryAuthors
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getProviders
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 getPublicationDates
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getShortTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getURLs
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getOpenUrlFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Model for Pazpar2 records.
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  RecordDrivers
25 * @author   Chris Hallberg <challber@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
28 */
29
30namespace VuFind\RecordDriver;
31
32use function count;
33use function in_array;
34use function is_array;
35
36/**
37 * Model for Pazpar2 records.
38 *
39 * @category VuFind
40 * @package  RecordDrivers
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
44 */
45class Pazpar2 extends DefaultRecord
46{
47    /**
48     * Pazpar2 fields
49     *
50     * @var array
51     */
52    protected $pz2fields = [];
53
54    /**
55     * Set raw data to initialize the object.
56     *
57     * @param mixed $data Raw data representing the record; Record Model
58     * objects are normally constructed by Record Driver objects using data
59     * passed in from a Search Results object. The exact nature of the data may
60     * vary depending on the data source -- the important thing is that the
61     * Record Driver + Search Results objects work together correctly.
62     *
63     * @return void
64     */
65    public function setRawData($data)
66    {
67        $this->pz2fields = $this->xmlToArray($data);
68    }
69
70    /**
71     * Converts a SimpleXMLElement to an array
72     *
73     * @param \SimpleXMLElement $xml to be converted
74     *
75     * @return associative array of converted XML
76     */
77    protected function xmlToArray($xml)
78    {
79        $array = [];
80        foreach ($xml as $key => $data) {
81            $children = [];
82            // Attributes
83            if (count($data->attributes()) > 0) {
84                $children['_attr_'] = [];
85                foreach ($data->attributes() as $name => $attr) {
86                    $children['_attr_'][$name] = (string)$attr;
87                }
88            }
89            // If there's no children, we're at data
90            if ($data->count() == 0) {
91                if (!isset($children['_attr_'])) {
92                    $children = (string)$data; // Flatten
93                } else {
94                    $children[$key] = (string)$data;
95                }
96            } else {
97                // If there's children, recurse on this XML
98                $children = $this->xmlToArray($data);
99            }
100            // If first child with this name
101            if (!isset($array[$key])) {
102                $array[$key] = $children;
103            } else {
104                if (
105                    is_array($array[$key])
106                    && is_numeric(current(array_keys($array[$key])))
107                ) {
108                    $array[$key][] = $children;
109                } else {
110                    // Convert for multiple children
111                    $array[$key] = [
112                        $array[$key],
113                        $children,
114                    ];
115                }
116            }
117        }
118        // Top-level Attributes
119        if (count($xml->attributes()) > 0) {
120            $array['_attr_'] = [];
121            foreach ($xml->attributes() as $key => $attr) {
122                $array['_attr_'][$key] = (string)$attr;
123            }
124        }
125        return $array;
126    }
127
128    /**
129     * Return the unique identifier of this record within the Solr index;
130     * useful for retrieving additional information (like tags and user
131     * comments) from the external MySQL database.
132     *
133     * @return string Unique identifier.
134     */
135    public function getUniqueId()
136    {
137        return $this->pz2fields['location']['md-id'] ?? $this->pz2fields['recid'];
138    }
139
140    /**
141     * Get the main authors of the record.
142     *
143     * @return array
144     */
145    public function getPrimaryAuthors()
146    {
147        $authors = $this->pz2fields['md-author'] ?? [];
148
149        return empty($authors) ? [] : (array)$authors;
150    }
151
152    /**
153     * Get the providers of the record.
154     *
155     * @return array
156     */
157    public function getProviders()
158    {
159        if (!$this->pz2fields['location']) {
160            return [];
161        }
162        if (isset($this->pz2fields['location']['_attr_'])) {
163            return [$this->pz2fields['location']['_attr_']['name']];
164        }
165        $providers = [];
166        foreach ($this->pz2fields['location'] as $location) {
167            if (
168                isset($location['_attr_']['name'])
169                && !in_array($location['_attr_']['name'], $providers)
170            ) {
171                $providers[] = $location['_attr_']['name'];
172            }
173        }
174        return $providers;
175    }
176
177    /**
178     * Get the publication dates of the record.  See also getDateSpan().
179     *
180     * @return array
181     */
182    public function getPublicationDates()
183    {
184        return isset($this->pz2fields['md-date']) ?
185            [$this->pz2fields['md-date']] : [];
186    }
187
188    /**
189     * Get the full title of the record.
190     *
191     * @return string
192     */
193    public function getTitle()
194    {
195        return $this->getShortTitle();
196    }
197
198    /**
199     * Get the short (pre-subtitle) title of the record.
200     *
201     * @return string
202     */
203    public function getShortTitle()
204    {
205        return $this->pz2fields['md-title'] ?? '';
206    }
207
208    /**
209     * Return an array of associative URL arrays with one or more of the following
210     * keys:
211     *
212     * <li>
213     *   <ul>desc: URL description text to display (optional)</ul>
214     *   <ul>url: fully-formed URL (required if 'route' is absent)</ul>
215     *   <ul>route: VuFind route to build URL with (required if 'url' is absent)</ul>
216     *   <ul>routeParams: Parameters for route (optional)</ul>
217     *   <ul>queryString: Query params to append after building route (optional)</ul>
218     * </li>
219     *
220     * @return array
221     */
222    public function getURLs()
223    {
224        if (isset($this->pz2fields['location']['md-electronic-url'])) {
225            return array_map(
226                function ($url) {
227                    return ['url' => $url];
228                },
229                (array)$this->pz2fields['location']['md-electronic-url']
230            );
231        }
232        return [];
233    }
234
235    /**
236     * Support method for getOpenUrl() -- pick the OpenURL format.
237     *
238     * @return string
239     */
240    protected function getOpenUrlFormat()
241    {
242        return 'Book';
243    }
244}