0 follower

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

InheritanceYiisoft\Db\Expression\Value\Builder\StructuredValueBuilder » Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder
ImplementsYiisoft\Db\Expression\ExpressionBuilderInterface

Default expression builder for {@see StructuredValue}. Builds an expression as a JSON.

Public Methods

Hide inherited methods

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

Protected Methods

Hide inherited methods

Method Description Defined By
buildStringValue() Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder
buildSubquery() Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder
buildValue() Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder
getLazyArrayValue() Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder
prepareValues() Returns the prepared value of the structured type, where: - object are converted to an array; - array elements are sorted according to the order of structured type columns; - indexed keys are replaced with column names; - missing elements are filled in with default values; - excessive elements are ignored. Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder

Method Details

Hide inherited methods

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

                public function __construct(protected readonly QueryBuilderInterface $queryBuilder) {}

            
build() public method

Defined in: Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder::build()

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

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

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 = $this->getLazyArrayValue($value);
    }
    if (is_string($value)) {
        return $this->buildStringValue($value, $expression, $params);
    }
    if ($value instanceof QueryInterface) {
        return $this->buildSubquery($value, $expression, $params);
    }
    return $this->buildValue($value, $expression, $params);
}

            
buildStringValue() protected method

protected string buildStringValue ( string $value, Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params )
$value string
$expression Yiisoft\Db\Expression\Value\StructuredValue
$params array

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

            
buildSubquery() protected method

protected string buildSubquery ( Yiisoft\Db\Query\QueryInterface $query, Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params )
$query Yiisoft\Db\Query\QueryInterface
$expression Yiisoft\Db\Expression\Value\StructuredValue
$params array

                protected function buildSubquery(QueryInterface $query, StructuredValue $expression, array &$params): string
{
    [$sql, $params] = $this->queryBuilder->build($query, $params);
    return "($sql)";
}

            
buildValue() protected method

protected string buildValue ( array|object $value, Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params )
$value array|object
$expression Yiisoft\Db\Expression\Value\StructuredValue
$params array

                protected function buildValue(array|object $value, StructuredValue $expression, array &$params): string
{
    $value = $this->prepareValues($value, $expression);
    $param = new Param(json_encode(array_values($value), JSON_THROW_ON_ERROR), DataType::STRING);
    return $this->queryBuilder->bindParam($param, $params);
}

            
getLazyArrayValue() protected method

protected array|string getLazyArrayValue ( Yiisoft\Db\Schema\Data\LazyArrayInterface $value )
$value Yiisoft\Db\Schema\Data\LazyArrayInterface

                protected function getLazyArrayValue(LazyArrayInterface $value): array|string
{
    return match ($value::class) {
        LazyArray::class, JsonLazyArray::class, StructuredLazyArray::class => $value->getRawValue(),
        default => $value->getValue(),
    };
}

            
prepareValues() protected method

Defined in: Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder::prepareValues()

Returns the prepared value of the structured type, where: - object are converted to an array; - array elements are sorted according to the order of structured type columns; - indexed keys are replaced with column names; - missing elements are filled in with default values; - excessive elements are ignored.

If the structured type columns are not specified it will only convert the object to an array.

protected array prepareValues ( array|object $value, Yiisoft\Db\Expression\Value\StructuredValue $expression )
$value array|object

The structured type value.

$expression Yiisoft\Db\Expression\Value\StructuredValue

The structured expression.

                protected function prepareValues(array|object $value, StructuredValue $expression): array
{
    $value = DbArrayHelper::toArray($value);
    $type = $expression->type;
    $columns = $type instanceof AbstractStructuredColumn ? $type->getColumns() : [];
    if (empty($columns)) {
        return $value;
    }
    $prepared = [];
    $columnNames = array_keys($columns);
    foreach ($columnNames as $i => $columnName) {
        $prepared[$columnName] = match (true) {
            array_key_exists($columnName, $value) => $value[$columnName],
            array_key_exists($i, $value) => $value[$i],
            default => $columns[$columnName]->getDefaultValue(),
        };
    }
    return $prepared;
}