Skip to content

Commit 3546ba9

Browse files
authored
Update SearchController.php
1 parent e57e979 commit 3546ba9

File tree

1 file changed

+94
-106
lines changed

1 file changed

+94
-106
lines changed

src/SearchController.php

+94-106
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212
class SearchController{
1313
/**
14-
* @var string SQL patterns
15-
*/
14+
* @var string SQL patterns
15+
*/
1616
public const START_WITH_QUERY = "query%";
1717
public const END_WITH_QUERY = "%query";
1818
public const HAVE_ANY_QUERY = "%query%";
@@ -22,62 +22,59 @@ class SearchController{
2222
public const START_END_WITH_QUERY = "query%query";
2323

2424
/**
25-
* @var string SearchController algorithms
26-
*/
25+
* @var string SearchController algorithms
26+
*/
2727
public const OR = "OR";
2828
public const AND = "AND";
2929
public const NAND = "NAND";
3030
public const NOR = "NOR";
3131

3232
/**
33-
* @var string SQL search keywords
34-
*/
33+
* @var string SQL search keywords
34+
*/
3535
public const LIKE = "LIKE";
3636
public const NOT_LIKE = "NOT LIKE";
3737

3838
/**
39-
* @var string SQL Query
40-
*/
39+
* @var string SQL Query
40+
*/
4141
private $QueryCondition = "";
4242

4343
/**
44-
* @var string Search query algorithm that needs to be used
45-
*/
44+
* @var string Search query algorithm that needs to be used
45+
*/
4646
private $searchAlgorithm;
4747

4848
/**
49-
* @var string Search request query value
50-
*/
49+
* @var string Search request query value
50+
*/
5151
private $searchQuery = null;
5252

5353
/**
54-
* @var array MYSQL database table rows to search from
55-
*/
54+
* @var array MYSQL database table rows to search from
55+
*/
5656
private $paramArray = array();
5757

5858
/**
59-
* @var string MYSQL database table row for tag value
60-
*/
59+
* @var string MYSQL database table row for tag value
60+
*/
6161
private $paramTags;
6262

6363
/**
64-
* @var string SQL LIKE query operator to be use
65-
*/
64+
* @var string SQL LIKE query operator to be use
65+
*/
6666
private $operators;
6767

6868
/**
69-
* @var string SQL query prefix
70-
*/
69+
* @var string SQL query prefix
70+
*/
7171
private $queryStart;
7272

7373
/**
74-
* @var string SQL query suffix
75-
*/
74+
* @var string SQL query suffix
75+
*/
7676
private $queryEnd;
7777

78-
/**
79-
* Constructor
80-
*/
8178
public function __construct($algorithm = self::OR) {
8279
$this->searchAlgorithm = $algorithm;
8380
$this->operators = self::END_WITH_QUERY;
@@ -86,67 +83,78 @@ public function __construct($algorithm = self::OR) {
8683
}
8784

8885
/**
89-
* Set database search table columns.
90-
*
91-
* @param array $param columns
92-
*/
86+
* Set database search table columns.
87+
*
88+
* @param array $param columns
89+
*/
9390
public function setParameter($param=array()){
9491
$this->paramArray = $param;
92+
return $this;
9593
}
9694

9795
/**
98-
* Set initial SQL queries.
99-
* @param string $query query
100-
*/
96+
* Set initial SQL queries.
97+
*
98+
* @param string $query query
99+
*/
101100
public function setSQLQuery($query){
102101
$this->QueryCondition = $query;
103102
}
104103

105104
/**
106-
* Set database search operator pattern.
107-
* @param string $operator name
108-
*/
109-
public function setOperators($operator){
110-
$this->operators = $operator;
105+
* Set database search operator pattern.
106+
*
107+
* @param string $pattern name
108+
*/
109+
public function setOperators($pattern){
110+
$this->operators = $pattern;
111+
return $this;
111112
}
112113

113114
/**
114-
* Set database tag table column name.
115-
* @param string $column name
116-
*/
115+
* Set database tag table column name.
116+
*
117+
* @param string $column name
118+
*/
117119
public function setTags($column){
118120
$this->paramTags = $column;
121+
return $this;
119122
}
120123

121124
/**
122-
* Set search query value.
123-
* @param string $query query value
124-
* @return object|SearchController
125-
*/
125+
* Set search query value.
126+
*
127+
* @param string $query query value
128+
* @return object|SearchController
129+
*/
126130
public function setQuery($query){
127131
$this->searchQuery = htmlspecialchars($query, ENT_QUOTES, "UTF-8");
128132
return $this;
129133
}
130134

131135
/**
132-
* Set query prefix string.
133-
* @param string $str query prefix
134-
*/
136+
* Set query prefix string.
137+
*
138+
* @param string $str query prefix
139+
*/
135140
public function setStart($str){
136141
$this->queryStart = $str;
142+
return $this;
137143
}
138144

139145
/**
140-
* Set query suffix string.
141-
* @param string $str query suffix
142-
*/
146+
* Set query suffix string.
147+
*
148+
* @param string $str query suffix
149+
*/
143150
public function setEnd($str){
144151
$this->queryEnd = $str;
152+
return $this;
145153
}
146154

147155
/**
148-
* Split search query value by space.
149-
*/
156+
* Split search query value by space.
157+
*/
150158
public function split(){
151159
if(strpos($this->searchQuery, " ") !== false) {
152160
$this->searchQuery = explode(" ", $this->searchQuery);
@@ -156,10 +164,11 @@ public function split(){
156164
}
157165

158166
/**
159-
* Create SQL query from the specified pattern.
160-
* @param string $value query value
161-
* @return string query
162-
*/
167+
* Create SQL query from the specified pattern.
168+
*
169+
* @param string $value query value
170+
* @return string query
171+
*/
163172

164173
private function format($value) {
165174
$queryString = "";
@@ -170,26 +179,19 @@ private function format($value) {
170179
return $queryString;
171180
}
172181

173-
/**
174-
* Builds SQL search query.
175-
* @return string query
176-
*/
177182
private function buildQuery(){
178183
return rtrim($this->format($this->searchQuery) , " {$this->queryEnd} ");
179184
}
180185

181-
/**
182-
* Builds SQL search query array.
183-
* @return array queries
184-
*/
185186
private function buildArrayQuery($i = 0){
186187
return rtrim($this->format($this->searchQuery[$i]) , " {$this->queryEnd} ");;
187188
}
188189

189190
/**
190-
* Determine which search method to use while creating query.
191-
* @return string SQL query
192-
*/
191+
* Determine which search method to use while creating query.
192+
*
193+
* @return string SQL query
194+
*/
193195
private function buildSQL(){
194196
$sql = "";
195197
if(!empty($this->paramTags)){
@@ -218,71 +220,57 @@ private function buildSQL(){
218220
}
219221

220222
/**
221-
* Gernerates the search query.
222-
* @param string $checkOpt the search query start operator
223-
* @return string SQL query
224-
*/
225-
public function generateQuery($checkOpt = "AUTO"){
223+
* Execute search query.
224+
*
225+
* @return string SQL query
226+
*/
227+
public function getQuery(){
226228
if (!empty($this->searchQuery)){
227229
if (!empty($this->searchQuery)){
228-
if($checkOpt != "AUTO" && empty($this->QueryCondition)){
229-
$this->QueryCondition .= " {$checkOpt} (";
230-
}else{
231-
$this->QueryCondition .= (!empty($this->QueryCondition) ? " AND (" : " WHERE (");
232-
}
230+
if (!empty($this->QueryCondition)){
231+
$this->QueryCondition .= " AND (";
232+
}else {
233+
$this->QueryCondition .= " WHERE (";
234+
}
235+
233236
switch ($this->searchAlgorithm){
237+
234238
case self::OR:
235239
$this->setStart(self::LIKE);
236240
$this->setEnd(self::OR);
241+
$this->QueryCondition .= $this->buildSQL();
242+
$this->QueryCondition .= " )";
237243
break;
238-
244+
239245
case self::AND:
240246
$this->setStart(self::LIKE);
241247
$this->setEnd(self::AND);
248+
$this->QueryCondition .= $this->buildSQL();
249+
$this->QueryCondition .= " )";
242250
break;
243-
251+
244252
case self::NAND:
245253
$this->setStart(self::NOT_LIKE);
246254
$this->setEnd(self::AND);
255+
$this->QueryCondition .= $this->buildSQL();
256+
$this->QueryCondition .= " )";
247257
break;
248-
258+
249259
case self::NOR:
250260
$this->setStart(self::NOT_LIKE);
251261
$this->setEnd(self::OR);
262+
$this->QueryCondition .= $this->buildSQL();
263+
$this->QueryCondition .= " )";
252264
break;
253265
default:
254266
$this->setStart(self::LIKE);
255267
$this->setEnd(self::OR);
268+
$this->QueryCondition .= $this->buildSQL();
269+
$this->QueryCondition .= " )";
256270
break;
257271
}
258-
$this->QueryCondition .= $this->buildSQL();
259-
$this->QueryCondition .= " )";
260272
}
261273
}
262274
return $this->QueryCondition;
263275
}
264-
265-
/**
266-
* Gets sql search query by apending AND clause.
267-
* @return string SQL query
268-
*/
269-
public function getAndQuery(){
270-
return $this->generateQuery("AND");
271-
}
272-
273-
/**
274-
* Gets sql search query by apending WHERE clause.
275-
* @return string SQL query
276-
*/
277-
public function getWhereQuery(){
278-
return $this->generateQuery("WHERE");
279-
}
280-
281-
/**
282-
* Gets sql search query by automatically detecting and apending start clause.
283-
* @return string SQL query
284-
*/
285-
public function getQuery(){
286-
return $this->generateQuery();
287-
}
288276
}

0 commit comments

Comments
 (0)