Skip to content

Commit 174f203

Browse files
authored
Update SearchController.php
1 parent 3e92740 commit 174f203

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

src/SearchController.php

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,42 @@ class SearchController{
3838
/**
3939
* @var string SQL Query
4040
*/
41-
private string $QueryCondition = '';
41+
private $QueryCondition = "";
4242

4343
/**
4444
* @var string Search query algorithm that needs to be used
4545
*/
46-
private string $searchAlgorithm;
46+
private $searchAlgorithm;
4747

4848
/**
4949
* @var string Search request query value
5050
*/
51-
private mixed $searchQuery = '';
52-
51+
private $searchQuery = null;
5352

5453
/**
55-
* @var array MYSQL database table rows to search form
54+
* @var array MYSQL database table rows to search from
5655
*/
57-
private array $paramArray = [];
56+
private $paramArray = [];
5857

5958
/**
6059
* @var string MYSQL database table row for tag value
6160
*/
62-
private string $paramTags;
61+
private $paramTags;
6362

6463
/**
65-
* @var string SQL LIKE query operator to be used
64+
* @var string SQL LIKE query operator to be use
6665
*/
67-
private string $operators;
66+
private $operators;
6867

6968
/**
7069
* @var string SQL query prefix
7170
*/
72-
private string $queryStart;
71+
private $queryStart;
7372

7473
/**
7574
* @var string SQL query suffix
7675
*/
77-
private string $queryEnd;
76+
private $queryEnd;
7877

7978
public function __construct(string $algorithm = self::OR) {
8079
$this->searchAlgorithm = $algorithm;
@@ -88,35 +87,39 @@ public function __construct(string $algorithm = self::OR) {
8887
*
8988
* @param array $param columns
9089
*/
91-
public function setParameter(array $param=[]): void{
90+
public function setParameter(array $param=[]): self{
9291
$this->paramArray = $param;
92+
return $this;
9393
}
9494

9595
/**
9696
* Set initial SQL queries.
9797
*
9898
* @param string $query query
9999
*/
100-
public function setSQLQuery(string $query): void{
100+
public function setSQLQuery(string $query): self{
101101
$this->QueryCondition = $query;
102+
return $this;
102103
}
103104

104105
/**
105106
* Set database search operator pattern.
106107
*
107108
* @param string $pattern name
108109
*/
109-
public function setOperators(string $pattern): void{
110+
public function setOperators(string $pattern): self{
110111
$this->operators = $pattern;
112+
return $this;
111113
}
112114

113115
/**
114116
* Set database tag table column name.
115117
*
116118
* @param string $column name
117119
*/
118-
public function setTags(string $column): void{
120+
public function setTags(string $column): self{
119121
$this->paramTags = $column;
122+
return $this;
120123
}
121124

122125
/**
@@ -125,27 +128,29 @@ public function setTags(string $column): void{
125128
* @param string $query query value
126129
* @return object|SearchController
127130
*/
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");
130133
return $this;
131134
}
132135

133136
/**
134137
* Set query prefix string.
135138
*
136-
* @param string $start query prefix
139+
* @param string $str query prefix
137140
*/
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;
140144
}
141145

142146
/**
143147
* Set query suffix string.
144148
*
145-
* @param string $end query suffix
149+
* @param string $str query suffix
146150
*/
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;
149154
}
150155

151156
/**
@@ -154,8 +159,9 @@ public function setEnd(string $end): void{
154159
public function split(): void{
155160
if(strpos($this->searchQuery, " ") !== false) {
156161
$this->searchQuery = explode(" ", $this->searchQuery);
162+
return;
157163
}
158-
//$this->searchQuery = [$this->searchQuery];
164+
$this->searchQuery = [$this->searchQuery];
159165
}
160166

161167
/**
@@ -165,29 +171,39 @@ public function split(): void{
165171
* @return string query
166172
*/
167173

168-
private function format(string $value): string {
174+
private function format(string $value): string{
169175
$queryString = "";
170176
foreach($this->paramArray as $col){
171177
$sqlQuery = str_replace("query", $value, $this->operators);
172-
$queryString .= "LOWER($col) {$this->queryStart} '{$sqlQuery}' {$this->queryEnd} ";
178+
$queryString .= $col . " {$this->queryStart} '{$sqlQuery}' {$this->queryEnd} ";
173179
}
174180
return $queryString;
175181
}
176182

177-
private function buildQuery(): string{
183+
/**
184+
* Get query from string
185+
* @return string query
186+
*/
187+
private function getQueryFromString(): string{
178188
return rtrim($this->format($this->searchQuery) , " {$this->queryEnd} ");
179189
}
180190

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} ");;
183199
}
184200

185201
/**
186-
* Determine which search method to use while creating a query.
202+
* Determine which search method to use while creating query.
187203
*
188204
* @return string SQL query
189205
*/
190-
private function buildSQL(): string{
206+
private function buildSearchQueryMethod(): string{
191207
$sql = "";
192208
if(!empty($this->paramTags)){
193209
if(is_array($this->searchQuery)) {
@@ -202,13 +218,13 @@ private function buildSQL(): string{
202218
if(is_array($this->searchQuery)) {
203219
$arrayCount = count($this->searchQuery);
204220
for ($i = 0; $i < $arrayCount; $i++) {
205-
$sql .= $this->buildArrayQuery($i);
221+
$sql .= $this->getQueryFromArray($i);
206222
if ($i != $arrayCount - 1) {
207223
$sql .= " {$this->queryEnd} ";
208224
}
209225
}
210226
} else {
211-
$sql .= $this->buildQuery();
227+
$sql .= $this->getQueryFromString();
212228
}
213229
}
214230
return $sql;
@@ -233,34 +249,34 @@ public function getQuery(): string{
233249
case self::OR:
234250
$this->setStart(self::LIKE);
235251
$this->setEnd(self::OR);
236-
$this->QueryCondition .= $this->buildSQL();
252+
$this->QueryCondition .= $this->buildSearchQueryMethod();
237253
$this->QueryCondition .= " )";
238254
break;
239255

240256
case self::AND:
241257
$this->setStart(self::LIKE);
242258
$this->setEnd(self::AND);
243-
$this->QueryCondition .= $this->buildSQL();
259+
$this->QueryCondition .= $this->buildSearchQueryMethod();
244260
$this->QueryCondition .= " )";
245261
break;
246262

247263
case self::NAND:
248264
$this->setStart(self::NOT_LIKE);
249265
$this->setEnd(self::AND);
250-
$this->QueryCondition .= $this->buildSQL();
266+
$this->QueryCondition .= $this->buildSearchQueryMethod();
251267
$this->QueryCondition .= " )";
252268
break;
253269

254270
case self::NOR:
255271
$this->setStart(self::NOT_LIKE);
256272
$this->setEnd(self::OR);
257-
$this->QueryCondition .= $this->buildSQL();
273+
$this->QueryCondition .= $this->buildSearchQueryMethod();
258274
$this->QueryCondition .= " )";
259275
break;
260276
default:
261277
$this->setStart(self::LIKE);
262278
$this->setEnd(self::OR);
263-
$this->QueryCondition .= $this->buildSQL();
279+
$this->QueryCondition .= $this->buildSearchQueryMethod();
264280
$this->QueryCondition .= " )";
265281
break;
266282
}

0 commit comments

Comments
 (0)