Final Class Yiisoft\Db\Migration\Service\Generate\FieldsParser
| Inheritance | Yiisoft\Db\Migration\Service\Generate\FieldsParser |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Migration\Service\Generate\FieldsParser | |
| parse() | Yiisoft\Db\Migration\Service\Generate\FieldsParser |
Method Details
| public mixed __construct ( Yiisoft\Db\Migration\Service\Generate\ForeignKeyFactory $foreignKeyFactory ) | ||
| $foreignKeyFactory | Yiisoft\Db\Migration\Service\Generate\ForeignKeyFactory | |
public function __construct(
private readonly ForeignKeyFactory $foreignKeyFactory,
) {}
| public array[] parse ( string $table, string|null $value, boolean $addDefaultPrimaryKey ) | ||
| $table | string | |
| $value | string|null | |
| $addDefaultPrimaryKey | boolean | |
public function parse(
string $table,
?string $value,
bool $addDefaultPrimaryKey,
): array {
$columns = [];
$foreignKeys = [];
if (!empty($value)) {
$fields = explode(',', $value);
/** @psalm-var string[] $fields */
foreach ($fields as $field) {
$chunks = $this->splitFieldIntoChunks($field);
$columnName = (string) array_shift($chunks);
/** @psalm-var string[] $chunks */
foreach ($chunks as $i => $chunk) {
if (str_starts_with($chunk, 'foreignKey')) {
preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
/** @psalm-suppress PossiblyNullArgument */
$foreignKeys[] = $this->foreignKeyFactory->create(
$table,
$columnName,
$matches[1] ?? preg_replace('/_id$/', '', $columnName),
empty($matches[2]) ? null : $matches[2],
);
unset($chunks[$i]);
continue;
}
if (!preg_match('/\(([^(]+)\)$/', $chunk)) {
$chunks[$i] .= '()';
}
}
/** @psalm-var string[] $chunks */
$columns[] = new Column($columnName, $chunks);
}
}
if ($addDefaultPrimaryKey) {
$this->addDefaultPrimaryKey($columns);
}
return [$columns, $foreignKeys];
}
Signup or Login in order to comment.