0 follower

Class Yiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder

InheritanceYiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder
ImplementsYiisoft\Db\Expression\ExpressionBuilderInterface

Build an object of {@see Like} or {@see NotLike} into SQL expressions.

Protected Properties

Hide inherited properties

Property Type Description Defined By
$escapingReplacements array Map of chars to their replacements in LIKE conditions. Yiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder

Protected Methods

Hide inherited methods

Method Description Defined By
getOperatorData() Get operator and not flag for the given condition. Yiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder
prepareColumn() Prepare column to use in SQL. Yiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder
preparePlaceholderName() Prepare value to use in SQL. Yiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder

Constants

Hide inherited constants

Constant Value Description Defined By
ESCAPE_SQL '' Yiisoft\Db\QueryBuilder\Condition\Builder\LikeBuilder

Property Details

Hide inherited properties

$escapingReplacements protected property

Map of chars to their replacements in LIKE conditions. By default, it's configured to escape %, _ and \ with \.

protected array $escapingReplacements = [
    
'%' => '\%',
    
'_' => '\_',
    
'\\' => '\\\\',
]

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder )
$queryBuilder Yiisoft\Db\QueryBuilder\QueryBuilderInterface

                public function __construct(
    private readonly QueryBuilderInterface $queryBuilder,
) {}

            
build() public method

Build SQL for {@see Like} or {@see NotLike}.

public string build ( Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike $expression, array &$params = [] )
$expression Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike
$params array
throws Yiisoft\Db\Exception\NotSupportedException

                public function build(ExpressionInterface $expression, array &$params = []): string
{
    $values = $expression->value;
    [$not, $operator] = $this->getOperatorData($expression);
    if ($values === null) {
        return $this->buildForEmptyValue($not);
    }
    if (is_iterable($values)) {
        if ($values instanceof Traversable) {
            $values = iterator_to_array($values);
        }
        if (empty($values)) {
            return $this->buildForEmptyValue($not);
        }
    } else {
        $values = [$values];
    }
    $column = $this->prepareColumn($expression, $params);
    $parts = [];
    foreach ($values as $value) {
        /** @var ExpressionInterface|int|string|Stringable $value */
        $placeholderName = $this->preparePlaceholderName($value, $expression, $params);
        $parts[] = "$column $operator $placeholderName" . static::ESCAPE_SQL;
    }
    $conjunction = match ($expression->conjunction) {
        LikeConjunction::And => ' AND ',
        LikeConjunction::Or => ' OR ',
    };
    return implode($conjunction, $parts);
}

            
getOperatorData() protected method

Get operator and not flag for the given condition.

protected array getOperatorData ( Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike $condition )
$condition Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike

                protected function getOperatorData(Like|NotLike $condition): array
{
    return match ($condition::class) {
        Like::class => [false, 'LIKE'],
        NotLike::class => [true, 'NOT LIKE'],
    };
}

            
prepareColumn() protected method

Prepare column to use in SQL.

protected string prepareColumn ( Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike $condition, array &$params )
$condition Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike
$params array
throws Yiisoft\Db\Exception\NotSupportedException

                protected function prepareColumn(Like|NotLike $condition, array &$params): string
{
    $column = $condition->column;
    if ($column instanceof ExpressionInterface) {
        return $this->queryBuilder->buildExpression($column, $params);
    }
    return $this->queryBuilder->getQuoter()->quoteColumnName($column);
}

            
preparePlaceholderName() protected method

Prepare value to use in SQL.

protected string preparePlaceholderName ( string|\Stringable|integer|Yiisoft\Db\Expression\ExpressionInterface $value, Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike $condition, array &$params )
$value string|\Stringable|integer|Yiisoft\Db\Expression\ExpressionInterface
$condition Yiisoft\Db\QueryBuilder\Condition\Like|Yiisoft\Db\QueryBuilder\Condition\NotLike
$params array
throws Yiisoft\Db\Exception\NotSupportedException

                protected function preparePlaceholderName(
    string|Stringable|int|ExpressionInterface $value,
    Like|NotLike $condition,
    array &$params,
): string {
    if ($value instanceof ExpressionInterface) {
        return $this->queryBuilder->buildExpression($value, $params);
    }
    if ($value instanceof Stringable) {
        $value = (string) $value;
    }
    if (is_string($value) && $condition->escape) {
        $value = strtr($value, $this->escapingReplacements);
    }
    $value = match ($condition->mode) {
        LikeMode::Contains => '%' . $value . '%',
        LikeMode::StartsWith => $value . '%',
        LikeMode::EndsWith => '%' . $value,
        LikeMode::Custom => (string) $value,
    };
    return $this->queryBuilder->bindParam(new Param($value, DataType::STRING), $params);
}