Skip to content

Commit 31d2b6f

Browse files
committed
Apply suggested changes and added language key and docs
1 parent d0717fc commit 31d2b6f

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

system/DataCaster/Cast/FloatCast.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,16 @@ public static function get(
3838
return (float) $value;
3939
}
4040

41-
// Map string flags to PHP constants
42-
$modeMap = [
43-
'up' => PHP_ROUND_HALF_UP,
44-
'down' => PHP_ROUND_HALF_DOWN,
45-
'even' => PHP_ROUND_HALF_EVEN,
46-
'odd' => PHP_ROUND_HALF_ODD,
47-
];
48-
4941
$mode = PHP_ROUND_HALF_UP; // Default mode
5042

5143
if (isset($params[1])) {
52-
$modeParam = strtolower($params[1]);
53-
if (isset($modeMap[$modeParam])) {
54-
$mode = $modeMap[$modeParam];
55-
} else {
56-
throw CastException::forInvalidFloatRoundingMode($params[1]);
57-
}
44+
$mode = match (strtolower($params[1])) {
45+
'up' => PHP_ROUND_HALF_UP,
46+
'down' => PHP_ROUND_HALF_DOWN,
47+
'even' => PHP_ROUND_HALF_EVEN,
48+
'odd' => PHP_ROUND_HALF_ODD,
49+
default => throw CastException::forInvalidFloatRoundingMode($params[1]),
50+
};
5851
}
5952

6053
return round((float) $value, $precision, $mode);

system/Entity/Cast/FloatCast.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,30 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16+
use CodeIgniter\DataCaster\Exceptions\CastException;
17+
1618
class FloatCast extends BaseCast
1719
{
1820
public static function get($value, array $params = []): float
1921
{
20-
return (float) $value;
22+
$precision = isset($params[0]) ? (int) $params[0] : null;
23+
24+
if ($precision === null) {
25+
return (float) $value;
26+
}
27+
28+
$mode = PHP_ROUND_HALF_UP; // Default mode
29+
30+
if (isset($params[1])) {
31+
$mode = match (strtolower($params[1])) {
32+
'up' => PHP_ROUND_HALF_UP,
33+
'down' => PHP_ROUND_HALF_DOWN,
34+
'even' => PHP_ROUND_HALF_EVEN,
35+
'odd' => PHP_ROUND_HALF_ODD,
36+
default => throw CastException::forInvalidFloatRoundingMode($params[1]),
37+
};
38+
}
39+
40+
return round((float) $value, $precision, $mode);
2141
}
2242
}

system/Language/en/Cast.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313

1414
// Cast language settings
1515
return [
16-
'baseCastMissing' => 'The "{0}" class must inherit the "CodeIgniter\Entity\Cast\BaseCast" class.',
17-
'enumInvalidCaseName' => 'Invalid case name "{0}" for enum "{1}".',
18-
'enumInvalidType' => 'Expected enum of type "{1}", but received "{0}".',
19-
'enumInvalidValue' => 'Invalid value "{1}" for enum "{0}".',
20-
'enumMissingClass' => 'Enum class must be specified for enum casting.',
21-
'enumNotEnum' => 'The "{0}" is not a valid enum class.',
22-
'invalidCastMethod' => 'The "{0}" is invalid cast method, valid methods are: ["get", "set"].',
23-
'invalidTimestamp' => 'Type casting "timestamp" expects a correct timestamp.',
24-
'jsonErrorCtrlChar' => 'Unexpected control character found.',
25-
'jsonErrorDepth' => 'Maximum stack depth exceeded.',
26-
'jsonErrorStateMismatch' => 'Underflow or the modes mismatch.',
27-
'jsonErrorSyntax' => 'Syntax error, malformed JSON.',
28-
'jsonErrorUnknown' => 'Unknown error.',
29-
'jsonErrorUtf8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.',
16+
'baseCastMissing' => 'The "{0}" class must inherit the "CodeIgniter\Entity\Cast\BaseCast" class.',
17+
'enumInvalidCaseName' => 'Invalid case name "{0}" for enum "{1}".',
18+
'enumInvalidType' => 'Expected enum of type "{1}", but received "{0}".',
19+
'enumInvalidValue' => 'Invalid value "{1}" for enum "{0}".',
20+
'enumMissingClass' => 'Enum class must be specified for enum casting.',
21+
'enumNotEnum' => 'The "{0}" is not a valid enum class.',
22+
'invalidCastMethod' => 'The "{0}" is invalid cast method, valid methods are: ["get", "set"].',
23+
'invalidTimestamp' => 'Type casting "timestamp" expects a correct timestamp.',
24+
'jsonErrorCtrlChar' => 'Unexpected control character found.',
25+
'jsonErrorDepth' => 'Maximum stack depth exceeded.',
26+
'jsonErrorStateMismatch' => 'Underflow or the modes mismatch.',
27+
'jsonErrorSyntax' => 'Syntax error, malformed JSON.',
28+
'jsonErrorUnknown' => 'Unknown error.',
29+
'jsonErrorUtf8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.',
30+
'floatInvalidRoundingMode' => 'Invalid rounding mode "{0}" for float casting.',
3031
];

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ Validation
245245

246246
- Custom rule methods that set an error via the ``&$error`` reference parameter now support the ``{field}``, ``{param}``, and ``{value}`` placeholders, consistent with language-file and ``setRule()``/``setRules()`` error messages.
247247

248+
Entities
249+
========
250+
251+
- **Float and Double Casting:** Added support for precision and rounding mode when casting to float or double in entities.
252+
248253
Others
249254
======
250255

user_guide_src/source/models/entities.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Add a question mark at the beginning of type to mark property as nullable, i.e.,
261261

262262
.. note:: **int-bool** can be used since v4.3.0.
263263
.. note:: **enum** can be used since v4.7.0.
264+
.. note:: Since v4.8.0, you can also pass parameters to **float** and **double** types to specify the number of decimal places and rounding mode, i.e., **float[2,even]**.
264265

265266
For example, if you had a User entity with an ``is_banned`` property, you can cast it as a boolean:
266267

0 commit comments

Comments
 (0)