Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
QueryBuilder
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 build
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 abstractQueryToArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * BrowZine QueryBuilder.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2017.
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   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Backend\BrowZine;
31
32use VuFindSearch\ParamBag;
33use VuFindSearch\Query\AbstractQuery;
34use VuFindSearch\Query\Query;
35
36/**
37 * BrowZine QueryBuilder.
38 *
39 * @category VuFind
40 * @package  Search
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org
44 */
45class QueryBuilder
46{
47    /// Public API
48
49    /**
50     * Return BrowZine search parameters based on a user query and params.
51     *
52     * @param AbstractQuery $query  User query
53     * @param ?ParamBag     $params Search backend parameters
54     *
55     * @return ParamBag
56     *
57     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
58     */
59    public function build(AbstractQuery $query, ?ParamBag $params = null)
60    {
61        // Send back results
62        $q = $this->abstractQueryToArray($query);
63        return new ParamBag(['query' => empty($q) ? '*' : $q]);
64    }
65
66    /// Internal API
67
68    /**
69     * Convert an AbstractQuery object to a query string.
70     *
71     * @param AbstractQuery $query Query to convert
72     *
73     * @return array
74     */
75    protected function abstractQueryToArray(AbstractQuery $query)
76    {
77        if ($query instanceof Query) {
78            return $query->getString();
79        } else {
80            throw new \Exception('Query groups not supported');
81        }
82    }
83}