Skip to content

Commit 9d2fdc2

Browse files
committed
add FormatterServiceInterface
1 parent 873bbc2 commit 9d2fdc2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Services/FormatterService.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
use Brick\Math\BigDecimal;
8+
use Brick\Math\RoundingMode;
9+
10+
final readonly class FormatterService implements FormatterServiceInterface
11+
{
12+
public function intValue(string|int|float $amount, int $decimalPlaces): string
13+
{
14+
return (string) BigDecimal::ten()
15+
->power($decimalPlaces)
16+
->multipliedBy(BigDecimal::of($amount))
17+
->toScale(0, RoundingMode::DOWN);
18+
}
19+
20+
public function floatValue(string|int|float $amount, int $decimalPlaces): string
21+
{
22+
return (string) BigDecimal::ofUnscaledValue($amount, $decimalPlaces);
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
/**
8+
* @internal
9+
*/
10+
interface FormatterServiceInterface
11+
{
12+
public function intValue(string|int|float $amount, int $decimalPlaces): string;
13+
14+
public function floatValue(string|int|float $amount, int $decimalPlaces): string;
15+
}

tests/Units/Service/FormatterTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Units\Service;
6+
7+
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
8+
use Bavix\Wallet\Services\FormatterServiceInterface;
9+
use Bavix\Wallet\Test\Infra\TestCase;
10+
11+
/**
12+
* @internal
13+
*/
14+
final class FormatterTest extends TestCase
15+
{
16+
/**
17+
* @throws ExceptionInterface
18+
*/
19+
public function testFloatValueDP3(): void
20+
{
21+
$result = app(FormatterServiceInterface::class)->floatValue('12345', 3);
22+
23+
self::assertSame('12.345', $result);
24+
}
25+
26+
/**
27+
* @throws ExceptionInterface
28+
*/
29+
public function testFloatValueDP2(): void
30+
{
31+
$result = app(FormatterServiceInterface::class)->floatValue('12345', 2);
32+
33+
self::assertSame('123.45', $result);
34+
}
35+
36+
/**
37+
* @throws ExceptionInterface
38+
*/
39+
public function testIntValueDP3(): void
40+
{
41+
$result = app(FormatterServiceInterface::class)->intValue('12.345', 3);
42+
43+
self::assertSame('12345', $result);
44+
}
45+
46+
/**
47+
* @throws ExceptionInterface
48+
*/
49+
public function testIntValueDP2(): void
50+
{
51+
$result = app(FormatterServiceInterface::class)->intValue('123.45', 2);
52+
53+
self::assertSame('12345', $result);
54+
}
55+
}

0 commit comments

Comments
 (0)