Final Class Yiisoft\Db\Schema\Data\LazyArray
Represents a 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 Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $value | array|string | The array value that can be represented as: - a string that can be parsed into an array; - an array that is already parsed and typecasted. | Yiisoft\Db\Schema\Data\AbstractLazyArray |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Schema\Data\AbstractLazyArray | |
| count() | Yiisoft\Db\Schema\Data\LazyArrayTrait | |
| getIterator() | 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. | Yiisoft\Db\Schema\Data\LazyArrayTrait |
| getValue() | Returns parsed and typecasted value. | Yiisoft\Db\Schema\Data\LazyArrayTrait |
| jsonSerialize() | Yiisoft\Db\Schema\Data\LazyArrayTrait | |
| offsetExists() | Yiisoft\Db\Schema\Data\LazyArrayTrait | |
| offsetGet() | Yiisoft\Db\Schema\Data\LazyArrayTrait | |
| offsetSet() | Yiisoft\Db\Schema\Data\LazyArrayTrait | |
| offsetUnset() | Yiisoft\Db\Schema\Data\LazyArrayTrait |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| parse() | Yiisoft\Db\Schema\Data\LazyArray | |
| phpTypecast() | Typecasts the array values to PHP types according to the column information. | Yiisoft\Db\Schema\Data\AbstractLazyArray |
| prepareValue() | Prepares the value to be used as an array or throws an exception if it's impossible. | Yiisoft\Db\Schema\Data\AbstractLazyArray |
Method Details
| public __construct( string $value, Yiisoft\Db\Schema\Column\ColumnInterface|null $column = null, integer $dimension = 1 ): mixed | ||
| $value | string |
The string retrieved value from the database that can be parsed into an array. |
| $column | Yiisoft\Db\Schema\Column\ColumnInterface|null |
The column information. This is used to typecast values. |
| $dimension | integer |
The number of indices needed to select an element. |
public function __construct(
string $value,
private readonly ?ColumnInterface $column = null,
private readonly int $dimension = 1,
) {
$this->value = $value;
}
Defined in: Yiisoft\Db\Schema\Data\LazyArrayTrait::count()
| public count( ): integer |
public function count(): int
{
$this->prepareValue();
return count($this->value);
}
| public getIterator( ): ArrayIterator |
public function getIterator(): ArrayIterator
{
$this->prepareValue();
return new ArrayIterator($this->value);
}
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;
}
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;
}
| public jsonSerialize( ): array |
public function jsonSerialize(): array
{
return $this->getValue();
}
| 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]);
}
| 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];
}
| 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;
}
| 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]);
}
| 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);
}
Defined in: Yiisoft\Db\Schema\Data\AbstractLazyArray::phpTypecast()
Typecasts the array values to PHP types according to the column information.
| protected phpTypecast( array $value ): array | ||
| $value | array |
The array to typecast. |
| return | array |
The typecasted array. |
|---|---|---|
protected function phpTypecast(array $value): array
{
if ($this->column === null || $this->column->getType() === ColumnType::STRING) {
return $value;
}
if ($this->dimension === 1 && $this->column->getType() !== ColumnType::JSON) {
return array_map($this->column->phpTypecast(...), $value);
}
array_walk_recursive($value, function (?string &$val): void {
/** @psalm-suppress PossiblyNullReference */
$val = $this->column->phpTypecast($val);
});
return $value;
}
Defined in: Yiisoft\Db\Schema\Data\AbstractLazyArray::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('Array value must be a valid string representation.');
}
$this->value = $this->phpTypecast($value);
}
}
Signup or Login in order to comment.