Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion system/DataCaster/Cast/FloatCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace CodeIgniter\DataCaster\Cast;

use CodeIgniter\DataCaster\Exceptions\CastException;

/**
* Class FloatCast
*
Expand All @@ -30,6 +32,31 @@ public static function get(
self::invalidTypeValueError($value);
}

return (float) $value;
$precision = isset($params[0]) ? (int) $params[0] : null;

if ($precision === null) {
return (float) $value;
}

// Map string flags to PHP constants
$modeMap = [
'up' => PHP_ROUND_HALF_UP,
'down' => PHP_ROUND_HALF_DOWN,
'even' => PHP_ROUND_HALF_EVEN,
'odd' => PHP_ROUND_HALF_ODD,
];

$mode = PHP_ROUND_HALF_UP; // Default mode

if (isset($params[1])) {
$modeParam = strtolower($params[1]);
if (isset($modeMap[$modeParam])) {
$mode = $modeMap[$modeParam];
} else {
throw CastException::forInvalidFloatRoundingMode($params[1]);
}
Comment on lines +53 to +57
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use match here and throw by default?

$mode = PHP_ROUND_HALF_UP; // Default mode

if (isset($params[1])) {
    $mode = match (strtolower($params[1])) {
        'up'   => PHP_ROUND_HALF_UP,
        'down' => PHP_ROUND_HALF_DOWN,
        'even' => PHP_ROUND_HALF_EVEN,
        'odd'  => PHP_ROUND_HALF_ODD,
        default => throw CastException::forInvalidFloatRoundingMode($params[1]),
    };
}

}

return round((float) $value, $precision, $mode);
}
}
10 changes: 10 additions & 0 deletions system/Entity/Exceptions/CastException.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,14 @@ public static function forInvalidEnumType(string $expectedClass, string $actualC
{
return new static(lang('Cast.enumInvalidType', [$actualClass, $expectedClass]));
}

/**
* Thrown when an invalid rounding mode is provided for float casting.
*
* @return static
*/
public static function forInvalidFloatRoundingMode(string $mode)
{
return new static(lang('Cast.floatInvalidRoundingMode', [$mode]));
}
}
56 changes: 56 additions & 0 deletions tests/system/DataConverter/DataConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,62 @@ public static function provideConvertDataFromDB(): iterable
'temp' => 15.9,
],
],
'float precise' => [
[
'id' => 'int',
'temp' => 'float[2]',
],
[
'id' => '1',
'temp' => '15.98765',
],
[
'id' => 1,
'temp' => 15.99,
],
],
'float precise-down' => [
[
'id' => 'int',
'temp' => 'float[2,down]',
],
[
'id' => '1',
'temp' => '1.235',
],
[
'id' => 1,
'temp' => 1.23,
],
],
'float precise-even' => [
[
'id' => 'int',
'temp' => 'float[2,even]',
],
[
'id' => '1',
'temp' => '20.005',
],
[
'id' => 1,
'temp' => 20.00,
],
],
'float precise-odd' => [
[
'id' => 'int',
'temp' => 'float[2,odd]',
],
[
'id' => '1',
'temp' => '1.255',
],
[
'id' => 1,
'temp' => 1.25,
],
],
'enum string-backed' => [
[
'id' => 'int',
Expand Down
Loading