From 9c592c43022266be1cb104c47b60ed165da1676c Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Tue, 27 Feb 2024 15:46:24 +0100 Subject: [PATCH] Add `MathUtils::convertCentsToEuro` : Convert cents to euro --- CHANGELOG.md | 6 ++++++ src/Query/MySQL/GroupConcat.php | 2 +- src/Utils/MathUtils.php | 8 ++++++++ tests/Utils/MathUtilsTest.php | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d051d01..330eb9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ CHANGELOG for 1.x =================== +## v1.0.4 - (2024-02-27) + +### Added + +- `MathUtils::convertCentsToEuro` : Convert cents to euro + ## v1.0.3 - (2024-01-11) ### Fix diff --git a/src/Query/MySQL/GroupConcat.php b/src/Query/MySQL/GroupConcat.php index 64f83d5..2684854 100644 --- a/src/Query/MySQL/GroupConcat.php +++ b/src/Query/MySQL/GroupConcat.php @@ -54,7 +54,7 @@ public function parse(\Doctrine\ORM\Query\Parser $parser): void } if ($lexer->isNextToken(Lexer::T_IDENTIFIER)) { - if (strtolower($lexer->lookahead['value']) !== 'separator') { + if (strtolower($lexer->lookahead['value']) !== 'separator') { // @phpstan-ignore-line $parser->syntaxError('separator'); } $parser->match(Lexer::T_IDENTIFIER); diff --git a/src/Utils/MathUtils.php b/src/Utils/MathUtils.php index ef6edfe..44ad42a 100644 --- a/src/Utils/MathUtils.php +++ b/src/Utils/MathUtils.php @@ -30,4 +30,12 @@ public static function formatBytes(float $size, int $precision = 2): string $floor = floor($base); return round(pow(1000, $base - $floor), $precision) . ' ' . $suffixes[$floor]; } + + public static function convertCentsToEuro(?float $price): float + { + if (null === $price) { + return 0.0; + } + return $price / 100; + } } diff --git a/tests/Utils/MathUtilsTest.php b/tests/Utils/MathUtilsTest.php index a73c067..2dfb793 100644 --- a/tests/Utils/MathUtilsTest.php +++ b/tests/Utils/MathUtilsTest.php @@ -2,6 +2,7 @@ namespace Smart\CoreBundle\Tests\Utils; +use App\Utils\NumberUtils; use Smart\CoreBundle\Utils\MathUtils; use PHPUnit\Framework\TestCase; @@ -68,4 +69,20 @@ public function formatBytesProvider(): array '420.96 TB' => ['420.96 TB', 420956000000000, 2], ]; } + + /** @dataProvider convertCentsToEuroProvider */ + public function testConvertCentsToEuro(float $expected, float $price): void + { + $this->assertSame($expected, MathUtils::convertCentsToEuro($price)); + } + + public function convertCentsToEuroProvider(): array + { + return [ + 'simple' => [5, 500], + 'float' => [5.54, 554], + 'little' => [0.01, 1], + 'price_float' => [5.893121, 589.3121], + ]; + } }