Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
Query | |
100.00% |
17 / 17 |
|
100.00% |
10 / 10 |
15 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
setString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOperator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOperator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
containsTerm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getAllTerms | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
replaceTerm | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * A single/simple query. |
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 David Maus <maus@hab.de> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org |
28 | */ |
29 | |
30 | namespace VuFindSearch\Query; |
31 | |
32 | /** |
33 | * A single/simple query. |
34 | * |
35 | * @category VuFind |
36 | * @package Search |
37 | * @author David Maus <maus@hab.de> |
38 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
39 | * @link https://vufind.org |
40 | */ |
41 | class Query extends AbstractQuery |
42 | { |
43 | /** |
44 | * Name of query handler, if any. |
45 | * |
46 | * @var string |
47 | */ |
48 | protected $queryHandler; |
49 | |
50 | /** |
51 | * Query string |
52 | * |
53 | * @var string |
54 | */ |
55 | protected $queryString; |
56 | |
57 | /** |
58 | * Operator to apply to query string (null if not applicable) |
59 | * |
60 | * @var string |
61 | */ |
62 | protected $operator; |
63 | |
64 | /** |
65 | * Constructor. |
66 | * |
67 | * @param string $string Search string |
68 | * @param string $handler Name of search handler |
69 | * @param string $operator Operator to apply to query string (null if n/a) |
70 | */ |
71 | public function __construct($string = '', $handler = null, $operator = null) |
72 | { |
73 | $this->queryHandler = $handler ? $handler : null; |
74 | $this->queryString = $string; |
75 | $this->operator = $operator; |
76 | } |
77 | |
78 | /** |
79 | * Return search string (optionally applying a normalization callback) |
80 | * |
81 | * @param callable $normalizer Function to normalize text strings (null for |
82 | * no normalization) |
83 | * |
84 | * @return string |
85 | */ |
86 | public function getString($normalizer = null) |
87 | { |
88 | return $normalizer ? $normalizer($this->queryString) : $this->queryString; |
89 | } |
90 | |
91 | /** |
92 | * Set the search string. |
93 | * |
94 | * @param string $string New search string |
95 | * |
96 | * @return void |
97 | */ |
98 | public function setString($string) |
99 | { |
100 | $this->queryString = $string; |
101 | } |
102 | |
103 | /** |
104 | * Return name of search handler. |
105 | * |
106 | * @return string |
107 | */ |
108 | public function getHandler() |
109 | { |
110 | return $this->queryHandler; |
111 | } |
112 | |
113 | /** |
114 | * Set name of search handler. |
115 | * |
116 | * @param string $handler Name of handler |
117 | * |
118 | * @return void |
119 | */ |
120 | public function setHandler($handler) |
121 | { |
122 | $this->queryHandler = $handler; |
123 | } |
124 | |
125 | /** |
126 | * Return operator (null if n/a). |
127 | * |
128 | * @return string |
129 | */ |
130 | public function getOperator() |
131 | { |
132 | return $this->operator; |
133 | } |
134 | |
135 | /** |
136 | * Set operator (null if n/a). |
137 | * |
138 | * @param string $operator Operator |
139 | * |
140 | * @return void |
141 | */ |
142 | public function setOperator($operator) |
143 | { |
144 | $this->operator = $operator; |
145 | } |
146 | |
147 | /** |
148 | * Does the query contain the specified term? An optional normalizer can be |
149 | * provided to allow for fuzzier matching. |
150 | * |
151 | * @param string $needle Term to check |
152 | * @param callable $normalizer Function to normalize text strings (null for |
153 | * no normalization) |
154 | * |
155 | * @return bool |
156 | */ |
157 | public function containsTerm($needle, $normalizer = null) |
158 | { |
159 | // Escape characters with special meaning in regular expressions to avoid |
160 | // errors: |
161 | $needle = preg_quote($normalizer ? $normalizer($needle) : $needle, '/'); |
162 | |
163 | return (bool)preg_match("/\b$needle\b/u", $this->getString($normalizer)); |
164 | } |
165 | |
166 | /** |
167 | * Get a concatenated list of all query strings within the object. |
168 | * |
169 | * @return string |
170 | */ |
171 | public function getAllTerms() |
172 | { |
173 | return $this->getString(); |
174 | } |
175 | |
176 | /** |
177 | * Replace a term. |
178 | * |
179 | * @param string $from Search term to find |
180 | * @param string $to Search term to insert |
181 | * @param callable $normalizer Function to normalize text strings (null for |
182 | * no normalization) |
183 | * |
184 | * @return void |
185 | */ |
186 | public function replaceTerm($from, $to, $normalizer = null) |
187 | { |
188 | // Escape $from so it is regular expression safe (just in case it |
189 | // includes any weird punctuation -- unlikely but possible): |
190 | $from = preg_quote($normalizer ? $normalizer($from) : $from, '/'); |
191 | $queryString = $this->getString($normalizer); |
192 | |
193 | // Try to match within word boundaries to prevent the replacement from |
194 | // affecting unexpected parts of the search query; if that fails to change |
195 | // anything, try again with a less restricted regular expression. The fall- |
196 | // back is needed when $from contains punctuation characters such as commas. |
197 | $this->queryString = preg_replace("/\b$from\b/i", $to, $queryString); |
198 | if ($queryString === $this->queryString) { |
199 | $this->queryString = preg_replace("/$from/i", $to, $queryString); |
200 | } |
201 | } |
202 | } |