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