Abstract Class Yiisoft\Db\Schema\Column\AbstractColumnFactory
| Inheritance | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
|---|---|
| Implements | Yiisoft\Db\Schema\Column\ColumnFactoryInterface |
The default implementation of the {@see ColumnFactoryInterface}.
Psalm Types
| Name | Value |
|---|---|
| ColumnClassNameDefinition | class-string<Yiisoft\Db\Schema\Column\ColumnInterface>|callable |
| TypeMap | array<string, \Yiisoft\Db\Constant\ColumnType::*|callable> |
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $classDefaults | array | Yiisoft\Db\Schema\Column\AbstractColumnFactory | |
| $classMap | array | Yiisoft\Db\Schema\Column\AbstractColumnFactory | |
| $map | array | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| columnDefinitionParser() | Returns the column definition parser. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| getColumnClass() | Yiisoft\Db\Schema\Column\AbstractColumnFactory | |
| getType() | Get the abstract database type for a database column type. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| isDbType() | Checks if the column type is a database type. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| isPseudoType() | Checks if the column type is a pseudo-type. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| isType() | Checks if the column type is an abstract type. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| mapType() | Maps a type to a value using a mapping array. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| normalizeDefaultValue() | Converts column's default value after retrieval from the database. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
| normalizeNotNullDefaultValue() | Converts a not null default value. | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| TYPE_MAP | [] | Yiisoft\Db\Schema\Column\AbstractColumnFactory |
Property Details
Method Details
| public mixed __construct ( array $map = [], array $definitions = [] ) | ||
| $map | array |
The mapping from physical column types to abstract column types. Where array keys
are physical column types and values are corresponding abstract column types or PHP callable with the following
signature: |
| $definitions | array |
Definitions of column classes that implement the abstract column types. Keys are abstract column types represented by {@see ColumnType::*}. Values can be:
For example: $map = [
];
|
public function __construct(
protected array $map = [],
array $definitions = [],
) {
$classMap = [];
$classDefaults = [];
foreach ($definitions as $type => $value) {
if (is_array($value)) {
if (array_key_exists(0, $value)) {
$classMap[$type] = $value[0];
unset($value[0]);
}
if (!empty($value)) {
$classDefaults[$type] = $value;
}
continue;
}
$classMap[$type] = $value;
}
$this->classMap = $classMap;
$this->classDefaults = $classDefaults;
}
Returns the column definition parser.
| protected Yiisoft\Db\Syntax\ColumnDefinitionParser columnDefinitionParser ( ) |
protected function columnDefinitionParser(): ColumnDefinitionParser
{
return new ColumnDefinitionParser();
}
| public Yiisoft\Db\Schema\Column\ColumnInterface fromDbType ( string $dbType, array $info = [] ) | ||
| $dbType | string | |
| $info | array | |
public function fromDbType(string $dbType, array $info = []): ColumnInterface
{
$info['dbType'] = $dbType;
/** @psalm-var ColumnType::* $type */
$type = $info['type']
?? $this->mapType($this->map, $dbType, $info)
?? $this->getType($dbType, $info);
return $this->fromType($type, $info);
}
| public Yiisoft\Db\Schema\Column\ColumnInterface fromDefinition ( string $definition, array $info = [] ) | ||
| $definition | string | |
| $info | array | |
public function fromDefinition(string $definition, array $info = []): ColumnInterface
{
$definitionInfo = $this->columnDefinitionParser()->parse($definition);
if (isset($info['extra'], $definitionInfo['extra'])) {
$info['extra'] = $definitionInfo['extra'] . ' ' . $info['extra'];
unset($definitionInfo['extra']);
}
/** @var string $type */
$type = $definitionInfo['type'] ?? '';
unset($definitionInfo['type']);
$info = array_merge($info, $definitionInfo);
$info['source'] ??= ColumnInfoSource::DEFINITION;
if ($this->isDbType($type)) {
return $this->fromDbType($type, $info);
}
if ($this->isType($type)) {
return $this->fromType($type, $info);
}
if ($this->isPseudoType($type)) {
return $this->fromPseudoType($type, $info);
}
return $this->fromDbType($type, $info);
}
| public Yiisoft\Db\Schema\Column\ColumnInterface fromPseudoType ( string $pseudoType, array $info = [] ) | ||
| $pseudoType | string | |
| $info | array | |
public function fromPseudoType(string $pseudoType, array $info = []): ColumnInterface
{
$info['primaryKey'] = true;
$info['autoIncrement'] = true;
if ($pseudoType === PseudoType::UPK || $pseudoType === PseudoType::UBIGPK) {
$info['unsigned'] = true;
}
$type = match ($pseudoType) {
PseudoType::PK => ColumnType::INTEGER,
PseudoType::UPK => ColumnType::INTEGER,
PseudoType::BIGPK => ColumnType::BIGINT,
PseudoType::UBIGPK => ColumnType::BIGINT,
PseudoType::UUID_PK => ColumnType::UUID,
PseudoType::UUID_PK_SEQ => ColumnType::UUID,
};
return $this->fromType($type, $info);
}
| public Yiisoft\Db\Schema\Column\ColumnInterface fromType ( string $type, array $info = [] ) | ||
| $type | string | |
| $info | array | |
public function fromType(string $type, array $info = []): ColumnInterface
{
$columnType = $info['type'] ?? $type;
unset($info['type']);
if ($type === ColumnType::ARRAY || !empty($info['dimension'])) {
if (empty($info['column'])) {
if (!empty($info['dbType']) && $info['dbType'] !== ColumnType::ARRAY) {
/** @psalm-suppress ArgumentTypeCoercion */
$info['column'] = $this->fromDbType(
$info['dbType'],
array_diff_key($info, ['dimension' => 1, 'defaultValueRaw' => 1]),
);
} elseif ($type !== ColumnType::ARRAY) {
/** @psalm-suppress ArgumentTypeCoercion */
$info['column'] = $this->fromType(
$type,
array_diff_key($info, ['dimension' => 1, 'defaultValueRaw' => 1]),
);
}
}
$columnType = $type = ColumnType::ARRAY;
}
/** @psalm-var class-string<ColumnInterface> $columnClass */
$columnClass = $this->mapType($this->classMap, $type, $info)
?? $this->getColumnClass($type, $info);
$columnParams = $info + ($this->classDefaults[$type] ?? []);
$column = new $columnClass($columnType, ...$columnParams);
if (array_key_exists('defaultValueRaw', $info)) {
$column->defaultValue($this->normalizeDefaultValue($info['defaultValueRaw'], $column));
}
return $column;
}
| protected string getColumnClass ( string $type, array $info = [] ) | ||
| $type | string | |
| $info | array | |
protected function getColumnClass(string $type, array $info = []): string
{
return match ($type) {
ColumnType::BOOLEAN => BooleanColumn::class,
ColumnType::BIT => BitColumn::class,
ColumnType::TINYINT => IntegerColumn::class,
ColumnType::SMALLINT => IntegerColumn::class,
ColumnType::INTEGER => PHP_INT_SIZE !== 8 && !empty($info['unsigned'])
? BigIntColumn::class
: IntegerColumn::class,
ColumnType::BIGINT => PHP_INT_SIZE !== 8 || !empty($info['unsigned'])
? BigIntColumn::class
: IntegerColumn::class,
ColumnType::DECIMAL => StringColumn::class,
ColumnType::FLOAT => DoubleColumn::class,
ColumnType::DOUBLE => DoubleColumn::class,
ColumnType::BINARY => BinaryColumn::class,
ColumnType::TIMESTAMP => DateTimeColumn::class,
ColumnType::DATETIME => DateTimeColumn::class,
ColumnType::DATETIMETZ => DateTimeColumn::class,
ColumnType::TIME => DateTimeColumn::class,
ColumnType::TIMETZ => DateTimeColumn::class,
ColumnType::DATE => DateTimeColumn::class,
ColumnType::ARRAY => ArrayColumn::class,
ColumnType::STRUCTURED => StructuredColumn::class,
ColumnType::JSON => JsonColumn::class,
ColumnType::ENUM => EnumColumn::class,
default => StringColumn::class,
};
}
Get the abstract database type for a database column type.
| protected string getType ( string $dbType, array $info = [] ) | ||
| $dbType | string |
The database column type. |
| $info | array |
The column information. |
| return | string |
The abstract database type. |
|---|---|---|
protected function getType(string $dbType, array $info = []): string
{
if (!empty($info['dimension'])) {
return ColumnType::ARRAY;
}
if (isset($info['values'])) {
return ColumnType::ENUM;
}
return static::TYPE_MAP[$dbType] ?? ColumnType::STRING;
}
Checks if the column type is a database type.
| protected boolean isDbType ( string $dbType ) | ||
| $dbType | string | |
protected function isDbType(string $dbType): bool
{
return isset(static::TYPE_MAP[$dbType]) || !($this->isType($dbType) || $this->isPseudoType($dbType));
}
Checks if the column type is a pseudo-type.
| protected boolean isPseudoType ( string $pseudoType ) | ||
| $pseudoType | string | |
protected function isPseudoType(string $pseudoType): bool
{
return match ($pseudoType) {
PseudoType::PK,
PseudoType::UPK,
PseudoType::BIGPK,
PseudoType::UBIGPK,
PseudoType::UUID_PK,
PseudoType::UUID_PK_SEQ => true,
default => false,
};
}
Checks if the column type is an abstract type.
| protected boolean isType ( string $type ) | ||
| $type | string | |
protected function isType(string $type): bool
{
return match ($type) {
ColumnType::BOOLEAN,
ColumnType::BIT,
ColumnType::TINYINT,
ColumnType::SMALLINT,
ColumnType::INTEGER,
ColumnType::BIGINT,
ColumnType::FLOAT,
ColumnType::DOUBLE,
ColumnType::DECIMAL,
ColumnType::MONEY,
ColumnType::CHAR,
ColumnType::STRING,
ColumnType::TEXT,
ColumnType::BINARY,
ColumnType::UUID,
ColumnType::TIMESTAMP,
ColumnType::DATETIME,
ColumnType::DATETIMETZ,
ColumnType::TIME,
ColumnType::TIMETZ,
ColumnType::DATE,
ColumnType::ARRAY,
ColumnType::STRUCTURED,
ColumnType::JSON,
ColumnType::ENUM => true,
default => isset($this->classMap[$type]),
};
}
Maps a type to a value using a mapping array.
| protected string|null mapType ( array $map, string $type, array &$info = [] ) | ||
| $map | array |
The mapping array. |
| $type | string |
The type to map. |
| $info | array |
The column information. |
| return | string|null |
The mapped value or |
|---|---|---|
protected function mapType(array $map, string $type, array &$info = []): ?string
{
if (!isset($map[$type])) {
return null;
}
if (is_callable($map[$type])) {
/** @var string|null */
return $map[$type]($type, $info);
}
/** @var string */
return $map[$type];
}
Converts column's default value after retrieval from the database.
| protected mixed normalizeDefaultValue ( string|null $defaultValue, Yiisoft\Db\Schema\Column\ColumnInterface $column ) | ||
| $defaultValue | string|null |
The default value retrieved from the database. |
| $column | Yiisoft\Db\Schema\Column\ColumnInterface |
The column object. |
| return | mixed |
The normalized default value. |
|---|---|---|
protected function normalizeDefaultValue(?string $defaultValue, ColumnInterface $column): mixed
{
if (
$defaultValue === null
|| $defaultValue === ''
|| $column->isPrimaryKey()
|| $column->isComputed()
|| preg_match('/^\(?NULL\b/i', $defaultValue) === 1
) {
return null;
}
return $this->normalizeNotNullDefaultValue($defaultValue, $column);
}
Converts a not null default value.
| protected mixed normalizeNotNullDefaultValue ( string $defaultValue, Yiisoft\Db\Schema\Column\ColumnInterface $column ) | ||
| $defaultValue | string | |
| $column | Yiisoft\Db\Schema\Column\ColumnInterface | |
protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInterface $column): mixed
{
$value = $defaultValue;
if ($value[0] === '(' && $value[-1] === ')') {
$value = substr($value, 1, -1);
}
if (is_numeric($value)) {
return $column->phpTypecast($value);
}
if ($value[0] === "'" && $value[-1] === "'") {
$value = substr($value, 1, -1);
$value = str_replace("''", "'", $value);
return $column->phpTypecast($value);
}
return match ($value) {
'true' => true,
'false' => false,
default => new Expression($defaultValue),
};
}
Signup or Login in order to comment.