-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Database): Add float cast that supports comma
- Loading branch information
Showing
5 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} |