1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Whallysson\Money\Money; |
||
6 | |||
7 | use Whallysson\Money\Currency\CurrencyInterface; |
||
8 | use Whallysson\Money\Formatter\FormatterInterface; |
||
9 | |||
10 | /** |
||
11 | * Class MoneyFormatter |
||
12 | * |
||
13 | * @author Whallysson Avelino <[email protected]> |
||
14 | * @package Whallysson\Money\Money |
||
15 | */ |
||
16 | class MoneyFormatter implements FormatterInterface |
||
17 | { |
||
18 | /** |
||
19 | * @param string $value |
||
20 | * @param CurrencyInterface $currency |
||
21 | * @param bool $showSymbol |
||
22 | * @param bool $showThousandsSeparator |
||
23 | * @return string |
||
24 | */ |
||
25 | public function format( |
||
26 | string $value, |
||
27 | CurrencyInterface $currency, |
||
28 | bool $showSymbol = true, |
||
29 | bool $showThousandsSeparator = true |
||
30 | ): string { |
||
31 | $formattedNumber = number_format( |
||
32 | (float)$value, |
||
33 | 2, |
||
34 | $currency->getDecimalSeparator(), |
||
35 | $showThousandsSeparator ? $currency->getThousandsSeparator() : '' |
||
36 | ); |
||
37 | |||
38 | if (!$showSymbol) { |
||
39 | return $formattedNumber; |
||
40 | } |
||
41 | |||
42 | return match($currency->getSymbolPosition(), [ |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
43 | 'before' => sprintf('%s %s', $currency->getSymbol(), $formattedNumber), |
||
44 | 'after' => sprintf('%s %s', $formattedNumber, $currency->getSymbol()) |
||
45 | ], sprintf('%s %s', $currency->getSymbol(), $formattedNumber)); |
||
46 | } |
||
47 | } |
||
48 |