Skip to content

Commit

Permalink
Update Schema::normalizeDefaultValue() (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jul 20, 2023
1 parent 7c402ef commit 56df5ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 10 additions & 12 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 56df5ac

Please sign in to comment.