11
11
*/
12
12
class SearchController{
13
13
/**
14
- * @var string SQL patterns
15
- */
14
+ * @var string SQL patterns
15
+ */
16
16
public const START_WITH_QUERY = "query% " ;
17
17
public const END_WITH_QUERY = "%query " ;
18
18
public const HAVE_ANY_QUERY = "%query% " ;
@@ -22,59 +22,62 @@ class SearchController{
22
22
public const START_END_WITH_QUERY = "query%query " ;
23
23
24
24
/**
25
- * @var string SearchController algorithms
26
- */
25
+ * @var string SearchController algorithms
26
+ */
27
27
public const OR = "OR " ;
28
28
public const AND = "AND " ;
29
29
public const NAND = "NAND " ;
30
30
public const NOR = "NOR " ;
31
31
32
32
/**
33
- * @var string SQL search keywords
34
- */
33
+ * @var string SQL search keywords
34
+ */
35
35
public const LIKE = "LIKE " ;
36
36
public const NOT_LIKE = "NOT LIKE " ;
37
37
38
38
/**
39
- * @var string SQL Query
40
- */
39
+ * @var string SQL Query
40
+ */
41
41
private $ QueryCondition = "" ;
42
42
43
43
/**
44
- * @var string Search query algorithm that needs to be used
45
- */
44
+ * @var string Search query algorithm that needs to be used
45
+ */
46
46
private $ searchAlgorithm ;
47
47
48
48
/**
49
- * @var string Search request query value
50
- */
49
+ * @var string Search request query value
50
+ */
51
51
private $ searchQuery = null ;
52
52
53
53
/**
54
- * @var array MYSQL database table rows to search from
55
- */
54
+ * @var array MYSQL database table rows to search from
55
+ */
56
56
private $ paramArray = array ();
57
57
58
58
/**
59
- * @var string MYSQL database table row for tag value
60
- */
59
+ * @var string MYSQL database table row for tag value
60
+ */
61
61
private $ paramTags ;
62
62
63
63
/**
64
- * @var string SQL LIKE query operator to be use
65
- */
64
+ * @var string SQL LIKE query operator to be use
65
+ */
66
66
private $ operators ;
67
67
68
68
/**
69
- * @var string SQL query prefix
70
- */
69
+ * @var string SQL query prefix
70
+ */
71
71
private $ queryStart ;
72
72
73
73
/**
74
- * @var string SQL query suffix
75
- */
74
+ * @var string SQL query suffix
75
+ */
76
76
private $ queryEnd ;
77
77
78
+ /**
79
+ * Constructor
80
+ */
78
81
public function __construct ($ algorithm = self ::OR ) {
79
82
$ this ->searchAlgorithm = $ algorithm ;
80
83
$ this ->operators = self ::END_WITH_QUERY ;
@@ -83,73 +86,67 @@ public function __construct($algorithm = self::OR) {
83
86
}
84
87
85
88
/**
86
- * Set database search table columns.
87
- *
88
- * @param array $param columns
89
- */
89
+ * Set database search table columns.
90
+ *
91
+ * @param array $param columns
92
+ */
90
93
public function setParameter ($ param =array ()){
91
94
$ this ->paramArray = $ param ;
92
95
}
93
96
94
97
/**
95
- * Set initial SQL queries.
96
- *
97
- * @param string $query query
98
- */
98
+ * Set initial SQL queries.
99
+ * @param string $query query
100
+ */
99
101
public function setSQLQuery ($ query ){
100
102
$ this ->QueryCondition = $ query ;
101
103
}
102
104
103
105
/**
104
- * Set database search operator pattern.
105
- *
106
- * @param string $pattern name
107
- */
108
- public function setOperators ($ pattern ){
109
- $ this ->operators = $ pattern ;
106
+ * Set database search operator pattern.
107
+ * @param string $operator name
108
+ */
109
+ public function setOperators ($ operator ){
110
+ $ this ->operators = $ operator ;
110
111
}
111
112
112
113
/**
113
- * Set database tag table column name.
114
- *
115
- * @param string $column name
116
- */
114
+ * Set database tag table column name.
115
+ * @param string $column name
116
+ */
117
117
public function setTags ($ column ){
118
118
$ this ->paramTags = $ column ;
119
119
}
120
120
121
121
/**
122
- * Set search query value.
123
- *
124
- * @param string $query query value
125
- * @return object|SearchController
126
- */
122
+ * Set search query value.
123
+ * @param string $query query value
124
+ * @return object|SearchController
125
+ */
127
126
public function setQuery ($ query ){
128
127
$ this ->searchQuery = htmlspecialchars ($ query , ENT_QUOTES , "UTF-8 " );
129
128
return $ this ;
130
129
}
131
130
132
131
/**
133
- * Set query prefix string.
134
- *
135
- * @param string $str query prefix
136
- */
132
+ * Set query prefix string.
133
+ * @param string $str query prefix
134
+ */
137
135
public function setStart ($ str ){
138
136
$ this ->queryStart = $ str ;
139
137
}
140
138
141
139
/**
142
- * Set query suffix string.
143
- *
144
- * @param string $str query suffix
145
- */
140
+ * Set query suffix string.
141
+ * @param string $str query suffix
142
+ */
146
143
public function setEnd ($ str ){
147
144
$ this ->queryEnd = $ str ;
148
145
}
149
146
150
147
/**
151
- * Split search query value by space.
152
- */
148
+ * Split search query value by space.
149
+ */
153
150
public function split (){
154
151
if (strpos ($ this ->searchQuery , " " ) !== false ) {
155
152
$ this ->searchQuery = explode (" " , $ this ->searchQuery );
@@ -159,11 +156,10 @@ public function split(){
159
156
}
160
157
161
158
/**
162
- * Create SQL query from the specified pattern.
163
- *
164
- * @param string $value query value
165
- * @return string query
166
- */
159
+ * Create SQL query from the specified pattern.
160
+ * @param string $value query value
161
+ * @return string query
162
+ */
167
163
168
164
private function format ($ value ) {
169
165
$ queryString = "" ;
@@ -174,19 +170,26 @@ private function format($value) {
174
170
return $ queryString ;
175
171
}
176
172
173
+ /**
174
+ * Builds SQL search query.
175
+ * @return string query
176
+ */
177
177
private function buildQuery (){
178
178
return rtrim ($ this ->format ($ this ->searchQuery ) , " {$ this ->queryEnd } " );
179
179
}
180
180
181
+ /**
182
+ * Builds SQL search query array.
183
+ * @return array queries
184
+ */
181
185
private function buildArrayQuery ($ i = 0 ){
182
186
return rtrim ($ this ->format ($ this ->searchQuery [$ i ]) , " {$ this ->queryEnd } " );;
183
187
}
184
188
185
189
/**
186
- * Determine which search method to use while creating query.
187
- *
188
- * @return string SQL query
189
- */
190
+ * Determine which search method to use while creating query.
191
+ * @return string SQL query
192
+ */
190
193
private function buildSQL (){
191
194
$ sql = "" ;
192
195
if (!empty ($ this ->paramTags )){
@@ -215,10 +218,9 @@ private function buildSQL(){
215
218
}
216
219
217
220
/**
218
- * Execute search query.
219
- *
220
- * @return string SQL query
221
- */
221
+ * Execute search query.
222
+ * @return string SQL query
223
+ */
222
224
public function getQuery (){
223
225
if (!empty ($ this ->searchQuery )){
224
226
if (!empty ($ this ->searchQuery )){
@@ -228,17 +230,17 @@ public function getQuery(){
228
230
$ this ->setStart (self ::LIKE );
229
231
$ this ->setEnd (self ::OR );
230
232
break ;
231
-
233
+
232
234
case self ::AND :
233
235
$ this ->setStart (self ::LIKE );
234
236
$ this ->setEnd (self ::AND );
235
237
break ;
236
-
238
+
237
239
case self ::NAND :
238
240
$ this ->setStart (self ::NOT_LIKE );
239
241
$ this ->setEnd (self ::AND );
240
242
break ;
241
-
243
+
242
244
case self ::NOR :
243
245
$ this ->setStart (self ::NOT_LIKE );
244
246
$ this ->setEnd (self ::OR );
0 commit comments