Created
May 10, 2012 20:48
-
-
Save awartoft/2655796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Antoine Hedgecock <[email protected]> | |
*/ | |
/** | |
* @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