Skip to content

Commit

Permalink
Merge pull request #1 from mahshid/patch-1
Browse files Browse the repository at this point in the history
Update to have more control on the query
  • Loading branch information
misagh authored Mar 1, 2023
2 parents 39a9e27 + dcc8287 commit 27b608e
Showing 1 changed file with 60 additions and 20 deletions.
80 changes: 60 additions & 20 deletions src/SphinxSE.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SphinxSE {
protected $maxmatches;

private $selects = [];
private $queries = [];

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

/**
* Set offset and count into result set, optionally set max-matches and cutoff limits
* Set a limit for the returned results
*
* @param integer $offset
* @param integer $limit
*
*/
public function setLimits($limit)
{
$this->limit = $limit;
}

/**
* Set offset, optionally set max-matches
*
* @param integer $offset
* @param integer $max
* @param integer $cutoff
*
*/
public function setLimits($offset, $limit, $max = 1000, $cutoff = 0)
public function setOffset($offset, $max = 1000)
{
if ($offset >= $max)
{
throw new SphinxSEException('Offset out of bounds exception.');
}

$this->offset = $offset;
$this->limit = $limit;
$this->maxmatches = $max;
}

Expand Down Expand Up @@ -142,14 +151,16 @@ public function setIndex($index)
}

/**
* Add select statement to the query
* Add select statement to the query. It can be a string or an array.
* In the case where data is stored in an array, you can set the special key for that.
* And also resetting the special select can be done by assigning null to its key.
*
* @param $select
*
*/
public function setSelect($select)
{
$this->selects[] = $select;
$this->selects = array_merge($this->selects, static::arrayWrap($select));
}

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

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

/**
Expand Down Expand Up @@ -218,7 +229,18 @@ public function setFilterRange($attribute, $min, $max, $exclude = false)
*/
public function setFilterFloatRange($attribute, $min, $max, $exclude = false)
{
$this->floatrange[] = ($exclude ? '!' : '') . $attribute . ",{$min},{$max}";
$this->floatrange[$attribute] = ($exclude ? '!' : '') . $attribute . ",{$min},{$max}";
}

/**
* Reset float range filter;
*
* @param string $attribute attribute name
*
*/
public function resetFilterFloatRange($attribute)
{
$this->floatrange[$attribute] = null;
}

/**
Expand Down Expand Up @@ -258,7 +280,7 @@ public function setGroupDistinct($attribute)
*/
public function setGeoAnchor($attrlat, $attrlong, $lat, $long, $alias = 'geodist')
{
$this->setSelect('GEODIST(' . $attrlat . ', ' . $attrlong . ', ' . $lat . ', ' . $long . ') AS ' . $alias);
$this->setSelect([$alias => 'GEODIST(' . $attrlat . ', ' . $attrlong . ', ' . $lat . ', ' . $long . ') AS ' . $alias]);
}

public function toQuery()
Expand All @@ -267,18 +289,19 @@ public function toQuery()

$properties = $reflection->getProperties();

$query = empty($this->selects) ? '' : 'select=' . implode(',', $this->selects) . ';';
$this->query = $this->getQuery();

$this->selects = array_filter($this->selects);

$query = empty($this->selects) ? '' : 'select=' . implode(',', $this->selects) . '; ';

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

if ($property->isProtected() && ! empty($element))
{
if (! is_array($element))
{
$element = [$element];
}
$element = array_filter(static::arrayWrap($element));

foreach ($element as $value)
{
Expand All @@ -293,12 +316,12 @@ public function toQuery()
$exclude = false;
}

$query .= ($exclude ? '!' : '') . $property->name . '=' . $value . ';';
$query .= ($exclude ? '!' : '') . $property->name . '=' . $value . '; ';
}
}
}

return $query;
return trim($query);
}

/**
Expand All @@ -325,23 +348,23 @@ public function escapeString($string)
*/
public function getQuery()
{
return $this->query;
return implode(' ', $this->queries);
}

/**
* @param mixed $query
*/
public function setQuery($query)
{
$this->query = str_replace(';', '', $query);
$this->queries = [str_replace(';', '', $query)];
}

/**
* @param mixed $query
*/
public function appendQuery($query)
{
$this->query .= str_replace(';', '', $query);
$this->queries[] = str_replace(';', '', $query);
}

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

$this->fieldQuery($name, $arguments[0]);
}

/**
* If the given value is not an array and not null, wrap it in one.
*
* @param mixed $value
*
* @return array
*/
private static function arrayWrap($value)
{
if (is_null($value))
{
return [];
}

return is_array($value) ? $value : [$value];
}
}

0 comments on commit 27b608e

Please sign in to comment.