Skip to content

Commit

Permalink
Merge branch 'fix/to-cents'
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Jan 31, 2024
2 parents a06dd6c + 875a193 commit 7aad2e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/MoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ class MoneyFormatter
{
public static function format($value, $currency, $locale, $monetarySeparator = null): string
{
$currencies = new ISOCurrencies();
if (is_null($value) || $value === '') {
return '';
}

$numberFormatter = self::getNumberFormatter($locale, NumberFormatter::CURRENCY);

$moneyFormatter = new IntlMoneyFormatter($numberFormatter, $currencies);
$moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());

$money = new Money($value, $currency);
return $moneyFormatter->format($money);
}

public static function parseDecimal($moneyString, $currency, $locale): string
{
if (is_null($moneyString) || $moneyString === '') {
return '';
}

$currencies = new ISOCurrencies();
$numberFormatter = self::getNumberFormatter($locale, NumberFormatter::DECIMAL);
$moneyParser = new IntlLocalizedDecimalParser($numberFormatter, $currencies);
Expand Down
56 changes: 44 additions & 12 deletions tests/MoneyFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ public static function provideMoneyDataSEK(): array
{
return [
'thousands' => [
'10 000,00 kr',
1000000,
'10 000,00 kr',
],
'decimals' => [
'100,45 kr',
10045,
'100,45 kr',
],
'millions' => [
'1 234 567,89 kr',
123456789,
'1 234 567,89 kr',
],
'empty_string' => [
'',
'',
],
'null' => [
null,
'',
],
];
}
Expand All @@ -29,16 +37,24 @@ public static function provideMoneyDataUSD(): array
{
return [
'thousands' => [
'$10,000.00',
1000000,
'$10,000.00',
],
'decimals' => [
'$100.45',
10045,
'$100.45',
],
'millions' => [
'$1,234,567.89',
123456789,
'$1,234,567.89',
],
'empty_string' => [
'',
'',
],
'null' => [
null,
'',
],
];
}
Expand All @@ -58,6 +74,14 @@ public static function provideDecimalDataSEK(): array
'1 234 567,89',
'123456789',
],
'empty_string' => [
'',
'',
],
'null' => [
null,
'',
],
];
}

Expand All @@ -76,6 +100,14 @@ public static function provideDecimalDataUSD(): array
'1,234,567.89',
'123456789',
],
'empty_string' => [
'',
'',
],
'null' => [
null,
'',
],
];
}

Expand All @@ -85,7 +117,7 @@ public static function provideDecimalDataUSD(): array
* @dataProvider provideMoneyDataSEK
*/
#[Framework\CoversClass(MoneyFormatter::class)]
public function testMoneyFormatterSEK( string $expectedOutput, mixed $input)
public function testMoneyFormatterSEK(mixed $input, string $expectedOutput)
{
self::assertSame(
static::replaceNonBreakingSpaces($expectedOutput),
Expand All @@ -98,7 +130,7 @@ public function testMoneyFormatterSEK( string $expectedOutput, mixed $input)
* @dataProvider provideMoneyDataUSD
*/
#[Framework\CoversClass(MoneyFormatter::class)]
public function testMoneyFormatterUSD( string $expectedOutput, mixed $input)
public function testMoneyFormatterUSD(mixed $input, string $expectedOutput)
{
self::assertSame(
static::replaceNonBreakingSpaces($expectedOutput),
Expand All @@ -107,11 +139,11 @@ public function testMoneyFormatterUSD( string $expectedOutput, mixed $input)
}

/**
* @covers MoneyFormatter::parse
* @covers MoneyFormatter::parseDecimal
* @dataProvider provideDecimalDataSEK
*/
#[Framework\CoversClass(MoneyFormatter::class)]
public function testMoneyParserDecimalSEK( string $input, mixed $expectedOutput)
public function testMoneyParserDecimalSEK(mixed $input, string $expectedOutput)
{
self::assertSame(
$expectedOutput,
Expand All @@ -120,11 +152,11 @@ public function testMoneyParserDecimalSEK( string $input, mixed $expectedOutput)
}

/**
* @covers MoneyFormatter::parse
* @covers MoneyFormatter::parseDecimal
* @dataProvider provideDecimalDataUSD
*/
#[Framework\CoversClass(MoneyFormatter::class)]
public function testMoneyParserDecimalUSD( string $input, mixed $expectedOutput)
public function testMoneyParserDecimalUSD(mixed $input, string $expectedOutput)
{
self::assertSame(
$expectedOutput,
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static function getProperty($object, $property)
return $reflection->getValue($object);
}

/**
* Replaces all non-breaking spaces in the given string with the Unicode character for non-breaking space.
*/
public static function replaceNonBreakingSpaces(string $string): string
{
return preg_replace('/\s/', "\xc2\xa0", $string);
Expand Down

0 comments on commit 7aad2e3

Please sign in to comment.