Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecordCollection
96.15% covered (success)
96.15%
25 / 26
83.33% covered (warning)
83.33%
5 / 6
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTotal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRawFacets
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFacets
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 setFacets
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOffset
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * EDS API record collection.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) EBSCO Industries 2013
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  Search
25 * @author   Michelle Milton <mmilton@epnet.com>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Backend\EDS\Response;
31
32use VuFindSearch\Response\AbstractRecordCollection;
33
34/**
35 * EDS API record collection.
36 *
37 * @category VuFind
38 * @package  Search
39 * @author   Michelle Milton <mmilton@epnet.com>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org
42 */
43class RecordCollection extends AbstractRecordCollection
44{
45    /**
46     * Response from API
47     *
48     * @var array
49     */
50    protected $response;
51
52    /**
53     * Facet fields.
54     *
55     * @var array
56     */
57    protected $facetFields = null;
58
59    /**
60     * Constructor.
61     *
62     * @param array $response EdsApi response
63     *
64     * @return void
65     */
66    public function __construct(array $response)
67    {
68        $this->response = $response;
69        $this->rewind();
70    }
71
72    /**
73     * Return total number of records found.
74     *
75     * @return int
76     */
77    public function getTotal()
78    {
79        return $this->response['SearchResult']['Statistics']['TotalHits'] ?? 0;
80    }
81
82    /**
83     * Return raw available facet information.
84     *
85     * @return array
86     */
87    public function getRawFacets()
88    {
89        return $this->response['SearchResult']['AvailableFacets'] ?? [];
90    }
91
92    /**
93     * Return available facets.
94     *
95     * Returns an associative array with the field name as key. The value is an
96     * associative array of available facets for the field, indexed by facet value.
97     *
98     * @return array
99     */
100    public function getFacets()
101    {
102        if ($this->facetFields === null) {
103            $this->facetFields = [];
104            $facets = $this->response['SearchResult']['AvailableFacets'] ?? [];
105            foreach ($facets as $facet) {
106                $values = [];
107                foreach ($facet['AvailableFacetValues'] as $facetValue) {
108                    $values[$facetValue['Value']] = $facetValue['Count'];
109                }
110                $this->facetFields[$facet['Id']] = $values;
111            }
112        }
113        return $this->facetFields;
114    }
115
116    /**
117     * Set facets.
118     *
119     * @param array $facets Facet fields
120     *
121     * @return void
122     */
123    public function setFacets(array $facets): void
124    {
125        $this->facetFields = $facets;
126    }
127
128    /**
129     * Return offset in the total search result set.
130     *
131     * @return int
132     */
133    public function getOffset()
134    {
135        if (
136            isset($this->response['SearchRequestGet'])
137            && !empty($this->response['SearchRequestGet']['QueryString'])
138        ) {
139            $qsParameters = explode(
140                '&',
141                $this->response['SearchRequestGet']['QueryString']
142            );
143            $page = empty($qsParameters['pagenumber'])
144                ? 0 : $qsParameters['pagenumber'];
145            $resultsPerPage = empty($qsParameters['resultsperpage'])
146                ? 0 : $qsParameters['resultsperpage'];
147            return $page * $resultsPerPage;
148        }
149        return 0;
150    }
151}