Skip to content

Commit 4b5a8e8

Browse files
authored
Update SearchController.php
1 parent 174f203 commit 4b5a8e8

File tree

1 file changed

+152
-114
lines changed

1 file changed

+152
-114
lines changed

src/SearchController.php

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

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

4848
/**
49-
* @var string Search request query value
49+
* @var string|array Search request query value
5050
*/
51-
private $searchQuery = null;
51+
private string|array $searchQuery = '';
52+
5253

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

5859
/**
5960
* @var string MYSQL database table row for tag value
6061
*/
61-
private $paramTags;
62+
private string $paramTags = '';
6263

6364
/**
64-
* @var string SQL LIKE query operator to be use
65+
* @var string SQL LIKE query operator to be used
6566
*/
66-
private $operators;
67+
private string $operators;
6768

6869
/**
6970
* @var string SQL query prefix
7071
*/
71-
private $queryStart;
72+
private string $queryStart;
7273

7374
/**
7475
* @var string SQL query suffix
7576
*/
76-
private $queryEnd;
77+
private string $queryEnd;
7778

7879
public function __construct(string $algorithm = self::OR) {
7980
$this->searchAlgorithm = $algorithm;
@@ -85,146 +86,181 @@ public function __construct(string $algorithm = self::OR) {
8586
/**
8687
* Set database search table columns.
8788
*
88-
* @param array $param columns
89+
* @param array $param columns
90+
*
91+
* @return self
8992
*/
90-
public function setParameter(array $param=[]): self{
93+
public function setParameter(array $param): self
94+
{
9195
$this->paramArray = $param;
96+
9297
return $this;
9398
}
9499

95100
/**
96101
* Set initial SQL queries.
97102
*
98-
* @param string $query query
103+
* @param string $query query
104+
*
105+
* @return self
99106
*/
100-
public function setSQLQuery(string $query): self{
101-
$this->QueryCondition = $query;
107+
public function setIniQuery(string $query): self
108+
{
109+
$this->queryCondition = $query;
110+
102111
return $this;
103112
}
104113

105114
/**
106115
* Set database search operator pattern.
107116
*
108-
* @param string $pattern name
117+
* @param string $pattern name
118+
*
119+
* @return self
109120
*/
110-
public function setOperators(string $pattern): self{
121+
public function setOperators(string $pattern): self
122+
{
111123
$this->operators = $pattern;
124+
112125
return $this;
113126
}
114127

115128
/**
116129
* Set database tag table column name.
117130
*
118-
* @param string $column name
131+
* @param string $column name
132+
*
133+
* @return self
119134
*/
120-
public function setTags(string $column): self{
135+
public function setTags(string $column): self
136+
{
121137
$this->paramTags = $column;
138+
122139
return $this;
123140
}
124141

125142
/**
126143
* Set search query value.
127144
*
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+
133153
return $this;
134154
}
135155

136156
/**
137157
* Set query prefix string.
138158
*
139-
* @param string $str query prefix
159+
* @param string $start query prefix
160+
*
161+
* @return self
140162
*/
141-
public function setStart(string $str): self{
142-
$this->queryStart = $str;
163+
public function setStart(string $start): self
164+
{
165+
$this->queryStart = $start;
166+
143167
return $this;
144168
}
145169

146170
/**
147171
* Set query suffix string.
148172
*
149-
* @param string $str query suffix
173+
* @param string $end query suffix
174+
*
175+
* @return self
150176
*/
151-
public function setEnd(string $str): self{
152-
$this->queryEnd = $str;
177+
public function setEnd(string $end): self
178+
{
179+
$this->queryEnd = $end;
180+
153181
return $this;
154182
}
155183

156184
/**
157185
* Split search query value by space.
186+
*
187+
* @return void
158188
*/
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) {
161192
$this->searchQuery = explode(" ", $this->searchQuery);
162-
return;
163193
}
164-
$this->searchQuery = [$this->searchQuery];
194+
//$this->searchQuery = [$this->searchQuery];
165195
}
166196

167197
/**
168198
* Create SQL query from the specified pattern.
169199
*
170-
* @param string $value query value
171-
* @return string query
200+
* @param string $value query value
201+
*
202+
* @return string $query
172203
*/
173-
174-
private function format(string $value): string{
175-
$queryString = "";
204+
private function format(string $value): string
205+
{
206+
$query = "";
176207
foreach($this->paramArray as $col){
177208
$sqlQuery = str_replace("query", $value, $this->operators);
178-
$queryString .= $col . " {$this->queryStart} '{$sqlQuery}' {$this->queryEnd} ";
209+
$query .= "LOWER($col) {$this->queryStart} '{$sqlQuery}' {$this->queryEnd} ";
179210
}
180-
return $queryString;
211+
return $query;
181212
}
182213

183214
/**
184-
* Get query from string
215+
* Build query string
216+
*
185217
* @return string query
186-
*/
187-
private function getQueryFromString(): string{
218+
*/
219+
private function buildQuery(): string
220+
{
188221
return rtrim($this->format($this->searchQuery) , " {$this->queryEnd} ");
189222
}
190223

191224
/**
192-
* Get query from array
193-
*
194-
* @param int $index query index
225+
* Build query array
226+
*
227+
* @param int $index array index
228+
*
195229
* @return string query
196-
*/
197-
private function getQueryFromArray(int $index = 0): string {
230+
*/
231+
private function buildArrayQuery(int $index = 0): string
232+
{
198233
return rtrim($this->format($this->searchQuery[$index]) , " {$this->queryEnd} ");;
199234
}
200235

201236
/**
202-
* Determine which search method to use while creating query.
237+
* Determine which search method to use while creating a query.
203238
*
204239
* @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 === ''){
218245
if(is_array($this->searchQuery)) {
219246
$arrayCount = count($this->searchQuery);
220247
for ($i = 0; $i < $arrayCount; $i++) {
221-
$sql .= $this->getQueryFromArray($i);
248+
$sql .= $this->buildArrayQuery($i);
222249
if ($i != $arrayCount - 1) {
223250
$sql .= " {$this->queryEnd} ";
224251
}
225252
}
226253
} 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})";
228264
}
229265
}
230266
return $sql;
@@ -234,54 +270,56 @@ private function buildSearchQueryMethod(): string{
234270
* Execute search query.
235271
*
236272
* @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 (";
284284
}
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;
286324
}
287325
}

0 commit comments

Comments
 (0)