Skip to content

Commit

Permalink
wip fix string -> int typecast
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 23, 2024
1 parent eeafc6d commit 24f948c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public function normalize($value)

break;
case 'integer':
case 'bigint':
case 'float':
case 'decimal':
case 'atk4_money':
Expand Down
11 changes: 9 additions & 2 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ private function _typecastPreField(Field $field, $value, bool $fromLoad)
switch ($field->type) {
case 'boolean':
case 'integer':
case 'bigint':
$value = preg_replace('~\s+|,~', '', $value);

break;
Expand All @@ -362,6 +363,7 @@ private function _typecastPreField(Field $field, $value, bool $fromLoad)
switch ($field->type) {
case 'boolean':
case 'integer':
case 'bigint':
case 'float':
case 'decimal':
case 'atk4_money':
Expand All @@ -378,6 +380,7 @@ private function _typecastPreField(Field $field, $value, bool $fromLoad)
case 'string':
case 'text':
case 'integer':
case 'bigint':
case 'float':
case 'decimal':
case 'atk4_money':
Expand Down Expand Up @@ -507,6 +510,7 @@ protected function _typecastSaveField(Field $field, $value)
}

$res = Type::getType($field->type)->convertToDatabaseValue($value, $this->getDatabasePlatform());

if (is_resource($res) && get_resource_type($res) === 'stream') {
$res = stream_get_contents($res);
}
Expand All @@ -527,7 +531,7 @@ 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)) {
if ($value === '' && in_array($field->type, ['boolean', 'integer', 'bigint', 'float', 'decimal', 'datetime', 'date', 'time', 'json', 'object'], true)) {
return null;
}

Expand Down Expand Up @@ -556,7 +560,10 @@ protected function _typecastLoadField(Field $field, $value)
}

$res = Type::getType($field->type)->convertToPHPValue($value, $this->getDatabasePlatform());
if (is_resource($res) && get_resource_type($res) === 'stream') {

if ($field->type === 'bigint' && $res === (string) (int) $res) {
$res = (int) $res;
} elseif (is_resource($res) && get_resource_type($res) === 'stream') {
$res = stream_get_contents($res);
}

Expand Down

0 comments on commit 24f948c

Please sign in to comment.