Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.97% covered (warning)
70.97%
22 / 31
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractEDSParams
70.97% covered (warning)
70.97%
22 / 31
50.00% covered (danger)
50.00%
2 / 4
27.83
0.00% covered (danger)
0.00%
0 / 1
 createBackendFilterParameters
61.11% covered (warning)
61.11%
11 / 18
0.00% covered (danger)
0.00%
0 / 1
13.76
 filterRequiresFacetOperator
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 getView
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFacetOperator
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
1<?php
2
3/**
4 * Common EDS & EPF API Params
5 *
6 * PHP version 8
7 *
8 * Copyright (C) EBSCO Industries 2013
9 * Copyright (C) The National Library of Finland 2022
10 * Copyright (C) Villanova University 2023
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24 *
25 * @category VuFind
26 * @package  EBSCO
27 * @author   Michelle Milton <mmilton@epnet.com>
28 * @author   Ere Maijala <ere.maijala@helsinki.fi>
29 * @author   Maccabee Levine <msl321@lehigh.edu>
30 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
31 * @link     https://vufind.org Main Page
32 */
33
34namespace VuFind\Search\EDS;
35
36use VuFindSearch\ParamBag;
37
38use function in_array;
39
40/**
41 * Common EDS & EPF API Params
42 *
43 * @category VuFind
44 * @package  EBSCO
45 * @author   Michelle Milton <mmilton@epnet.com>
46 * @author   Ere Maijala <ere.maijala@helsinki.fi>
47 * @author   Maccabee Levine <msl321@lehigh.edu>
48 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
49 * @link     https://vufind.org Main Page
50 */
51class AbstractEDSParams extends \VuFind\Search\Base\Params
52{
53    /**
54     * Fields that the EDS API will always filter multiple values using OR, not AND.
55     *
56     * @var array
57     */
58    protected $forcedOrFields = [
59    ];
60
61    /**
62     * Set up filters based on VuFind settings.
63     *
64     * @param ParamBag $params Parameter collection to update
65     *
66     * @return void
67     */
68    public function createBackendFilterParameters(ParamBag $params)
69    {
70        // Which filters should be applied to our query?
71        $filterList = $this->getFilterList();
72        $hiddenFilterList = $this->getHiddenFilters();
73        if (!empty($filterList)) {
74            // Loop through all filters and add appropriate values to request:
75            foreach ($filterList as $filterArray) {
76                foreach ($filterArray as $filt) {
77                    $fq = $filt['field']
78                        . ($this->filterRequiresFacetOperator($filt['field']) ?
79                            ":{$this->getFacetOperator($filt['field'], $filt['operator'])}" : '')
80                        . ":{$filt['value']}";
81                    $params->add('filters', $fq);
82                }
83            }
84        }
85        if (!empty($hiddenFilterList)) {
86            foreach ($hiddenFilterList as $field => $hiddenFilters) {
87                foreach ($hiddenFilters as $value) {
88                    $hfq = $field
89                        . ($this->filterRequiresFacetOperator($field) ?
90                            ":{$this->getFacetOperator($field)}" : '')
91                        . ":{$value}";
92                    $params->add('filters', $hfq);
93                }
94            }
95        }
96    }
97
98    /**
99     * Determines if the given filter field is a normal one, which should include the AND/OR operator,
100     * or a special filter which should not.
101     *
102     * @param string $field Filter field name
103     *
104     * @return boolean
105     */
106    protected function filterRequiresFacetOperator($field)
107    {
108        if (
109            str_starts_with($field, 'LIMIT') ||
110            str_starts_with($field, 'EXPAND') ||
111            str_starts_with($field, 'SEARCHMODE') ||
112            str_starts_with($field, 'PublicationDate')
113        ) {
114            return false;
115        }
116        return true;
117    }
118
119    /**
120     * Return the value for which search view we use
121     *
122     * @return string
123     */
124    public function getView()
125    {
126        $viewArr = explode('|', $this->view ?? '');
127        return $viewArr[0];
128    }
129
130    /**
131     * Get facet operator for the specified field
132     *
133     * @param string $field             Field name
134     * @param string $specifiedOperator Operator specified on a config filter line
135     *
136     * @return string
137     */
138    public function getFacetOperator($field, $specifiedOperator = null)
139    {
140        if ($specifiedOperator && $specifiedOperator != 'AND') {
141            return $specifiedOperator;
142        }
143        if (in_array($field, $this->forcedOrFields)) {
144            return 'OR';
145        }
146        return parent::getFacetOperator($field);
147    }
148}