Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
6 / 8
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecordTrait
75.00% covered (warning)
75.00%
6 / 8
71.43% covered (warning)
71.43%
5 / 7
9.00
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
 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     * Used for identifying the search backend used to find the record
52     *
53     * @var string
54     */
55    protected $searchBackendIdentifier = '';
56
57    /**
58     * Labels for the record
59     *
60     * @var array
61     */
62    protected $labels = [];
63
64    /**
65     * Set the record source backend identifier.
66     *
67     * @param string $identifier Record source identifier
68     *
69     * @return void
70     *
71     * @deprecated Use setSourceIdentifiers instead
72     */
73    public function setSourceIdentifier($identifier)
74    {
75        $this->setSourceIdentifiers($identifier, $identifier);
76    }
77
78    /**
79     * Set the source backend identifiers.
80     *
81     * @param string $recordSourceId  Record source identifier
82     * @param string $searchBackendId Search backend identifier (if different from
83     * $recordSourceId)
84     *
85     * @return void
86     */
87    public function setSourceIdentifiers($recordSourceId, $searchBackendId = '')
88    {
89        $this->sourceIdentifier = $recordSourceId;
90        $this->searchBackendIdentifier = $searchBackendId ?: $recordSourceId;
91    }
92
93    /**
94     * Return the source backend identifier.
95     *
96     * @return string
97     */
98    public function getSourceIdentifier()
99    {
100        return $this->sourceIdentifier;
101    }
102
103    /**
104     * Return the search backend identifier used to find the record.
105     *
106     * @return string
107     */
108    public function getSearchBackendIdentifier()
109    {
110        return $this->searchBackendIdentifier;
111    }
112
113    /**
114     * Add a label for the record
115     *
116     * @param string $label Label, may be a translation key
117     * @param string $class Label class
118     *
119     * @return void
120     */
121    public function addLabel(string $label, string $class)
122    {
123        $this->labels[] = compact('label', 'class');
124    }
125
126    /**
127     * Set the labels for the record
128     *
129     * @param array $labels An array of associative arrays with keys 'label' and
130     * 'class'
131     *
132     * @return void
133     */
134    public function setLabels(array $labels)
135    {
136        $this->labels = $labels;
137    }
138
139    /**
140     * Return all labels for the record
141     *
142     * @return array An array of associative arrays with keys 'label' and 'class'
143     */
144    public function getLabels()
145    {
146        return $this->labels;
147    }
148}