Skip to content

Commit

Permalink
Refactor ColumnSchema.php (yiisoft#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jul 29, 2023
1 parent 79527de commit adcb2ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 10 additions & 11 deletions src/ColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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),
};
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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();
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit adcb2ee

Please sign in to comment.