From 6d89f9ae280f73478ec36e4502b1309c9c6c09b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Fri, 2 Jun 2023 12:09:00 +0200 Subject: [PATCH] feat(Database): Add float cast that supports comma --- composer.json | 2 +- src/Core/Helpers/Value.php | 23 ++++++++++++ src/Database/Models/Casts/FloatCast.php | 24 +++++++++++++ src/Validation/Rules/NumberRule.php | 9 +++-- tests/Unit/Core/Helpers/ValueTest.php | 48 +++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 src/Core/Helpers/Value.php create mode 100644 src/Database/Models/Casts/FloatCast.php create mode 100644 tests/Unit/Core/Helpers/ValueTest.php diff --git a/composer.json b/composer.json index 7e8b97d2..bb31395c 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=8.1", - "laravel/framework": "^9", + "laravel/framework": "^9.49.0", "psr/log": "^2 | ^3" }, "require-dev": { diff --git a/src/Core/Helpers/Value.php b/src/Core/Helpers/Value.php new file mode 100644 index 00000000..e7623062 --- /dev/null +++ b/src/Core/Helpers/Value.php @@ -0,0 +1,23 @@ + '.', + ]); + + if (is_numeric($value) === false) { + return null; + } + } + + return (float) $value; + } +} diff --git a/src/Database/Models/Casts/FloatCast.php b/src/Database/Models/Casts/FloatCast.php new file mode 100644 index 00000000..1805fcc3 --- /dev/null +++ b/src/Database/Models/Casts/FloatCast.php @@ -0,0 +1,24 @@ + + */ +final class FloatCast implements CastsAttributes +{ + public function get($model, string $key, $value, array $attributes) + { + return (float) $value; + } + + public function set($model, string $key, $value, array $attributes) + { + return Value::toFloat((string) $value); + } +} diff --git a/src/Validation/Rules/NumberRule.php b/src/Validation/Rules/NumberRule.php index d53986e7..d6644508 100644 --- a/src/Validation/Rules/NumberRule.php +++ b/src/Validation/Rules/NumberRule.php @@ -5,6 +5,7 @@ namespace LaraStrict\Validation\Rules; use Illuminate\Contracts\Validation\Rule; +use LaraStrict\Core\Helpers\Value; class NumberRule implements Rule { @@ -15,15 +16,13 @@ public function passes($attribute, $value): bool return $intVal !== PHP_INT_MAX && $intVal !== PHP_INT_MIN; } - $value = strtr((string) $value, [ - ',' => '.', - ]); + $value = Value::toFloat((string) $value); - if (is_numeric($value) === false) { + if ($value === null) { return false; } - return str_contains((string) (float) $value, 'E+') === false; + return str_contains((string) $value, 'E+') === false; } public function message(): string diff --git a/tests/Unit/Core/Helpers/ValueTest.php b/tests/Unit/Core/Helpers/ValueTest.php new file mode 100644 index 00000000..7adba517 --- /dev/null +++ b/tests/Unit/Core/Helpers/ValueTest.php @@ -0,0 +1,48 @@ +assertSame($expected, Value::toFloat($actual)); + } + + public function provideValueToFloat(): array + { + return [ + ['', null], + ['.', null], + [',', null], + ['a', null], + ['0', 0.0], + ['-.0', 0.0], + ['-0.', 0.0], + ['-0', 0.0], + ['+0', 0.0], + ['0.0', 0.0], + ['0.5', 0.5], + ['0,5', 0.5], + ['-0,5', -0.5], + ['-,5', -0.5], + ['+0,5', 0.5], + ['+,5', 0.5], + [0, 0.0], + [0.0, 0.0], + [1.5, 1.5], + ['9223372036854775807', 9.223372036854776E+18], + ['9223372036854775807.5', 9.223372036854776E+18], + ['922337203685477580703434355.5', 9.223372036854776E+26], + ['-922337203685477580703434355.5', -9.223372036854776E+26], + ]; + } +}