Skip to content

Commit cae1e22

Browse files
committed
Apply suggested changes
1 parent 31d2b6f commit cae1e22

File tree

8 files changed

+82
-32
lines changed

8 files changed

+82
-32
lines changed

system/DataCaster/Cast/FloatCast.php

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

41-
$mode = PHP_ROUND_HALF_UP; // Default mode
42-
43-
if (isset($params[1])) {
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-
};
51-
}
41+
$mode = match (strtolower($params[1] ?? 'up')) {
42+
'up' => PHP_ROUND_HALF_UP,
43+
'down' => PHP_ROUND_HALF_DOWN,
44+
'even' => PHP_ROUND_HALF_EVEN,
45+
'odd' => PHP_ROUND_HALF_ODD,
46+
default => throw CastException::forInvalidFloatRoundingMode($params[1]),
47+
};
5248

5349
return round((float) $value, $precision, $mode);
5450
}

system/Entity/Cast/FloatCast.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ public static function get($value, array $params = []): float
2525
return (float) $value;
2626
}
2727

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-
}
28+
$mode = match (strtolower($params[1] ?? 'up')) {
29+
'up' => PHP_ROUND_HALF_UP,
30+
'down' => PHP_ROUND_HALF_DOWN,
31+
'even' => PHP_ROUND_HALF_EVEN,
32+
'odd' => PHP_ROUND_HALF_ODD,
33+
default => throw CastException::forInvalidFloatRoundingMode($params[1]),
34+
};
3935

4036
return round((float) $value, $precision, $mode);
4137
}

system/Entity/Exceptions/CastException.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ public static function forInvalidEnumType(string $expectedClass, string $actualC
125125

126126
/**
127127
* Thrown when an invalid rounding mode is provided for float casting.
128-
*
129-
* @return static
130128
*/
131-
public static function forInvalidFloatRoundingMode(string $mode)
129+
public static function forInvalidFloatRoundingMode(string $mode): static
132130
{
133-
return new static(lang('Cast.floatInvalidRoundingMode', [$mode]));
131+
return new static(lang('Cast.invalidFloatRoundingMode', [$mode]));
134132
}
135133
}

system/Language/en/Cast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
'jsonErrorSyntax' => 'Syntax error, malformed JSON.',
2828
'jsonErrorUnknown' => 'Unknown error.',
2929
'jsonErrorUtf8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.',
30-
'floatInvalidRoundingMode' => 'Invalid rounding mode "{0}" for float casting.',
30+
'invalidFloatRoundingMode' => 'Invalid rounding mode "{0}" for float casting.',
3131
];

tests/system/Entity/EntityTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,36 @@ public function testCastFloat(): void
419419
$this->assertEqualsWithDelta(3.6, $entity->second, PHP_FLOAT_EPSILON);
420420
}
421421

422+
public function testCastFloatWithPrecision(): void
423+
{
424+
$entity = $this->getCastEntity();
425+
426+
$entity->fourteenth = 3.1415926535;
427+
428+
$this->assertIsFloat($entity->fourteenth);
429+
$this->assertEqualsWithDelta(3.14, $entity->fourteenth, PHP_FLOAT_EPSILON);
430+
431+
$entity->fourteenth = '3.1415926535';
432+
433+
$this->assertIsFloat($entity->fourteenth);
434+
$this->assertEqualsWithDelta(3.14, $entity->fourteenth, PHP_FLOAT_EPSILON);
435+
}
436+
437+
public function testCastFloatWithPrecisionAndRoundingMode(): void
438+
{
439+
$entity = $this->getCastEntity();
440+
441+
$entity->fifteenth = 3.145;
442+
443+
$this->assertIsFloat($entity->fifteenth);
444+
$this->assertEqualsWithDelta(3.14, $entity->fifteenth, PHP_FLOAT_EPSILON);
445+
446+
$entity->fifteenth = '3.135';
447+
448+
$this->assertIsFloat($entity->fifteenth);
449+
$this->assertEqualsWithDelta(3.13, $entity->fifteenth, PHP_FLOAT_EPSILON);
450+
}
451+
422452
public function testCastDouble(): void
423453
{
424454
$entity = $this->getCastEntity();
@@ -1750,6 +1780,8 @@ private function getCastEntity($data = null): object
17501780
'eleventh' => 'json-array',
17511781
'twelfth' => 'csv',
17521782
'thirteenth' => 'uri',
1783+
'fourteenth' => 'float[2]',
1784+
'fifteenth' => 'float[2,down]',
17531785
];
17541786

17551787
public function setSeventh(string $seventh): void

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,12 @@ 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-
253248
Others
254249
======
255250

251+
- **Float and Double Casting:** Added support for precision and rounding mode when casting to float or double in entities.
252+
- Float and Double casting now throws ``CastException::forInvalidFloatRoundingMode()`` if an rounding mode other than up, down, even or odd is provided.
253+
256254
***************
257255
Message Changes
258256
***************

user_guide_src/source/models/model.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,20 @@ of type to mark the field as nullable, i.e., ``?int``, ``?datetime``.
380380
|``enum`` | Enum | string/int type |
381381
+---------------+----------------+---------------------------+
382382

383+
float
384+
-----
385+
386+
Casting as ``float`` will convert the value to a float type in PHP.
387+
This is best used with database columns that are of a float or numeric type.
388+
389+
You can also pass arguments to the ``float`` type to specify the number
390+
of decimal places to round to as well as the rounding mode (up, down, even or odd).
391+
392+
.. literalinclude:: model/067.php
393+
394+
.. note:: Prior to v4.8.0 the ``float`` type did not support any parameters.
395+
It simply converted the value to a float type in PHP without rounding.
396+
383397
csv
384398
---
385399

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
7+
class TransactionModel extends Model
8+
{
9+
// ...
10+
protected array $casts = [
11+
'id' => 'int',
12+
'currency' => 'string',
13+
'amount' => 'float[2,even]',
14+
];
15+
// ...
16+
}

0 commit comments

Comments
 (0)