0 follower

Final Class Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder

InheritanceYiisoft\Db\Expression\Value\Builder\JsonValueBuilder
ImplementsYiisoft\Db\Expression\ExpressionBuilderInterface

Builds expressions for {@see JsonValue}.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder
build() The method builds the raw SQL from the $expression that won't be additionally escaped or quoted. Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder

Protected Methods

Hide inherited methods

Method Description Defined By
buildStringValue() Builds a SQL expression for a string value. Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder
buildValue() Builds a SQL expression for an array value. Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder

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

The method builds the raw SQL from the $expression that won't be additionally escaped or quoted.

public string build ( Yiisoft\Db\Expression\Value\JsonValue $expression, array &$params = [] )
$expression Yiisoft\Db\Expression\Value\JsonValue

The expression to build.

$params array

The binding parameters.

return string

The raw SQL that won't be additionally escaped or quoted.

                public function build(ExpressionInterface $expression, array &$params = []): string
{
    $value = $expression->value;
    if ($value === null) {
        return 'NULL';
    }
    if ($value instanceof LazyArrayInterface) {
        $value = match ($value::class) {
            LazyArray::class, JsonLazyArray::class, StructuredLazyArray::class => $value->getRawValue(),
            default => $value->getValue(),
        };
    }
    if (is_string($value) && strlen($value) > 1
        && ($value[0] === '{' && $value[-1] === '}' || $value[0] === '[' && $value[-1] === ']')
    ) {
        return $this->buildStringValue($value, $params);
    }
    if ($value instanceof ExpressionInterface) {
        return $this->queryBuilder->buildExpression($value, $params);
    }
    return $this->buildValue($value, $params);
}

            
buildStringValue() protected method

Builds a SQL expression for a string value.

protected string buildStringValue ( string $value, array &$params )
$value string
$params array

                protected function buildStringValue(string $value, array &$params): string
{
    $param = new Param($value, DataType::STRING);
    return $this->queryBuilder->bindParam($param, $params);
}

            
buildValue() protected method

Builds a SQL expression for an array value.

protected string buildValue ( mixed $value, array &$params )
$value mixed
$params array

The binding parameters.

                protected function buildValue(mixed $value, array &$params): string
{
    if ($value instanceof Traversable && !$value instanceof JsonSerializable) {
        $value = iterator_to_array($value);
    }
    return $this->buildStringValue(json_encode($value, JSON_THROW_ON_ERROR), $params);
}