From 56df5ac9907ecc0582d3f4303b711588620a806e Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 20 Jul 2023 23:00:32 +0700 Subject: [PATCH] Update `Schema::normalizeDefaultValue()` (#294) --- CHANGELOG.md | 3 ++- src/Schema.php | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) 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); } /**