Skip to content

Commit 56df5ac

Browse files
authored
Update Schema::normalizeDefaultValue() (#294)
1 parent 7c402ef commit 56df5ac

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
- Bug #287: Fix `bit` type (@Tigrov)
88
- Enh #289: Array parser refactoring (@Tigrov)
99
- Chg #288: Typecast refactoring (@Tigrov)
10-
- Chg #291: Update phpTypecast for bool type (@Tigrov)
10+
- Chg #291: Update phpTypecast for bool type (@Tigrov)
11+
- Enh #294: Refactoring of `Schema::normalizeDefaultValue()` method (@Tigrov)
1112

1213
## 1.0.0 April 12, 2023
1314

src/Schema.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,13 @@ protected function getColumnPhpType(ColumnSchemaInterface $column): string
842842
*
843843
* @return mixed The normalized default value.
844844
*/
845-
private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterface $column): mixed
845+
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
846846
{
847-
if ($defaultValue === null || $column->isPrimaryKey()) {
847+
if (
848+
$defaultValue === null
849+
|| $column->isPrimaryKey()
850+
|| str_starts_with($defaultValue, 'NULL::')
851+
) {
848852
return null;
849853
}
850854

@@ -859,19 +863,13 @@ private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterf
859863
return new Expression($defaultValue);
860864
}
861865

862-
if (preg_match("/^B?'(.*?)'::/", $defaultValue, $matches) === 1) {
863-
return $column->getType() === self::TYPE_BINARY && str_starts_with($matches[1], '\\x')
864-
? hex2bin(substr($matches[1], 2))
865-
: $column->phpTypecast($matches[1]);
866-
}
866+
$value = preg_replace("/^B?['(](.*?)[)']::[^:]+$/", '$1', $defaultValue);
867867

868-
if (preg_match('/^(\()?(.*?)(?(1)\))(?:::.+)?$/', $defaultValue, $matches) === 1) {
869-
return $matches[2] !== 'NULL'
870-
? $column->phpTypecast($matches[2])
871-
: null;
868+
if ($column->getType() === self::TYPE_BINARY && str_starts_with($value, '\\x')) {
869+
return hex2bin(substr($value, 2));
872870
}
873871

874-
return $column->phpTypecast($defaultValue);
872+
return $column->phpTypecast($value);
875873
}
876874

877875
/**

0 commit comments

Comments
 (0)