|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Folk; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class to manage a query to search rows. |
|
7
|
|
|
*/ |
|
8
|
|
|
class SearchQuery |
|
9
|
|
|
{ |
|
10
|
|
|
protected $limit = 50; |
|
11
|
|
|
protected $page; |
|
12
|
|
|
protected $ids = []; |
|
13
|
|
|
protected $conditions = []; |
|
14
|
|
|
protected $words = []; |
|
15
|
|
|
protected $sort; |
|
16
|
|
|
protected $direction; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $query |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(array $query = []) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!empty($query['query'])) { |
|
24
|
|
|
$this->setQuery($query['query']); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (!empty($query['page'])) { |
|
28
|
|
|
$this->setPage($query['page']); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!empty($query['sort'])) { |
|
32
|
|
|
$this->setSortAndDirection($query['sort'], isset($query['direction']) ? $query['direction'] : null); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the a query. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $query |
|
40
|
|
|
* |
|
41
|
|
|
* @return self |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setQuery($query) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->conditions = $this->ids = $this->words = []; |
|
46
|
|
|
|
|
47
|
|
|
preg_match_all('/([\w]+:)?("([^"]*)"|([^ ]*))/', trim($query), $pieces, PREG_SET_ORDER); |
|
48
|
|
|
|
|
49
|
|
|
if (is_array($pieces)) { |
|
50
|
|
|
foreach ($pieces as $piece) { |
|
51
|
|
|
if (empty($piece[0])) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$name = $piece[1] ? substr($piece[1], 0, -1) : null; |
|
56
|
|
|
$value = isset($piece[4]) ? $piece[4] : $piece[3]; |
|
57
|
|
|
|
|
58
|
|
|
if ($name !== null) { |
|
59
|
|
|
if (!isset($this->conditions[$name])) { |
|
60
|
|
|
$this->conditions[$name] = [$value]; |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->conditions[$name][] = $value; |
|
63
|
|
|
} |
|
64
|
|
|
} elseif (preg_match('/^#[\w-]+$/', $value)) { |
|
65
|
|
|
$this->ids[] = substr($value, 1); |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->words[] = $value; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the query as string. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getQuery() |
|
81
|
|
|
{ |
|
82
|
|
|
$query = implode(' ', $this->words); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($this->ids as $id) { |
|
85
|
|
|
$query .= " #{$id}"; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
foreach ($this->conditions as $name => $values) { |
|
89
|
|
|
foreach ($values as $value) { |
|
90
|
|
|
if (strpos($value, ' ') === false) { |
|
91
|
|
|
$query .= " {$name}:{$value}"; |
|
92
|
|
|
} else { |
|
93
|
|
|
$query .= " {$name}:\"{$value}\""; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return trim($query); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the page number. |
|
103
|
|
|
* |
|
104
|
|
|
* @return null|int |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getPage() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->page; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set the page. |
|
113
|
|
|
* |
|
114
|
|
|
* @param null|int $page |
|
115
|
|
|
* |
|
116
|
|
|
* @return self |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setPage($page) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->page = (int) $page; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the limit of results per page. |
|
127
|
|
|
* |
|
128
|
|
|
* @return int |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getLimit() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->limit; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set the limit of results per page. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int $limit |
|
139
|
|
|
* |
|
140
|
|
|
* @return self |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setLimit($limit) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->limit = (int) $limit; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns all ids found. |
|
151
|
|
|
* |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getIds() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->ids; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set new ids. |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $ids |
|
163
|
|
|
* |
|
164
|
|
|
* @return self |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setIds(array $ids) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->ids = $ids; |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Returns all words in the query. |
|
175
|
|
|
* |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getWords() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->words; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Set new words. |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $words |
|
187
|
|
|
* |
|
188
|
|
|
* @return self |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setWords(array $words) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->words = $words; |
|
193
|
|
|
|
|
194
|
|
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Set new conditions. |
|
199
|
|
|
* |
|
200
|
|
|
* @param array $conditions |
|
201
|
|
|
* |
|
202
|
|
|
* @return self |
|
203
|
|
|
*/ |
|
204
|
|
|
public function setConditions(array $conditions) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
$this->conditions[$name] = $value; |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Return all conditions. |
|
213
|
|
|
* |
|
214
|
|
|
* @return array |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getConditions() |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->conditions; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Set the sort and direction fields. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $sort |
|
225
|
|
|
* @param string $direction |
|
226
|
|
|
* |
|
227
|
|
|
* @return self |
|
228
|
|
|
*/ |
|
229
|
|
|
public function setSortAndDirection($sort, $direction) |
|
230
|
|
|
{ |
|
231
|
|
|
$this->sort = $this->direction = null; |
|
232
|
|
|
|
|
233
|
|
|
if (!empty($sort)) { |
|
234
|
|
|
$this->sort = $sort; |
|
235
|
|
|
$this->direction = strtoupper($direction); |
|
236
|
|
|
|
|
237
|
|
|
if ($this->direction !== 'DESC') { |
|
238
|
|
|
$this->direction = 'ASC'; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $this; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Return the sort field. |
|
247
|
|
|
* |
|
248
|
|
|
* @return string|null |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getSort() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->sort; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Return the sort direction in UPPERCASE. |
|
257
|
|
|
* |
|
258
|
|
|
* @return string|null |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getDirection() |
|
261
|
|
|
{ |
|
262
|
|
|
return isset($this->direction) ? strtoupper($this->direction) : null; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.