Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryBuilder
92.00% covered (success)
92.00%
23 / 25
80.00% covered (warning)
80.00%
4 / 5
13.09
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 build
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 queryToEdsQuery
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 abstractQueryToArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 queryGroupToArray
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
5.15
1<?php
2
3/**
4 * EDS API Querybuilder
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;
31
32use VuFindSearch\ParamBag;
33use VuFindSearch\Query\AbstractQuery;
34use VuFindSearch\Query\Query;
35use VuFindSearch\Query\QueryGroup;
36
37/**
38 * EDS API Querybuilder
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   Michelle Milton <mmilton@epnet.com>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org
45 */
46class QueryBuilder
47{
48    /**
49     * Default query (used when query string is empty). This should retrieve all
50     * records in the index, facilitating high-level facet-based browsing.
51     *
52     * @var string
53     */
54    protected $defaultQuery = '(FT yes) OR (FT no)';
55
56    /**
57     * Constructor
58     */
59    public function __construct()
60    {
61    }
62
63    /**
64     * Construct EdsApi search parameters based on a user query and params.
65     *
66     * @param AbstractQuery $query  User query
67     * @param ?ParamBag     $params Search backend parameters
68     *
69     * @return ParamBag
70     *
71     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
72     */
73    public function build(AbstractQuery $query, ?ParamBag $params = null)
74    {
75        // Build base query
76        $queries = $this->abstractQueryToArray($query);
77
78        // Send back results
79        return new ParamBag(['query' => $queries]);
80    }
81
82    /**
83     * Convert a single Query object to an eds api query array
84     *
85     * @param Query  $query    Query to convert
86     * @param string $operator Operator to apply
87     *
88     * @return array
89     */
90    protected function queryToEdsQuery(Query $query, $operator = 'AND')
91    {
92        $expression = $query->getString();
93        $fieldCode = ($query->getHandler() == 'AllFields')
94            ? '' : $query->getHandler();  //fieldcode
95        // Special case: default search
96        if (empty($fieldCode) && empty($expression)) {
97            $expression = $this->defaultQuery;
98        }
99        return json_encode(
100            ['term' => $expression, 'field' => $fieldCode, 'bool' => $operator]
101        );
102    }
103
104    /// Internal API
105
106    /**
107     * Convert an AbstractQuery object to a query string.
108     *
109     * @param AbstractQuery $query Query to convert
110     *
111     * @return array
112     */
113    protected function abstractQueryToArray(AbstractQuery $query)
114    {
115        if ($query instanceof Query) {
116            return ['1' => $this->queryToEdsQuery($query)];
117        } else {
118            return $this->queryGroupToArray($query);
119        }
120    }
121
122    /**
123     * Convert a QueryGroup object to a query string.
124     *
125     * @param QueryGroup $query QueryGroup to convert
126     *
127     * @return array
128     */
129    protected function queryGroupToArray(QueryGroup $query)
130    {
131        $groups = [];
132        foreach ($query->getQueries() as $params) {
133            // Advanced Search
134            if ($params instanceof QueryGroup) {
135                // Process each search group
136                foreach ($params->getQueries() as $q) {
137                    // Build this group individually as a basic search
138                    $op = $q->getOperator();
139                    if ($params->isNegated()) {
140                        $op = 'NOT';
141                    }
142                    $grp  = $this->queryToEdsQuery($q, $op);
143                    $groups[] = $grp;
144                }
145            } else {
146                // Basic Search
147                $groups[] = $this->queryToEdsQuery($params);
148            }
149        }
150        return $groups;
151    }
152}