Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SourceAndIdList
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getAll
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIdsBySource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRecordPositions
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * Record ID list (support class for Loader)
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
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  Record
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Record;
31
32use VuFind\RecordDriver\AbstractBase as Record;
33
34use function is_array;
35
36/**
37 * Record ID list (support class for Loader)
38 *
39 * @category VuFind
40 * @package  Record
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 Main Site
44 */
45class SourceAndIdList
46{
47    /**
48     * Processed ID data.
49     *
50     * @var array
51     */
52    protected $ids = [];
53
54    /**
55     * Record positions in the original list, indexed by source and ID.
56     *
57     * @var array
58     */
59    protected $bySource = [];
60
61    /**
62     * Constructor
63     *
64     * @param array $ids Array of associative arrays with id/source keys or strings
65     * in source|id format. In associative array formats, there is also an optional
66     * "extra_fields" key which can be used to pass in data formatted as if it
67     * belongs to the Solr schema; this is used to create a mock driver object if
68     * the real data source is unavailable.
69     */
70    public function __construct($ids)
71    {
72        // Sort the IDs by source -- we'll create an associative array indexed by
73        // source and record ID which points to the desired position of the indexed
74        // record in the final return array:
75        foreach ($ids as $i => $details) {
76            // Convert source|id string to array if necessary:
77            if (!is_array($details)) {
78                $parts = explode('|', $details, 2);
79                $ids[$i] = $details = [
80                    'source' => $parts[0], 'id' => $parts[1],
81                ];
82            }
83            $this->bySource[$details['source']][$details['id']][] = $i;
84        }
85        $this->ids = $ids;
86    }
87
88    /**
89     * Get the full list of IDs sent to the constructor, normalized to array
90     * format.
91     *
92     * @return array
93     */
94    public function getAll()
95    {
96        return $this->ids;
97    }
98
99    /**
100     * Get an associative source => id list array.
101     *
102     * @return array
103     */
104    public function getIdsBySource()
105    {
106        return array_map('array_keys', $this->bySource);
107    }
108
109    /**
110     * If the provided record driver corresponds with an ID in the list, return
111     * the associated positions in the list. Otherwise, return an empty array.
112     *
113     * @param Record $record Record
114     *
115     * @return int[]
116     */
117    public function getRecordPositions(Record $record)
118    {
119        $id = $record->getUniqueId();
120        $source = $record->getSourceIdentifier();
121
122        // In some cases (e.g. Summon), the ID may have changed, so also check the
123        // prior ID if available. We should do this BEFORE checking the primary ID
124        // to ensure that we match the correct record in the edge case where a list
125        // contains both an OLD record ID and the NEW record ID that it has been
126        // replaced with. Checking the old ID first ensures that we don't match the
127        // same position twice for two different records.
128        $oldId = $record->tryMethod('getPreviousUniqueId');
129        if ($oldId !== null && isset($this->bySource[$source][$oldId])) {
130            return $this->bySource[$source][$oldId];
131        }
132        if (isset($this->bySource[$source][$id])) {
133            return $this->bySource[$source][$id];
134        }
135        return [];
136    }
137}