Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.86% |
13 / 14 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
QueryBuilder | |
92.86% |
13 / 14 |
|
80.00% |
4 / 5 |
7.02 | |
0.00% |
0 / 1 |
build | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
setDefaultWidgetType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
abstractQueryToArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
queryGroupToArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
queryToArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * LibGuides 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 Chelsea Lobdell <clobdel1@swarthmore.edu> |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org |
29 | */ |
30 | |
31 | namespace VuFindSearch\Backend\LibGuides; |
32 | |
33 | use VuFindSearch\ParamBag; |
34 | use VuFindSearch\Query\AbstractQuery; |
35 | use VuFindSearch\Query\Query; |
36 | use VuFindSearch\Query\QueryGroup; |
37 | |
38 | /** |
39 | * LibGuides QueryBuilder. |
40 | * |
41 | * @category VuFind |
42 | * @package Search |
43 | * @author Chelsea Lobdell <clobdel1@swarthmore.edu> |
44 | * @author Demian Katz <demian.katz@villanova.edu> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org |
47 | */ |
48 | class QueryBuilder |
49 | { |
50 | /** |
51 | * LibGuides widget type |
52 | * |
53 | * 1 = Research Guides |
54 | * 2 = Database A-Z List |
55 | * |
56 | * @var string |
57 | */ |
58 | protected $widgetType = '1'; |
59 | |
60 | /// Public API |
61 | |
62 | /** |
63 | * Return LibGuides search parameters based on a user query and params. |
64 | * |
65 | * @param AbstractQuery $query User query |
66 | * @param ?ParamBag $params Search backend parameters |
67 | * |
68 | * @return ParamBag |
69 | * |
70 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
71 | */ |
72 | public function build(AbstractQuery $query, ?ParamBag $params = null) |
73 | { |
74 | // Send back results |
75 | $newParams = new ParamBag(); |
76 | |
77 | // Convert the query to an array, then flatten that to a string |
78 | // (right now, we're ignoring a lot of data -- we may want to |
79 | // revisit this and see if more detail can be utilized). |
80 | $array = $this->abstractQueryToArray($query); |
81 | if (isset($array[0]['lookfor'])) { |
82 | $newParams->set('search', $array[0]['lookfor']); |
83 | } |
84 | $newParams->set('widget_type', $this->widgetType); |
85 | |
86 | return $newParams; |
87 | } |
88 | |
89 | /** |
90 | * Set the widget type for this QueryBuilder instance. See $widgetType. |
91 | * |
92 | * @param string $type Widget type |
93 | * |
94 | * @return void |
95 | */ |
96 | public function setDefaultWidgetType($type) |
97 | { |
98 | $this->widgetType = $type; |
99 | } |
100 | |
101 | /// Internal API |
102 | |
103 | /** |
104 | * Convert an AbstractQuery object to a query string. |
105 | * |
106 | * @param AbstractQuery $query Query to convert |
107 | * |
108 | * @return array |
109 | */ |
110 | protected function abstractQueryToArray(AbstractQuery $query) |
111 | { |
112 | if ($query instanceof Query) { |
113 | return $this->queryToArray($query); |
114 | } else { |
115 | return $this->queryGroupToArray($query); |
116 | } |
117 | } |
118 | |
119 | /** |
120 | * Convert a QueryGroup object to a query string. |
121 | * |
122 | * @param QueryGroup $query QueryGroup to convert |
123 | * |
124 | * @return array |
125 | * |
126 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
127 | */ |
128 | protected function queryGroupToArray(QueryGroup $query) |
129 | { |
130 | throw new \Exception('Advanced search not supported.'); |
131 | } |
132 | |
133 | /** |
134 | * Convert a single Query object to a query string. |
135 | * |
136 | * @param Query $query Query to convert |
137 | * |
138 | * @return array |
139 | */ |
140 | protected function queryToArray(Query $query) |
141 | { |
142 | // Clean and validate input: |
143 | $index = $query->getHandler(); |
144 | $lookfor = $query->getString(); |
145 | return [compact('index', 'lookfor')]; |
146 | } |
147 | } |