-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(cast): Add precision and rounding mode in FloatCast #10086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,30 @@ | |
|
|
||
| namespace CodeIgniter\Entity\Cast; | ||
|
|
||
| use CodeIgniter\DataCaster\Exceptions\CastException; | ||
|
|
||
| class FloatCast extends BaseCast | ||
| { | ||
| public static function get($value, array $params = []): float | ||
| { | ||
| return (float) $value; | ||
| $precision = isset($params[0]) ? (int) $params[0] : null; | ||
|
|
||
| if ($precision === null) { | ||
| return (float) $value; | ||
| } | ||
|
|
||
| $mode = PHP_ROUND_HALF_UP; // Default mode | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| return new static(lang('Cast.floatInvalidRoundingMode', [$mode])); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,18 +13,19 @@ | |||||
|
|
||||||
| // Cast language settings | ||||||
| return [ | ||||||
| 'baseCastMissing' => 'The "{0}" class must inherit the "CodeIgniter\Entity\Cast\BaseCast" class.', | ||||||
| 'enumInvalidCaseName' => 'Invalid case name "{0}" for enum "{1}".', | ||||||
| 'enumInvalidType' => 'Expected enum of type "{1}", but received "{0}".', | ||||||
| 'enumInvalidValue' => 'Invalid value "{1}" for enum "{0}".', | ||||||
| 'enumMissingClass' => 'Enum class must be specified for enum casting.', | ||||||
| 'enumNotEnum' => 'The "{0}" is not a valid enum class.', | ||||||
| 'invalidCastMethod' => 'The "{0}" is invalid cast method, valid methods are: ["get", "set"].', | ||||||
| 'invalidTimestamp' => 'Type casting "timestamp" expects a correct timestamp.', | ||||||
| 'jsonErrorCtrlChar' => 'Unexpected control character found.', | ||||||
| 'jsonErrorDepth' => 'Maximum stack depth exceeded.', | ||||||
| 'jsonErrorStateMismatch' => 'Underflow or the modes mismatch.', | ||||||
| 'jsonErrorSyntax' => 'Syntax error, malformed JSON.', | ||||||
| 'jsonErrorUnknown' => 'Unknown error.', | ||||||
| 'jsonErrorUtf8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', | ||||||
| 'baseCastMissing' => 'The "{0}" class must inherit the "CodeIgniter\Entity\Cast\BaseCast" class.', | ||||||
| 'enumInvalidCaseName' => 'Invalid case name "{0}" for enum "{1}".', | ||||||
| 'enumInvalidType' => 'Expected enum of type "{1}", but received "{0}".', | ||||||
| 'enumInvalidValue' => 'Invalid value "{1}" for enum "{0}".', | ||||||
| 'enumMissingClass' => 'Enum class must be specified for enum casting.', | ||||||
| 'enumNotEnum' => 'The "{0}" is not a valid enum class.', | ||||||
| 'invalidCastMethod' => 'The "{0}" is invalid cast method, valid methods are: ["get", "set"].', | ||||||
| 'invalidTimestamp' => 'Type casting "timestamp" expects a correct timestamp.', | ||||||
| 'jsonErrorCtrlChar' => 'Unexpected control character found.', | ||||||
| 'jsonErrorDepth' => 'Maximum stack depth exceeded.', | ||||||
| 'jsonErrorStateMismatch' => 'Underflow or the modes mismatch.', | ||||||
| 'jsonErrorSyntax' => 'Syntax error, malformed JSON.', | ||||||
| 'jsonErrorUnknown' => 'Unknown error.', | ||||||
| 'jsonErrorUtf8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', | ||||||
| 'floatInvalidRoundingMode' => 'Invalid rounding mode "{0}" for float casting.', | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this addition to changelog. |
||||||
| ]; | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -245,6 +245,11 @@ Validation | |||||||||||
|
|
||||||||||||
| - 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. | ||||||||||||
|
|
||||||||||||
| Entities | ||||||||||||
| ======== | ||||||||||||
|
|
||||||||||||
| - **Float and Double Casting:** Added support for precision and rounding mode when casting to float or double in entities. | ||||||||||||
|
|
||||||||||||
|
Comment on lines
+248
to
+252
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
And maybe we can put it under the "Others" section. |
||||||||||||
| Others | ||||||||||||
| ====== | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be simplified like this (just fix the indent):