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 committed Jul 30, 2023
1 parent d5f295e commit 4119262
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 @@ -74,8 +74,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 Down Expand Up @@ -141,7 +140,7 @@ private function dbTypecastValue(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,

Schema::TYPE_COMPOSITE => new CompositeExpression($value, $this->getDbType(), $this->columns),

Expand All @@ -167,15 +166,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 @@ -187,7 +186,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 Down Expand Up @@ -246,7 +245,7 @@ private function phpTypecastComposite(mixed $value): array|null
/**
* 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 @@ -189,6 +189,15 @@ public function testNegativeDefaultValues()
$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'));
}

public function testCompositeType(): void
{
$db = $this->getConnection(true);
Expand Down

0 comments on commit 4119262

Please sign in to comment.