diff --git a/CHANGELOG.md b/CHANGELOG.md index e4c6eb318..e7fcc11cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ - Bug #287: Fix `bit` type (@Tigrov) - Enh #289: Array parser refactoring (@Tigrov) - Chg #288: Typecast refactoring (@Tigrov) -- Chg #291: Update phpTypecast for bool type (@Tigrov) +- Chg #291: Update phpTypecast for bool type (@Tigrov) +- Enh #294: Refactoring of `Schema::normalizeDefaultValue()` method (@Tigrov) ## 1.0.0 April 12, 2023 diff --git a/src/Schema.php b/src/Schema.php index 3c8932b0c..768893494 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -842,9 +842,13 @@ protected function getColumnPhpType(ColumnSchemaInterface $column): string * * @return mixed The normalized default value. */ - private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterface $column): mixed + private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed { - if ($defaultValue === null || $column->isPrimaryKey()) { + if ( + $defaultValue === null + || $column->isPrimaryKey() + || str_starts_with($defaultValue, 'NULL::') + ) { return null; } @@ -859,19 +863,13 @@ private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterf return new Expression($defaultValue); } - if (preg_match("/^B?'(.*?)'::/", $defaultValue, $matches) === 1) { - return $column->getType() === self::TYPE_BINARY && str_starts_with($matches[1], '\\x') - ? hex2bin(substr($matches[1], 2)) - : $column->phpTypecast($matches[1]); - } + $value = preg_replace("/^B?['(](.*?)[)']::[^:]+$/", '$1', $defaultValue); - if (preg_match('/^(\()?(.*?)(?(1)\))(?:::.+)?$/', $defaultValue, $matches) === 1) { - return $matches[2] !== 'NULL' - ? $column->phpTypecast($matches[2]) - : null; + if ($column->getType() === self::TYPE_BINARY && str_starts_with($value, '\\x')) { + return hex2bin(substr($value, 2)); } - return $column->phpTypecast($defaultValue); + return $column->phpTypecast($value); } /**