Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
QueryBuilder
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 build
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 abstractQueryToArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 queryGroupToArray
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 queryToArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Primo Central QueryBuilder.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
26 * @author   David Maus <maus@hab.de>
27 * @author   Demian Katz <demian.katz@villanova.edu>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org
30 */
31
32namespace VuFindSearch\Backend\Primo;
33
34use VuFindSearch\ParamBag;
35use VuFindSearch\Query\AbstractQuery;
36use VuFindSearch\Query\Query;
37use VuFindSearch\Query\QueryGroup;
38
39/**
40 * Primo Central QueryBuilder.
41 *
42 * @category VuFind
43 * @package  Search
44 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
45 * @author   David Maus <maus@hab.de>
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org
49 */
50class QueryBuilder
51{
52    /// Public API
53
54    /**
55     * Return Primo search parameters based on a user query and params.
56     *
57     * @param AbstractQuery $query  User query
58     * @param ?ParamBag     $params Search backend parameters
59     *
60     * @return ParamBag
61     *
62     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63     */
64    public function build(AbstractQuery $query, ?ParamBag $params = null)
65    {
66        // Send back results
67        $newParams = new ParamBag();
68        $newParams->set('query', $this->abstractQueryToArray($query));
69        return $newParams;
70    }
71
72    /// Internal API
73
74    /**
75     * Convert an AbstractQuery object to a query string.
76     *
77     * @param AbstractQuery $query Query to convert
78     *
79     * @return array
80     */
81    protected function abstractQueryToArray(AbstractQuery $query)
82    {
83        if ($query instanceof Query) {
84            return $this->queryToArray($query);
85        } else {
86            return $this->queryGroupToArray($query);
87        }
88    }
89
90    /**
91     * Convert a QueryGroup object to a query string.
92     *
93     * @param QueryGroup $query QueryGroup to convert
94     *
95     * @return array
96     */
97    protected function queryGroupToArray(QueryGroup $query)
98    {
99        $nextLevel = $query->getQueries();
100        $parts = [];
101        foreach ($nextLevel[0]->getQueries() as $q) {
102            $index = $q->getHandler();
103            $op = $q->getOperator();
104            $lookfor = $q->getString();
105            $parts[] = compact('index', 'op', 'lookfor');
106        }
107        return $parts;
108    }
109
110    /**
111     * Convert a single Query object to a query string.
112     *
113     * @param Query $query Query to convert
114     *
115     * @return array
116     */
117    protected function queryToArray(Query $query)
118    {
119        // Clean and validate input:
120        $index = $query->getHandler();
121        $lookfor = $query->getString();
122        return [compact('index', 'lookfor')];
123    }
124}