Class yii\sphinx\MatchBuilder
| Inheritance | yii\ |
|---|---|
| Implements | yii\ |
| Available since version | 2.0.6 |
| Source Code | https://github.com/yiisoft/yii2-sphinx/blob/master/MatchBuilder.php |
MatchBuilder builds a MATCH SphinxQL expression based on the specification given as a yii\
See also:
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $db | yii\ |
The Sphinx connection. | yii\ |
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $matchBuilders | array | Map of MATCH keywords to builder methods. | yii\ |
| $matchOperators | array | Map of MATCH operators. | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\ |
| __construct() | Constructor. | yii\ |
| __get() | Returns the value of an object property. | yii\ |
| __isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
| __set() | Sets value of an object property. | yii\ |
| __unset() | Sets an object property to null. | yii\ |
| build() | Generates the MATCH expression from given yii\ |
yii\ |
| buildAndMatch() | Connects two or more MATCH expressions with the AND or OR operator |
yii\ |
| buildHashMatch() | Creates a MATCH based on column-value pairs. | yii\ |
| buildIgnoreMatch() | Create ignored MATCH expressions | yii\ |
| buildMatch() | Create MATCH expression. | yii\ |
| buildMultipleMatch() | Create MAYBE, SENTENCE or PARAGRAPH expressions. | yii\ |
| buildProximityMatch() | Create PROXIMITY expressions | yii\ |
| buildSimpleMatch() | Creates an Match expressions like "column" operator value. |
yii\ |
| buildZoneMatch() | Create MATCH expressions for zones. | yii\ |
| canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
| canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
| className() | Returns the fully qualified name of this class. | yii\ |
| hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
| hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
| init() | Initializes the object. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| buildMatchColumn() | Created column as string for expression of MATCH | yii\ |
| buildMatchValue() | Create placeholder for expression of MATCH | yii\ |
| parseParams() | Returns the actual MATCH expression by inserting parameter values into the corresponding placeholders. | yii\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| PARAM_PREFIX | ':mp' | The prefix for automatically generated query binding parameters. | yii\ |
Property Details
Map of MATCH keywords to builder methods.
These methods are used by buildMatch() to build MATCH expression from array syntax.
Map of MATCH operators.
These operators are used for replacement of verbose operators.
Method Details
Defined in:
yii\
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
| public __call( string $name, array $params ): mixed | ||
| $name | string |
The method name |
| $params | array |
Method parameters |
| return | mixed |
The method return value |
|---|---|---|
| throws | yii\ |
when calling unknown method |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Constructor.
| public __construct( yii\ | ||
| $connection | yii\ |
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);
}
Defined in:
yii\
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;.
See also __set().
| public __get( string $name ): mixed | ||
| $name | string |
The property name |
| return | mixed |
The property value |
|---|---|---|
| throws | yii\ |
if the property is not defined |
| throws | yii\ |
if the property is write-only |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in:
yii\
Checks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property).
Note that if the property is not defined, false will be returned.
| public __isset( string $name ): boolean | ||
| $name | string |
The property name or the event name |
| return | boolean |
Whether the named property is set (not null). |
|---|---|---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in:
yii\
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;.
See also __get().
| public __set( string $name, mixed $value ): mixed | ||
| $name | string |
The property name or the event name |
| $value | mixed |
The property value |
| throws | yii\ |
if the property is not defined |
|---|---|---|
| throws | yii\ |
if the property is read-only |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Defined in:
yii\
Sets an object property to null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property).
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
| public __unset( string $name ): mixed | ||
| $name | string |
The property name |
| throws | yii\ |
if the property is read only. |
|---|---|---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Generates the MATCH expression from given yii\
| public build( yii\ | ||
| $match | yii\ |
The yii\ |
| return | string |
Generated MATCH expression. |
|---|---|---|
public function build($match)
{
$params = $match->params;
$expression = $this->buildMatch($match->match, $params);
return $this->parseParams($expression, $params);
}
Connects two or more MATCH expressions with the AND or OR operator
| public buildAndMatch( string $operator, array $operands, array & $params ): string | ||
| $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) . ')';
}
Creates a MATCH based on column-value pairs.
| public buildHashMatch( array $match, array & $params ): string | ||
| $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) . ')';
}
Create ignored MATCH expressions
| public buildIgnoreMatch( string $operator, array $operands, array & $params ): string | ||
| $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);
}
Create MATCH expression.
| public buildMatch( string|array $match, array & $params ): string | ||
| $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);
}
Created column as string for expression of MATCH
| protected buildMatchColumn( string $column, boolean $ignored = false ): string | ||
| $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 . ')');
}
Create placeholder for expression of MATCH
| protected buildMatchValue( string|array|yii\ | ||
| $value | string|array|yii\ |
|
| $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);
}
Create MAYBE, SENTENCE or PARAGRAPH expressions.
| public buildMultipleMatch( string $operator, array $operands, array & $params ): string | ||
| $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);
}
Create PROXIMITY expressions
| public buildProximityMatch( string $operator, array $operands, array & $params ): string | ||
| $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;
}
Creates an Match expressions like "column" operator value.
| public buildSimpleMatch( string $operator, array $operands, array & $params ): string | ||
| $operator | string |
The operator to use. Anything could be used e.g. |
| $operands | array |
Contains two column names. |
| $params | array |
The expression parameters to be populated |
| return | string |
The MATCH expression |
|---|---|---|
| throws | yii\ |
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);
}
Create MATCH expressions for zones.
| public buildZoneMatch( string $operator, array $operands, array & $params ): string | ||
| $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) . ')';
}
Defined in:
yii\
Returns a value indicating whether a property can be read.
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
See also canSetProperty().
| public canGetProperty( string $name, boolean $checkVars = true ): boolean | ||
| $name | string |
The property name |
| $checkVars | boolean |
Whether to treat member variables as properties |
| return | boolean |
Whether the property can be read |
|---|---|---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in:
yii\
Returns a value indicating whether a property can be set.
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
See also canGetProperty().
| public canSetProperty( string $name, boolean $checkVars = true ): boolean | ||
| $name | string |
The property name |
| $checkVars | boolean |
Whether to treat member variables as properties |
| return | boolean |
Whether the property can be written |
|---|---|---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class instead.
Defined in:
yii\
Returns the fully qualified name of this class.
| public static className( ): string | ||
| return | string |
The fully qualified name of this class. |
|---|---|---|
public static function className()
{
return get_called_class();
}
Defined in:
yii\
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists().
You may override this method when you implemented the php magic method __call().
| public hasMethod( string $name ): boolean | ||
| $name | string |
The method name |
| return | boolean |
Whether the method is defined |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in:
yii\
Returns a value indicating whether a property is defined.
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
See also:
| public hasProperty( string $name, boolean $checkVars = true ): boolean | ||
| $name | string |
The property name |
| $checkVars | boolean |
Whether to treat member variables as properties |
| return | boolean |
Whether the property is defined |
|---|---|---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in:
yii\
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
| public init( ): mixed |
public function init()
{
}
Returns the actual MATCH expression by inserting parameter values into the corresponding placeholders.
| protected parseParams( string $expression, array $params ): string | ||
| $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;
}