0 follower

Final Class Yiisoft\Db\Schema\Data\StructuredLazyArray

InheritanceYiisoft\Db\Schema\Data\StructuredLazyArray » Yiisoft\Db\Schema\Data\AbstractStructuredLazyArray
ImplementsArrayAccess, Countable, IteratorAggregate, JsonSerializable, Yiisoft\Db\Schema\Data\LazyArrayInterface
Uses TraitsYiisoft\Db\Schema\Data\LazyArrayTrait

Represents a structured JSON array value retrieved from the database.

Initially, the value is a string that parsed into an array when it's accessed as an array or iterated over.

Protected Methods

Hide inherited methods

Method Description Defined By
parse() Yiisoft\Db\Schema\Data\StructuredLazyArray
phpTypecast() Typecasts the structured values to PHP types according to the column schemas information. Yiisoft\Db\Schema\Data\AbstractStructuredLazyArray
prepareValue() Prepares the value to be used as an array or throws an exception if it's impossible. Yiisoft\Db\Schema\Data\AbstractStructuredLazyArray

Method Details

Hide inherited methods

__construct() public method
public __construct( string $value, Yiisoft\Db\Schema\Column\ColumnInterface[] $columns = [] ): mixed
$value string

The string retrieved value from the database that can be parsed into an array.

$columns Yiisoft\Db\Schema\Column\ColumnInterface[]

The structured type columns that are used for value normalization and type casting.

                public function __construct(
    string $value,
    private readonly array $columns = [],
) {
    $this->value = $value;
}

            
count() public method
public count( ): integer

                public function count(): int
{
    $this->prepareValue();
    return count($this->value);
}

            
getIterator() public method
public getIterator( ): ArrayIterator

                public function getIterator(): ArrayIterator
{
    $this->prepareValue();
    return new ArrayIterator($this->value);
}

            
getRawValue() public method

Defined in: Yiisoft\Db\Schema\Data\LazyArrayTrait::getRawValue()

The raw value that can be represented as: - a string retrieved value from the database that can be parsed into an array; - an array of values if the value is already parsed.

public getRawValue( ): array|string

                public function getRawValue(): array|string
{
    return $this->value;
}

            
getValue() public method

Defined in: Yiisoft\Db\Schema\Data\LazyArrayTrait::getValue()

Returns parsed and typecasted value.

public getValue( ): array

                public function getValue(): array
{
    $this->prepareValue();
    return $this->value;
}

            
jsonSerialize() public method
public jsonSerialize( ): array

                public function jsonSerialize(): array
{
    return $this->getValue();
}

            
offsetExists() public method
public offsetExists( integer|string $offset ): boolean
$offset integer|string

The offset to check.

                public function offsetExists(mixed $offset): bool
{
    $this->prepareValue();
    return isset($this->value[$offset]);
}

            
offsetGet() public method
public offsetGet( integer|string $offset ): mixed
$offset integer|string

The offset to retrieve.

                public function offsetGet(mixed $offset): mixed
{
    $this->prepareValue();
    return $this->value[$offset];
}

            
offsetSet() public method
public offsetSet( integer|string $offset, mixed $value ): void
$offset integer|string

The offset to assign the value to.

$value mixed

                public function offsetSet(mixed $offset, mixed $value): void
{
    $this->prepareValue();
    $this->value[$offset] = $value;
}

            
offsetUnset() public method
public offsetUnset( integer|string $offset ): void
$offset integer|string

The offset to unset.

                public function offsetUnset(mixed $offset): void
{
    $this->prepareValue();
    unset($this->value[$offset]);
}

            
parse() protected method

protected parse( string $value ): array|null
$value string

                protected function parse(string $value): ?array
{
    /** @var array|null */
    return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
}

            
phpTypecast() protected method

Defined in: Yiisoft\Db\Schema\Data\AbstractStructuredLazyArray::phpTypecast()

Typecasts the structured values to PHP types according to the column schemas information.

protected phpTypecast( array $value ): array
$value array

                protected function phpTypecast(array $value): array
{
    if (empty($this->columns)) {
        return $value;
    }
    $fields = [];
    $columnNames = array_keys($this->columns);
    foreach ($value as $columnName => $item) {
        $columnName = $columnNames[$columnName] ?? $columnName;
        if (isset($this->columns[$columnName])) {
            $fields[$columnName] = $this->columns[$columnName]->phpTypecast($item);
        } else {
            $fields[$columnName] = $item;
        }
    }
    return $fields;
}

            
prepareValue() protected method

Defined in: Yiisoft\Db\Schema\Data\AbstractStructuredLazyArray::prepareValue()

Prepares the value to be used as an array or throws an exception if it's impossible.

protected prepareValue( ): void

                protected function prepareValue(): void
{
    if (is_string($this->value)) {
        $value = $this->parse($this->value);
        if ($value === null) {
            throw new InvalidArgumentException('Structured value must be a valid string representation.');
        }
        $this->value = $this->phpTypecast($value);
    }
}