Skip to content

Commit

Permalink
add smallint/bigint cast to null
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 24, 2024
1 parent d729f6f commit 557253c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,21 +359,37 @@ private function _typecastPreField(Field $field, $value, bool $fromLoad)
break;
}

switch ($field->type) {
case 'boolean':
case 'smallint':
case 'integer':
case 'bigint':
case 'float':
case 'decimal':
case 'atk4_money':
if ($value === '') {
if ($value === '') {
// TODO should be handled by DBAL types itself like "json" type already does
// https://github.com/doctrine/dbal/blob/4.0.2/src/Types/JsonType.php#L55
switch ($field->type) {
case 'boolean':
case 'smallint':
case 'integer':
case 'bigint':
case 'float':
case 'decimal':
case 'atk4_money':
case 'object':
$value = null;
} elseif (!is_numeric($value)) {
throw new Exception('Must be numeric');
}

break;
break;
}
} else {
switch ($field->type) {
case 'boolean':
case 'smallint':
case 'integer':
case 'bigint':
case 'float':
case 'decimal':
case 'atk4_money':
if (!is_numeric($value)) {
throw new Exception('Must be numeric');
}

break;
}
}
} elseif ($value !== null) {
switch ($field->type) {
Expand Down Expand Up @@ -529,11 +545,6 @@ protected function _typecastLoadField(Field $field, $value)
{
$value = $this->_typecastPreField($field, $value, true);

// TODO casting optionally to null should be handled by type itself solely
if ($value === '' && in_array($field->type, ['boolean', 'integer', 'float', 'decimal', 'datetime', 'date', 'time', 'json', 'object'], true)) {
return null;
}

// native DBAL DT types have no microseconds support
if (in_array($field->type, ['datetime', 'date', 'time'], true)
&& str_starts_with(get_class(Type::getType($field->type)), 'Doctrine\DBAL\Types\\')) {
Expand Down
10 changes: 10 additions & 0 deletions tests/TypecastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ public function testEmptyValues(): void
'datetime' => '',
'time' => '',
'boolean' => '',
'smallint' => '',
'integer' => '',
'bigint' => '',
'money' => '',
'float' => '',
'decimal' => '',
Expand All @@ -177,7 +179,9 @@ public function testEmptyValues(): void
$m->addField('datetime', ['type' => 'datetime']);
$m->addField('time', ['type' => 'time']);
$m->addField('boolean', ['type' => 'boolean']);
$m->addField('smallint', ['type' => 'smallint']);
$m->addField('integer', ['type' => 'integer']);
$m->addField('bigint', ['type' => 'bigint']);
$m->addField('money', ['type' => 'atk4_money']);
$m->addField('float', ['type' => 'float']);
$m->addField('decimal', ['type' => 'decimal']);
Expand All @@ -192,7 +196,9 @@ public function testEmptyValues(): void
self::assertNull($mm->get('datetime'));
self::assertNull($mm->get('time'));
self::assertNull($mm->get('boolean'));
self::assertNull($mm->get('smallint'));
self::assertNull($mm->get('integer'));
self::assertNull($mm->get('bigint'));
self::assertNull($mm->get('money'));
self::assertNull($mm->get('float'));
self::assertNull($mm->get('decimal'));
Expand All @@ -210,7 +216,9 @@ public function testEmptyValues(): void
self::assertNull($mm->get('datetime'));
self::assertNull($mm->get('time'));
self::assertNull($mm->get('boolean'));
self::assertNull($mm->get('smallint'));
self::assertNull($mm->get('integer'));
self::assertNull($mm->get('bigint'));
self::assertNull($mm->get('money'));
self::assertNull($mm->get('float'));
self::assertNull($mm->get('decimal'));
Expand Down Expand Up @@ -241,7 +249,9 @@ public function testEmptyValues(): void
'datetime' => null,
'time' => null,
'boolean' => null,
'smallint' => null,
'integer' => null,
'bigint' => null,
'money' => null,
'float' => null,
'decimal' => null,
Expand Down

0 comments on commit 557253c

Please sign in to comment.