Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
8 / 10
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecordTrait
80.00% covered (warning)
80.00%
8 / 10
77.78% covered (warning)
77.78%
7 / 9
10.80
0.00% covered (danger)
0.00%
0 / 1
 setSourceIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSourceIdentifiers
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getSourceIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSearchBackendIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setResultSetIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResultSetIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addLabel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLabels
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLabels
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Record trait that implements common interface methods.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2022
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 Search
24 * @package  Service
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
28 */
29
30namespace VuFindSearch\Response;
31
32/**
33 * Record trait that implements common interface methods.
34 *
35 * @category Search
36 * @package  Service
37 * @author   Ere Maijala <ere.maijala@helsinki.fi>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development
40 */
41trait RecordTrait
42{
43    /**
44     * Used for identifying record source backend
45     *
46     * @var string
47     */
48    protected $sourceIdentifier = '';
49
50    /**
51     * The unique identifier for the result set.
52     *
53     * This property stores a UUID or similar identifier that uniquely identifies
54     * the result set. It is typically set by calling the `setResultSetIdentifier` method.
55     *
56     * @var string|null
57     */
58    protected $resultSetIdentifier = null;
59
60    /**
61     * Used for identifying the search backend used to find the record
62     *
63     * @var string
64     */
65    protected $searchBackendIdentifier = '';
66
67    /**
68     * Labels for the record
69     *
70     * @var array
71     */
72    protected $labels = [];
73
74    /**
75     * Set the record source backend identifier.
76     *
77     * @param string $identifier Record source identifier
78     *
79     * @return void
80     *
81     * @deprecated Use setSourceIdentifiers instead
82     */
83    public function setSourceIdentifier($identifier)
84    {
85        $this->setSourceIdentifiers($identifier, $identifier);
86    }
87
88    /**
89     * Set the source backend identifiers.
90     *
91     * @param string $recordSourceId  Record source identifier
92     * @param string $searchBackendId Search backend identifier (if different from
93     * $recordSourceId)
94     *
95     * @return void
96     */
97    public function setSourceIdentifiers($recordSourceId, $searchBackendId = '')
98    {
99        $this->sourceIdentifier = $recordSourceId;
100        $this->searchBackendIdentifier = $searchBackendId ?: $recordSourceId;
101    }
102
103    /**
104     * Return the source backend identifier.
105     *
106     * @return string
107     */
108    public function getSourceIdentifier()
109    {
110        return $this->sourceIdentifier;
111    }
112
113    /**
114     * Return the search backend identifier used to find the record.
115     *
116     * @return string
117     */
118    public function getSearchBackendIdentifier()
119    {
120        return $this->searchBackendIdentifier;
121    }
122
123    /**
124     * Sets the unique result set identifier.
125     *
126     * This method assigns a UUID or similar identifier to the result set.
127     *
128     * @param string $uuid A valid UUID or identifier to assign to the result set.
129     *
130     * @return void
131     */
132    public function setResultSetIdentifier(string $uuid)
133    {
134        $this->resultSetIdentifier = $uuid;
135    }
136
137    /**
138     * Retrieves the unique result set identifier.
139     *
140     * This method returns the UUID or similar identifier associated with the result set.
141     * If no identifier has been set, it will return null.
142     *
143     * @return string|null The UUID of the result set, or null if not set.
144     */
145    public function getResultSetIdentifier(): ?string
146    {
147        return $this->resultSetIdentifier;
148    }
149
150    /**
151     * Add a label for the record
152     *
153     * @param string $label Label, may be a translation key
154     * @param string $class Label class
155     *
156     * @return void
157     */
158    public function addLabel(string $label, string $class)
159    {
160        $this->labels[] = compact('label', 'class');
161    }
162
163    /**
164     * Set the labels for the record
165     *
166     * @param array $labels An array of associative arrays with keys 'label' and
167     * 'class'
168     *
169     * @return void
170     */
171    public function setLabels(array $labels)
172    {
173        $this->labels = $labels;
174    }
175
176    /**
177     * Return all labels for the record
178     *
179     * @return array An array of associative arrays with keys 'label' and 'class'
180     */
181    public function getLabels()
182    {
183        return $this->labels;
184    }
185}