Skip to content

Commit

Permalink
feat(Database): Add float cast that supports comma
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna authored and pionl committed Jun 2, 2023
1 parent 5d8120e commit 6d89f9a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=8.1",
"laravel/framework": "^9",
"laravel/framework": "^9.49.0",
"psr/log": "^2 | ^3"
},
"require-dev": {
Expand Down
23 changes: 23 additions & 0 deletions src/Core/Helpers/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Core\Helpers;

final class Value
{
public static function toFloat(string|int|float $value): ?float
{
if (is_string($value)) {
$value = strtr($value, [
',' => '.',
]);

if (is_numeric($value) === false) {
return null;
}
}

return (float) $value;
}
}
24 changes: 24 additions & 0 deletions src/Database/Models/Casts/FloatCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Database\Models\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use LaraStrict\Core\Helpers\Value;

/**
* @implements CastsAttributes<float, null|int|string|float>
*/
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);
}
}
9 changes: 4 additions & 5 deletions src/Validation/Rules/NumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LaraStrict\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use LaraStrict\Core\Helpers\Value;

class NumberRule implements Rule
{
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/Core/Helpers/ValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Unit\Core\Helpers;

use LaraStrict\Core\Helpers\Value;
use PHPUnit\Framework\TestCase;

final class ValueTest extends TestCase
{
/**
* @dataProvider provideValueToFloat
*/
public function testValueToFloat(string|float|int $actual, ?float $expected): void
{
$this->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],
];
}
}

0 comments on commit 6d89f9a

Please sign in to comment.