Skip to content

Instantly share code, notes, and snippets.

@macnibblet
Created May 10, 2012 20:48
Show Gist options
  • Save macnibblet/2655796 to your computer and use it in GitHub Desktop.
Save macnibblet/2655796 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock <antoine@pmg.se>
*/
/**
* @namespace
*/
namespace MCN\Sphinx\Db\Sql\Predicate;
use Zend\Db\Sql\Predicate\PredicateInterface;
/**
*
*/
class Match implements PredicateInterface
{
/**
* @var array
*/
protected $matches = array();
/**
* @param array $matches
*/
public function __construct(array $matches)
{
$this->matches = $matches;
}
/**
* @param $match
*
* @return Match
*/
public function addMatch($match)
{
$this->matches[] = $match;
return $this;
}
/**
* @param array $matches
* @return Match
*/
public function setMatches(array $matches)
{
$this->matches = $matches;
return $this;
}
/**
* @return mixed
*/
public function getMatches()
{
return $this->matches;
}
/**
*
* @return array of array|string should return an array in the format:
*
* array (
* // a sprintf formatted string
* string $specification,
*
* // the values for the above sprintf formatted string
* array $values,
*
* // an array of equal length of the $values array, with either TYPE_IDENTIFIER or TYPE_VALUE for each value
* array $types,
* )
*
*/
public function getExpressionData()
{
$str = 'MATCH(';
$types = array();
$values = array();
foreach($this->matches as $match)
{
list($field, $value) = array_values($match);
// Do some sphinx formatting of the sql
if (strpos($field, ',')) {
$field = '@(' . $field . ')';
} else {
$field = '@' . $field;
}
$str .= '%1$s %2$s';
$types[] = self::TYPE_IDENTIFIER;
$values[] = $field;
$types[] = self::TYPE_VALUE;
$values[] = $value;
}
$str .= ')';
return array(
array(
$str,
$values,
$types
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment