Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
8 / 10
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryBuilder
80.00% covered (warning)
80.00%
8 / 10
60.00% covered (warning)
60.00%
3 / 5
6.29
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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 abstractQueryToString
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 queryGroupToString
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 queryToString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Pazpar2 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\Pazpar2;
33
34use VuFindSearch\ParamBag;
35use VuFindSearch\Query\AbstractQuery;
36use VuFindSearch\Query\Query;
37use VuFindSearch\Query\QueryGroup;
38
39/**
40 * Pazpar2 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     * Constructor
56     */
57    public function __construct()
58    {
59    }
60
61    /**
62     * Return Pazpar2 search parameters based on a user query and params.
63     *
64     * @param AbstractQuery $query  User query
65     * @param ?ParamBag     $params Search backend parameters
66     *
67     * @return ParamBag
68     *
69     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
70     */
71    public function build(AbstractQuery $query, ?ParamBag $params = null)
72    {
73        // Build base query
74        $queryStr = $this->abstractQueryToString($query);
75
76        // Send back results
77        $newParams = new ParamBag();
78        $newParams->set('query', $queryStr);
79        return $newParams;
80    }
81
82    /// Internal API
83
84    /**
85     * Convert an AbstractQuery object to a query string.
86     *
87     * @param AbstractQuery $query Query to convert
88     *
89     * @return string
90     */
91    protected function abstractQueryToString(AbstractQuery $query)
92    {
93        if ($query instanceof Query) {
94            return $this->queryToString($query);
95        } else {
96            return $this->queryGroupToString($query);
97        }
98    }
99
100    /**
101     * Convert a QueryGroup object to a query string.
102     *
103     * @param QueryGroup $query QueryGroup to convert
104     *
105     * @return string
106     *
107     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
108     */
109    protected function queryGroupToString(QueryGroup $query)
110    {
111        throw new \Exception('Advanced queries not supported.');
112    }
113
114    /**
115     * Convert a single Query object to a query string.
116     *
117     * @param Query $query Query to convert
118     *
119     * @return string
120     */
121    protected function queryToString(Query $query)
122    {
123        return strtolower($query->getString());
124    }
125}