Skip to content

Commit 27b608e

Browse files
authored
Merge pull request #1 from mahshid/patch-1
Update to have more control on the query
2 parents 39a9e27 + dcc8287 commit 27b608e

File tree

1 file changed

+60
-20
lines changed

1 file changed

+60
-20
lines changed

src/SphinxSE.php

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SphinxSE {
2222
protected $maxmatches;
2323

2424
private $selects = [];
25+
private $queries = [];
2526

2627
public function __construct($config = [])
2728
{
@@ -48,23 +49,31 @@ public function setServer($host, $port = 0)
4849
}
4950

5051
/**
51-
* Set offset and count into result set, optionally set max-matches and cutoff limits
52+
* Set a limit for the returned results
5253
*
53-
* @param integer $offset
5454
* @param integer $limit
55+
*
56+
*/
57+
public function setLimits($limit)
58+
{
59+
$this->limit = $limit;
60+
}
61+
62+
/**
63+
* Set offset, optionally set max-matches
64+
*
65+
* @param integer $offset
5566
* @param integer $max
56-
* @param integer $cutoff
5767
*
5868
*/
59-
public function setLimits($offset, $limit, $max = 1000, $cutoff = 0)
69+
public function setOffset($offset, $max = 1000)
6070
{
6171
if ($offset >= $max)
6272
{
6373
throw new SphinxSEException('Offset out of bounds exception.');
6474
}
6575

6676
$this->offset = $offset;
67-
$this->limit = $limit;
6877
$this->maxmatches = $max;
6978
}
7079

@@ -142,14 +151,16 @@ public function setIndex($index)
142151
}
143152

144153
/**
145-
* Add select statement to the query
154+
* Add select statement to the query. It can be a string or an array.
155+
* In the case where data is stored in an array, you can set the special key for that.
156+
* And also resetting the special select can be done by assigning null to its key.
146157
*
147158
* @param $select
148159
*
149160
*/
150161
public function setSelect($select)
151162
{
152-
$this->selects[] = $select;
163+
$this->selects = array_merge($this->selects, static::arrayWrap($select));
153164
}
154165

155166
/**
@@ -172,7 +183,7 @@ public function fieldQuery($fields, $value, $quorum = 0.8, $operator = '/')
172183
$field_string = '@' . $fields;
173184
}
174185

175-
$this->query .= $field_string . ' "' . $this->escapeString($value) . '"' . ($quorum ? $operator . $quorum : '') . ' ';
186+
$this->queries[$field_string] = $field_string . ' "' . $this->escapeString($value) . '"' . ($quorum ? $operator . $quorum : '');
176187
}
177188

178189
/**
@@ -218,7 +229,18 @@ public function setFilterRange($attribute, $min, $max, $exclude = false)
218229
*/
219230
public function setFilterFloatRange($attribute, $min, $max, $exclude = false)
220231
{
221-
$this->floatrange[] = ($exclude ? '!' : '') . $attribute . ",{$min},{$max}";
232+
$this->floatrange[$attribute] = ($exclude ? '!' : '') . $attribute . ",{$min},{$max}";
233+
}
234+
235+
/**
236+
* Reset float range filter;
237+
*
238+
* @param string $attribute attribute name
239+
*
240+
*/
241+
public function resetFilterFloatRange($attribute)
242+
{
243+
$this->floatrange[$attribute] = null;
222244
}
223245

224246
/**
@@ -258,7 +280,7 @@ public function setGroupDistinct($attribute)
258280
*/
259281
public function setGeoAnchor($attrlat, $attrlong, $lat, $long, $alias = 'geodist')
260282
{
261-
$this->setSelect('GEODIST(' . $attrlat . ', ' . $attrlong . ', ' . $lat . ', ' . $long . ') AS ' . $alias);
283+
$this->setSelect([$alias => 'GEODIST(' . $attrlat . ', ' . $attrlong . ', ' . $lat . ', ' . $long . ') AS ' . $alias]);
262284
}
263285

264286
public function toQuery()
@@ -267,18 +289,19 @@ public function toQuery()
267289

268290
$properties = $reflection->getProperties();
269291

270-
$query = empty($this->selects) ? '' : 'select=' . implode(',', $this->selects) . ';';
292+
$this->query = $this->getQuery();
293+
294+
$this->selects = array_filter($this->selects);
295+
296+
$query = empty($this->selects) ? '' : 'select=' . implode(',', $this->selects) . '; ';
271297

272298
foreach ($properties as $property)
273299
{
274300
$element = $this->{$property->name};
275301

276302
if ($property->isProtected() && ! empty($element))
277303
{
278-
if (! is_array($element))
279-
{
280-
$element = [$element];
281-
}
304+
$element = array_filter(static::arrayWrap($element));
282305

283306
foreach ($element as $value)
284307
{
@@ -293,12 +316,12 @@ public function toQuery()
293316
$exclude = false;
294317
}
295318

296-
$query .= ($exclude ? '!' : '') . $property->name . '=' . $value . ';';
319+
$query .= ($exclude ? '!' : '') . $property->name . '=' . $value . '; ';
297320
}
298321
}
299322
}
300323

301-
return $query;
324+
return trim($query);
302325
}
303326

304327
/**
@@ -325,23 +348,23 @@ public function escapeString($string)
325348
*/
326349
public function getQuery()
327350
{
328-
return $this->query;
351+
return implode(' ', $this->queries);
329352
}
330353

331354
/**
332355
* @param mixed $query
333356
*/
334357
public function setQuery($query)
335358
{
336-
$this->query = str_replace(';', '', $query);
359+
$this->queries = [str_replace(';', '', $query)];
337360
}
338361

339362
/**
340363
* @param mixed $query
341364
*/
342365
public function appendQuery($query)
343366
{
344-
$this->query .= str_replace(';', '', $query);
367+
$this->queries[] = str_replace(';', '', $query);
345368
}
346369

347370
public function __call($name, $arguments)
@@ -353,4 +376,21 @@ public function __call($name, $arguments)
353376

354377
$this->fieldQuery($name, $arguments[0]);
355378
}
379+
380+
/**
381+
* If the given value is not an array and not null, wrap it in one.
382+
*
383+
* @param mixed $value
384+
*
385+
* @return array
386+
*/
387+
private static function arrayWrap($value)
388+
{
389+
if (is_null($value))
390+
{
391+
return [];
392+
}
393+
394+
return is_array($value) ? $value : [$value];
395+
}
356396
}

0 commit comments

Comments
 (0)