From adcb2eeddfabd2bde57bd94c3421a6479e944661 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 30 Jul 2023 02:42:05 +0700 Subject: [PATCH] Refactor ColumnSchema.php (#302) --- CHANGELOG.md | 2 ++ src/ColumnSchema.php | 21 ++++++++++----------- tests/ColumnSchemaTest.php | 9 +++++++++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e85fe6fda..094958f1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Enh #301: Refactor `JsonExpressionBuilder` (@Tigrov) - Enh #300: Refactor `ArrayExpressionBuilder` (@Tigrov) +- Enh #302: Refactor `ColumnSchema` (@Tigrov) +- Bug #302: Fix incorrect convert string value for BIT type (@Tigrov) ## 1.1.0 July 24, 2023 diff --git a/src/ColumnSchema.php b/src/ColumnSchema.php index a40a6ab78..db39d788b 100644 --- a/src/ColumnSchema.php +++ b/src/ColumnSchema.php @@ -65,8 +65,7 @@ final class ColumnSchema extends AbstractColumnSchema * * @param mixed $value input value * - * @return mixed Converted value. This may also be an array containing the value as the first element and the PDO - * type as the second element. + * @return mixed Converted value. */ public function dbTypecast(mixed $value): mixed { @@ -87,7 +86,7 @@ public function dbTypecast(mixed $value): mixed Schema::TYPE_BIT => is_int($value) ? str_pad(decbin($value), (int) $this->getSize(), '0', STR_PAD_LEFT) - : $this->typecast($value), + : (string) $value, default => $this->typecast($value), }; @@ -111,15 +110,15 @@ public function phpTypecast(mixed $value): mixed $value = $this->getArrayParser()->parse($value); } - if (is_array($value)) { - array_walk_recursive($value, function (string|null &$val) { - /** @psalm-var mixed $val */ - $val = $this->phpTypecastValue($val); - }); - } else { + if (!is_array($value)) { return null; } + array_walk_recursive($value, function (mixed &$val) { + /** @psalm-var mixed $val */ + $val = $this->phpTypecastValue($val); + }); + return $value; } @@ -131,7 +130,7 @@ public function phpTypecast(mixed $value): mixed * * @throws JsonException */ - protected function phpTypecastValue(mixed $value): mixed + private function phpTypecastValue(mixed $value): mixed { if ($value === null) { return null; @@ -152,7 +151,7 @@ protected function phpTypecastValue(mixed $value): mixed /** * Creates instance of ArrayParser. */ - protected function getArrayParser(): ArrayParser + private function getArrayParser(): ArrayParser { return new ArrayParser(); } diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnSchemaTest.php index 01b8ed031..2c8195acb 100644 --- a/tests/ColumnSchemaTest.php +++ b/tests/ColumnSchemaTest.php @@ -188,4 +188,13 @@ public function testNegativeDefaultValues() $this->assertSame(-12345.6789, $tableSchema->getColumn('float_col')->getDefaultValue()); $this->assertSame(-33.22, $tableSchema->getColumn('numeric_col')->getDefaultValue()); } + + public function testDbTypeCastBit() + { + $db = $this->getConnection(true); + $schema = $db->getSchema(); + $tableSchema = $schema->getTableSchema('type'); + + $this->assertSame('01100100', $tableSchema->getColumn('bit_col')->dbTypecast('01100100')); + } }