Class yii\sphinx\MatchBuilder

Inheritanceyii\sphinx\MatchBuilder » yii\base\BaseObject
Available since extension's version2.0.6
Source Code https://github.com/yiisoft/yii2-sphinx/blob/master/src/MatchBuilder.php

MatchBuilder builds a MATCH SphinxQL expression based on the specification given as a yii\sphinx\MatchExpression object.

See also:

Public Properties

Hide inherited properties

Property Type Description Defined By
$db yii\sphinx\Connection The Sphinx connection. yii\sphinx\MatchBuilder
$matchBuilders array Map of MATCH keywords to builder methods. yii\sphinx\MatchBuilder
$matchOperators array Map of MATCH operators. yii\sphinx\MatchBuilder

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Constructor. yii\sphinx\MatchBuilder
build() Generates the MATCH expression from given yii\sphinx\MatchExpression object. yii\sphinx\MatchBuilder
buildAndMatch() Connects two or more MATCH expressions with the AND or OR operator yii\sphinx\MatchBuilder
buildHashMatch() Creates a MATCH based on column-value pairs. yii\sphinx\MatchBuilder
buildIgnoreMatch() Create ignored MATCH expressions yii\sphinx\MatchBuilder
buildMatch() Create MATCH expression. yii\sphinx\MatchBuilder
buildMultipleMatch() Create MAYBE, SENTENCE or PARAGRAPH expressions. yii\sphinx\MatchBuilder
buildProximityMatch() Create PROXIMITY expressions yii\sphinx\MatchBuilder
buildSimpleMatch() Creates an Match expressions like "column" operator value. yii\sphinx\MatchBuilder
buildZoneMatch() Create MATCH expressions for zones. yii\sphinx\MatchBuilder

Protected Methods

Hide inherited methods

Method Description Defined By
buildMatchColumn() Created column as string for expression of MATCH yii\sphinx\MatchBuilder
buildMatchValue() Create placeholder for expression of MATCH yii\sphinx\MatchBuilder
parseParams() Returns the actual MATCH expression by inserting parameter values into the corresponding placeholders. yii\sphinx\MatchBuilder

Constants

Hide inherited constants

Constant Value Description Defined By
PARAM_PREFIX ':mp' The prefix for automatically generated query binding parameters. yii\sphinx\MatchBuilder

Property Details

Hide inherited properties

$db public property

The Sphinx connection.

public yii\sphinx\Connection $db null
$matchBuilders protected property

Map of MATCH keywords to builder methods. These methods are used by buildMatch() to build MATCH expression from array syntax.

protected array $matchBuilders = [
    
'AND' => 'buildAndMatch',
    
'OR' => 'buildAndMatch',
    
'IGNORE' => 'buildIgnoreMatch',
    
'PROXIMITY' => 'buildProximityMatch',
    
'MAYBE' => 'buildMultipleMatch',
    
'SENTENCE' => 'buildMultipleMatch',
    
'PARAGRAPH' => 'buildMultipleMatch',
    
'ZONE' => 'buildZoneMatch',
    
'ZONESPAN' => 'buildZoneMatch',
]
$matchOperators protected property

Map of MATCH operators. These operators are used for replacement of verbose operators.

protected array $matchOperators = [
    
'AND' => ' ',
    
'OR' => ' | ',
    
'NOT' => ' !',
    
'=' => ' ',
]

Method Details

Hide inherited methods

__construct() public method

Constructor.

public void __construct ( $connection, $config = [] )
$connection yii\sphinx\Connection

The Sphinx connection.

$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($connection, $config = [])
{
    $this->db = $connection;
    parent::__construct($config);
}

            
build() public method

Generates the MATCH expression from given yii\sphinx\MatchExpression object.

public string build ( $match )
$match yii\sphinx\MatchExpression

The yii\sphinx\MatchExpression object from which the MATCH expression will be generated.

return string

Generated MATCH expression.

                public function build($match)
{
    $params = $match->params;
    $expression = $this->buildMatch($match->match, $params);
    return $this->parseParams($expression, $params);
}

            
buildAndMatch() public method

Connects two or more MATCH expressions with the AND or OR operator

public string buildAndMatch ( $operator, $operands, &$params )
$operator string

The operator which is used for connecting the given operands

$operands array

The Match expressions to connect

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildAndMatch($operator, $operands, &$params)
{
    $parts = [];
    foreach ($operands as $operand) {
        if (is_array($operand) || is_object($operand)) {
            $operand = $this->buildMatch($operand, $params);
        }
        if ($operand !== '') {
            $parts[] = $operand;
        }
    }
    if (empty($parts)) {
        return '';
    }
    return '(' . implode(')' . ($operator === 'OR' ? ' | ' : ' ') . '(', $parts) . ')';
}

            
buildHashMatch() public method

Creates a MATCH based on column-value pairs.

public string buildHashMatch ( $match, &$params )
$match array

The match condition

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildHashMatch($match, &$params)
{
    $parts = [];
    foreach ($match as $column => $value) {
        $parts[] = $this->buildMatchColumn($column) . ' ' . $this->buildMatchValue($value, $params);
    }
    return count($parts) === 1 ? $parts[0] : '(' . implode(') (', $parts) . ')';
}

            
buildIgnoreMatch() public method

Create ignored MATCH expressions

public string buildIgnoreMatch ( $operator, $operands, &$params )
$operator string

The operator which is used for Create Match expressions

$operands array

The Match expressions

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildIgnoreMatch($operator, $operands, &$params)
{
    if (!isset($operands[0], $operands[1])) {
        throw new InvalidParamException("Operator '$operator' requires two operands.");
    }
    list($column, $value) = $operands;
    return $this->buildMatchColumn($column, true) . ' ' . $this->buildMatchValue($value, $params);
}

            
buildMatch() public method

Create MATCH expression.

public string buildMatch ( $match, &$params )
$match string|array

MATCH specification.

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildMatch($match, &$params)
{
    if (empty($match)) {
        return '';
    }
    if ($match instanceof Expression) {
        return $this->buildMatchValue($match, $params);
    }
    if (!is_array($match)) {
        return $match;
    }
    if (isset($match[0])) {
        // operator format: operator, operand 1, operand 2, ...
        $operator = strtoupper($match[0]);
        if (isset($this->matchBuilders[$operator])) {
            $method = $this->matchBuilders[$operator];
        } else {
            $method = 'buildSimpleMatch';
        }
        array_shift($match);
        return $this->$method($operator, $match, $params);
    }
    // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
    return $this->buildHashMatch($match, $params);
}

            
buildMatchColumn() protected method

Created column as string for expression of MATCH

protected string buildMatchColumn ( $column, $ignored false )
$column string

Column specification.

$ignored boolean

Whether column should be specified as 'ignored'.

return string

The column statement.

                protected function buildMatchColumn($column, $ignored = false)
{
    if (empty($column)) {
        return '';
    }
    if ($column === '*') {
        return '@*';
    }
    return '@' . ($ignored ? '!' : '') . (strpos($column, ',') === false ? $column : '(' . $column . ')');
}

            
buildMatchValue() protected method

Create placeholder for expression of MATCH

protected string buildMatchValue ( $value, &$params )
$value string|array|\yii\db\Expression
$params array

The expression parameters to be populated

return string

The MATCH expression

                protected function buildMatchValue($value, &$params)
{
    if (empty($value)) {
        return '""';
    }
    if ($value instanceof Expression) {
        $params = array_merge($params, $value->params);
        return $value->expression;
    }
    $parts = [];
    foreach ((array) $value as $v) {
        if ($v instanceof Expression) {
            $params = array_merge($params, $v->params);
            $parts[] = $v->expression;
        } else {
            $phName = self::PARAM_PREFIX . count($params);
            $parts[] = $phName;
            $params[$phName] = $v;
        }
    }
    return implode(' | ', $parts);
}

            
buildMultipleMatch() public method

Create MAYBE, SENTENCE or PARAGRAPH expressions.

public string buildMultipleMatch ( $operator, $operands, &$params )
$operator string

The operator which is used for Create Match expressions

$operands array

The Match expressions

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildMultipleMatch($operator, $operands, &$params)
{
    if (count($operands) < 3) {
        throw new InvalidParamException("Operator '$operator' requires three or more operands.");
    }
    $column = array_shift($operands);
    $phNames = [];
    foreach ($operands as $operand) {
        $phNames[] = $this->buildMatchValue($operand, $params);
    }
    return $this->buildMatchColumn($column) . ' ' . implode(' ' . $operator . ' ', $phNames);
}

            
buildProximityMatch() public method

Create PROXIMITY expressions

public string buildProximityMatch ( $operator, $operands, &$params )
$operator string

The operator which is used for Create Match expressions

$operands array

The Match expressions

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildProximityMatch($operator, $operands, &$params)
{
    if (!isset($operands[0], $operands[1], $operands[2])) {
        throw new InvalidParamException("Operator '$operator' requires three operands.");
    }
    list($column, $value, $proximity) = $operands;
    return $this->buildMatchColumn($column) . ' ' . $this->buildMatchValue($value, $params) . '~' . (int) $proximity;
}

            
buildSimpleMatch() public method

Creates an Match expressions like "column" operator value.

public string buildSimpleMatch ( $operator, $operands, &$params )
$operator string

The operator to use. Anything could be used e.g. >, <=, etc.

$operands array

Contains two column names.

$params array

The expression parameters to be populated

return string

The MATCH expression

throws \yii\base\InvalidParamException

on invalid operands count.

                public function buildSimpleMatch($operator, $operands, &$params)
{
    if (count($operands) !== 2) {
        throw new InvalidParamException("Operator '$operator' requires two operands.");
    }
    list($column, $value) = $operands;
    if (isset($this->matchOperators[$operator])) {
        $operator = $this->matchOperators[$operator];
    }
    return $this->buildMatchColumn($column) . $operator . $this->buildMatchValue($value, $params);
}

            
buildZoneMatch() public method

Create MATCH expressions for zones.

public string buildZoneMatch ( $operator, $operands, &$params )
$operator string

The operator which is used for Create Match expressions

$operands array

The Match expressions

$params array

The expression parameters to be populated

return string

The MATCH expression

                public function buildZoneMatch($operator, $operands, &$params)
{
    if (!isset($operands[0])) {
        throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
    }
    $zones = (array)$operands[0];
    return "$operator: (" . implode(',', $zones) . ")";
}

            
parseParams() protected method

Returns the actual MATCH expression by inserting parameter values into the corresponding placeholders.

protected string parseParams ( $expression, $params )
$expression string

The expression string which is needed to prepare.

$params array

The binding parameters for inserting.

return string

Parsed expression.

                protected function parseParams($expression, $params)
{
    if (empty($params)) {
        return $expression;
    }
    foreach ($params as $name => $value) {
        if (strncmp($name, ':', 1) !== 0) {
            $name = ':' . $name;
        }
        // unable to use `str_replace()` because particular param name may be a substring of another param name
        $pattern = "/" . preg_quote($name, '/') . '\b/';
        $value = '"' . $this->db->escapeMatchValue($value) . '"';
        $expression = preg_replace($pattern, $value, $expression, -1, $cnt);
    }
    return $expression;
}