@@ -38,42 +38,43 @@ class SearchController{
38
38
/**
39
39
* @var string SQL Query
40
40
*/
41
- private $ QueryCondition = "" ;
41
+ private string $ queryCondition = '' ;
42
42
43
43
/**
44
44
* @var string Search query algorithm that needs to be used
45
45
*/
46
- private $ searchAlgorithm ;
46
+ private string $ searchAlgorithm ;
47
47
48
48
/**
49
- * @var string Search request query value
49
+ * @var string|array Search request query value
50
50
*/
51
- private $ searchQuery = null ;
51
+ private string |array $ searchQuery = '' ;
52
+
52
53
53
54
/**
54
- * @var array MYSQL database table rows to search from
55
+ * @var array MYSQL database table rows to search form
55
56
*/
56
- private $ paramArray = [];
57
+ private array $ paramArray = [];
57
58
58
59
/**
59
60
* @var string MYSQL database table row for tag value
60
61
*/
61
- private $ paramTags ;
62
+ private string $ paramTags = '' ;
62
63
63
64
/**
64
- * @var string SQL LIKE query operator to be use
65
+ * @var string SQL LIKE query operator to be used
65
66
*/
66
- private $ operators ;
67
+ private string $ operators ;
67
68
68
69
/**
69
70
* @var string SQL query prefix
70
71
*/
71
- private $ queryStart ;
72
+ private string $ queryStart ;
72
73
73
74
/**
74
75
* @var string SQL query suffix
75
76
*/
76
- private $ queryEnd ;
77
+ private string $ queryEnd ;
77
78
78
79
public function __construct (string $ algorithm = self ::OR ) {
79
80
$ this ->searchAlgorithm = $ algorithm ;
@@ -85,146 +86,181 @@ public function __construct(string $algorithm = self::OR) {
85
86
/**
86
87
* Set database search table columns.
87
88
*
88
- * @param array $param columns
89
+ * @param array $param columns
90
+ *
91
+ * @return self
89
92
*/
90
- public function setParameter (array $ param =[]): self {
93
+ public function setParameter (array $ param ): self
94
+ {
91
95
$ this ->paramArray = $ param ;
96
+
92
97
return $ this ;
93
98
}
94
99
95
100
/**
96
101
* Set initial SQL queries.
97
102
*
98
- * @param string $query query
103
+ * @param string $query query
104
+ *
105
+ * @return self
99
106
*/
100
- public function setSQLQuery (string $ query ): self {
101
- $ this ->QueryCondition = $ query ;
107
+ public function setIniQuery (string $ query ): self
108
+ {
109
+ $ this ->queryCondition = $ query ;
110
+
102
111
return $ this ;
103
112
}
104
113
105
114
/**
106
115
* Set database search operator pattern.
107
116
*
108
- * @param string $pattern name
117
+ * @param string $pattern name
118
+ *
119
+ * @return self
109
120
*/
110
- public function setOperators (string $ pattern ): self {
121
+ public function setOperators (string $ pattern ): self
122
+ {
111
123
$ this ->operators = $ pattern ;
124
+
112
125
return $ this ;
113
126
}
114
127
115
128
/**
116
129
* Set database tag table column name.
117
130
*
118
- * @param string $column name
131
+ * @param string $column name
132
+ *
133
+ * @return self
119
134
*/
120
- public function setTags (string $ column ): self {
135
+ public function setTags (string $ column ): self
136
+ {
121
137
$ this ->paramTags = $ column ;
138
+
122
139
return $ this ;
123
140
}
124
141
125
142
/**
126
143
* Set search query value.
127
144
*
128
- * @param string $query query value
129
- * @return object|SearchController
130
- */
131
- public function setQuery (mixed $ query ): self {
132
- $ this ->searchQuery = htmlspecialchars ((string ) $ query , ENT_QUOTES , "UTF-8 " );
145
+ * @param string $query query value
146
+ *
147
+ * @return self
148
+ */
149
+ public function setQuery (string $ query ): self
150
+ {
151
+ $ this ->searchQuery = strtolower (htmlspecialchars ($ query , ENT_QUOTES , "UTF-8 " ));
152
+
133
153
return $ this ;
134
154
}
135
155
136
156
/**
137
157
* Set query prefix string.
138
158
*
139
- * @param string $str query prefix
159
+ * @param string $start query prefix
160
+ *
161
+ * @return self
140
162
*/
141
- public function setStart (string $ str ): self {
142
- $ this ->queryStart = $ str ;
163
+ public function setStart (string $ start ): self
164
+ {
165
+ $ this ->queryStart = $ start ;
166
+
143
167
return $ this ;
144
168
}
145
169
146
170
/**
147
171
* Set query suffix string.
148
172
*
149
- * @param string $str query suffix
173
+ * @param string $end query suffix
174
+ *
175
+ * @return self
150
176
*/
151
- public function setEnd (string $ str ): self {
152
- $ this ->queryEnd = $ str ;
177
+ public function setEnd (string $ end ): self
178
+ {
179
+ $ this ->queryEnd = $ end ;
180
+
153
181
return $ this ;
154
182
}
155
183
156
184
/**
157
185
* Split search query value by space.
186
+ *
187
+ * @return void
158
188
*/
159
- public function split (): void {
160
- if (strpos ($ this ->searchQuery , " " ) !== false ) {
189
+ public function split (): void
190
+ {
191
+ if (is_string ($ this ->searchQuery ) && strpos ($ this ->searchQuery , " " ) !== false ) {
161
192
$ this ->searchQuery = explode (" " , $ this ->searchQuery );
162
- return ;
163
193
}
164
- $ this ->searchQuery = [$ this ->searchQuery ];
194
+ // $this->searchQuery = [$this->searchQuery];
165
195
}
166
196
167
197
/**
168
198
* Create SQL query from the specified pattern.
169
199
*
170
- * @param string $value query value
171
- * @return string query
200
+ * @param string $value query value
201
+ *
202
+ * @return string $query
172
203
*/
173
-
174
- private function format ( string $ value ): string {
175
- $ queryString = "" ;
204
+ private function format ( string $ value ): string
205
+ {
206
+ $ query = "" ;
176
207
foreach ($ this ->paramArray as $ col ){
177
208
$ sqlQuery = str_replace ("query " , $ value , $ this ->operators );
178
- $ queryString .= $ col . " {$ this ->queryStart } ' {$ sqlQuery }' {$ this ->queryEnd } " ;
209
+ $ query .= " LOWER( $ col) {$ this ->queryStart } ' {$ sqlQuery }' {$ this ->queryEnd } " ;
179
210
}
180
- return $ queryString ;
211
+ return $ query ;
181
212
}
182
213
183
214
/**
184
- * Get query from string
215
+ * Build query string
216
+ *
185
217
* @return string query
186
- */
187
- private function getQueryFromString (): string {
218
+ */
219
+ private function buildQuery (): string
220
+ {
188
221
return rtrim ($ this ->format ($ this ->searchQuery ) , " {$ this ->queryEnd } " );
189
222
}
190
223
191
224
/**
192
- * Get query from array
193
- *
194
- * @param int $index query index
225
+ * Build query array
226
+ *
227
+ * @param int $index array index
228
+ *
195
229
* @return string query
196
- */
197
- private function getQueryFromArray (int $ index = 0 ): string {
230
+ */
231
+ private function buildArrayQuery (int $ index = 0 ): string
232
+ {
198
233
return rtrim ($ this ->format ($ this ->searchQuery [$ index ]) , " {$ this ->queryEnd } " );;
199
234
}
200
235
201
236
/**
202
- * Determine which search method to use while creating query.
237
+ * Determine which search method to use while creating a query.
203
238
*
204
239
* @return string SQL query
205
- */
206
- private function buildSearchQueryMethod (): string {
207
- $ sql = "" ;
208
- if (!empty ($ this ->paramTags )){
209
- if (is_array ($ this ->searchQuery )) {
210
- foreach ($ this ->searchQuery as $ tag ){
211
- $ sql .= "FIND_IN_SET(' {$ tag }', {$ this ->paramTags }) {$ this ->queryEnd } " ;
212
- }
213
- $ sql = rtrim ($ sql , " {$ this ->queryEnd } " );
214
- }else {
215
- $ sql .= "FIND_IN_SET(' {$ this ->searchQuery }', {$ this ->paramTags }) " ;
216
- }
217
- }else {
240
+ */
241
+ private function buildSQL (): string
242
+ {
243
+ $ sql = '' ;
244
+ if ($ this ->paramTags === '' ){
218
245
if (is_array ($ this ->searchQuery )) {
219
246
$ arrayCount = count ($ this ->searchQuery );
220
247
for ($ i = 0 ; $ i < $ arrayCount ; $ i ++) {
221
- $ sql .= $ this ->getQueryFromArray ($ i );
248
+ $ sql .= $ this ->buildArrayQuery ($ i );
222
249
if ($ i != $ arrayCount - 1 ) {
223
250
$ sql .= " {$ this ->queryEnd } " ;
224
251
}
225
252
}
226
253
} else {
227
- $ sql .= $ this ->getQueryFromString ();
254
+ $ sql .= $ this ->buildQuery ();
255
+ }
256
+ }else {
257
+ if (is_array ($ this ->searchQuery )) {
258
+ foreach ($ this ->searchQuery as $ tag ){
259
+ $ sql .= "FIND_IN_SET(' {$ tag }', {$ this ->paramTags }) {$ this ->queryEnd } " ;
260
+ }
261
+ $ sql = rtrim ($ sql , " {$ this ->queryEnd } " );
262
+ }else {
263
+ $ sql .= "FIND_IN_SET(' {$ this ->searchQuery }', {$ this ->paramTags }) " ;
228
264
}
229
265
}
230
266
return $ sql ;
@@ -234,54 +270,56 @@ private function buildSearchQueryMethod(): string{
234
270
* Execute search query.
235
271
*
236
272
* @return string SQL query
237
- */
238
- public function getQuery (): string {
239
- if (!empty ($ this ->searchQuery )){
240
- if (!empty ($ this ->searchQuery )){
241
- if (!empty ($ this ->QueryCondition )){
242
- $ this ->QueryCondition .= " AND ( " ;
243
- }else {
244
- $ this ->QueryCondition .= " WHERE ( " ;
245
- }
246
-
247
- switch ($ this ->searchAlgorithm ){
248
-
249
- case self ::OR :
250
- $ this ->setStart (self ::LIKE );
251
- $ this ->setEnd (self ::OR );
252
- $ this ->QueryCondition .= $ this ->buildSearchQueryMethod ();
253
- $ this ->QueryCondition .= " ) " ;
254
- break ;
255
-
256
- case self ::AND :
257
- $ this ->setStart (self ::LIKE );
258
- $ this ->setEnd (self ::AND );
259
- $ this ->QueryCondition .= $ this ->buildSearchQueryMethod ();
260
- $ this ->QueryCondition .= " ) " ;
261
- break ;
262
-
263
- case self ::NAND :
264
- $ this ->setStart (self ::NOT_LIKE );
265
- $ this ->setEnd (self ::AND );
266
- $ this ->QueryCondition .= $ this ->buildSearchQueryMethod ();
267
- $ this ->QueryCondition .= " ) " ;
268
- break ;
269
-
270
- case self ::NOR :
271
- $ this ->setStart (self ::NOT_LIKE );
272
- $ this ->setEnd (self ::OR );
273
- $ this ->QueryCondition .= $ this ->buildSearchQueryMethod ();
274
- $ this ->QueryCondition .= " ) " ;
275
- break ;
276
- default :
277
- $ this ->setStart (self ::LIKE );
278
- $ this ->setEnd (self ::OR );
279
- $ this ->QueryCondition .= $ this ->buildSearchQueryMethod ();
280
- $ this ->QueryCondition .= " ) " ;
281
- break ;
282
- }
283
- }
273
+ */
274
+ public function getQuery (): string
275
+ {
276
+ if ($ this ->searchQuery === '' || $ this ->searchQuery === []){
277
+ return $ this ->queryCondition ;
278
+ }
279
+
280
+ if ($ this ->queryCondition === '' ){
281
+ $ this ->queryCondition .= " WHERE ( " ;
282
+ }else {
283
+ $ this ->queryCondition .= " AND ( " ;
284
284
}
285
- return $ this ->QueryCondition ;
285
+
286
+ switch ($ this ->searchAlgorithm ){
287
+
288
+ case self ::OR :
289
+ $ this ->setStart (self ::LIKE );
290
+ $ this ->setEnd (self ::OR );
291
+ $ this ->queryCondition .= $ this ->buildSQL ();
292
+ $ this ->queryCondition .= " ) " ;
293
+ break ;
294
+
295
+ case self ::AND :
296
+ $ this ->setStart (self ::LIKE );
297
+ $ this ->setEnd (self ::AND );
298
+ $ this ->queryCondition .= $ this ->buildSQL ();
299
+ $ this ->queryCondition .= " ) " ;
300
+ break ;
301
+
302
+ case self ::NAND :
303
+ $ this ->setStart (self ::NOT_LIKE );
304
+ $ this ->setEnd (self ::AND );
305
+ $ this ->queryCondition .= $ this ->buildSQL ();
306
+ $ this ->queryCondition .= " ) " ;
307
+ break ;
308
+
309
+ case self ::NOR :
310
+ $ this ->setStart (self ::NOT_LIKE );
311
+ $ this ->setEnd (self ::OR );
312
+ $ this ->queryCondition .= $ this ->buildSQL ();
313
+ $ this ->queryCondition .= " ) " ;
314
+ break ;
315
+ default :
316
+ $ this ->setStart (self ::LIKE );
317
+ $ this ->setEnd (self ::OR );
318
+ $ this ->queryCondition .= $ this ->buildSQL ();
319
+ $ this ->queryCondition .= " ) " ;
320
+ break ;
321
+ }
322
+
323
+ return $ this ->queryCondition ;
286
324
}
287
325
}
0 commit comments