Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,13 @@ public function convertedTo(
* Note that NumberFormatter internally represents values using floating point arithmetic,
* so discrepancies can appear when formatting very large monetary values.
*
* @deprecated Use MoneyNumberFormatter::format($money).
*
* @param NumberFormatter $formatter The formatter to format with.
*/
public function formatWith(NumberFormatter $formatter): string
{
return $formatter->formatCurrency(
$this->amount->toFloat(),
$this->currency->getCurrencyCode(),
);
return (new MoneyNumberFormatter($formatter))->format($this);
}

/**
Expand All @@ -696,43 +695,28 @@ public function formatWith(NumberFormatter $formatter): string
* Note that this method uses NumberFormatter, which internally represents values using floating point arithmetic,
* so discrepancies can appear when formatting very large monetary values.
*
* @deprecated Use Money::format($locale, $allowWholeNumber).
*
* @param string $locale The locale to format to.
* @param bool $allowWholeNumber Whether to allow formatting as a whole number if the amount has no fraction.
*/
public function formatTo(string $locale, bool $allowWholeNumber = false): string
{
/** @var NumberFormatter|null $lastFormatter */
static $lastFormatter = null;
static $lastFormatterLocale;
static $lastFormatterScale;

if ($allowWholeNumber && ! $this->amount->hasNonZeroFractionalPart()) {
$scale = 0;
} else {
$scale = $this->amount->getScale();
}

if ($lastFormatter !== null && $lastFormatterLocale === $locale) {
$formatter = $lastFormatter;

if ($lastFormatterScale !== $scale) {
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $scale);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $scale);

$lastFormatterScale = $scale;
}
} else {
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $scale);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $scale);

$lastFormatter = $formatter;
$lastFormatterLocale = $locale;
$lastFormatterScale = $scale;
}
return $this->formatToLocale($locale, $allowWholeNumber);
}

return $this->formatWith($formatter);
/**
* Formats this Money to the given locale.
*
* Note that this method uses MoneyLocaleFormatter, which in turns internally uses NumberFormatter, which represents values using floating
* point arithmetic, so discrepancies can appear when formatting very large monetary values.
*
* @param string $locale The locale to format to.
* @param bool $allowWholeNumber Whether to allow formatting as a whole number if the amount has no fraction.
*/
public function formatToLocale(string $locale, bool $allowWholeNumber = false): string
{
return (new MoneyLocaleFormatter($locale, $allowWholeNumber))->format($this);
}

public function toRational(): RationalMoney
Expand Down
10 changes: 10 additions & 0 deletions src/MoneyFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Brick\Money;

interface MoneyFormatter
{
public function format(Money $money): string;
}
45 changes: 45 additions & 0 deletions src/MoneyLocaleFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Brick\Money;

use NumberFormatter;
use Override;

final class MoneyLocaleFormatter implements MoneyFormatter
{
protected readonly string $locale;

protected readonly bool $allowWholeNumber;

protected readonly NumberFormatter $numberFormatter;

public function __construct(string $locale, bool $allowWholeNumber)
{
$this->locale = $locale;
$this->allowWholeNumber = $allowWholeNumber;
$this->numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
}

#[Override]
public function format(Money $money): string
{
if ($this->allowWholeNumber && ! $money->getAmount()->hasNonZeroFractionalPart()) {
$scale = 0;
} else {
$scale = $money->getAmount()->getScale();
}

/**
* Adjust scale used by the number formatter in $this->moneyFormatter.
*/
$this->numberFormatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $scale);
$this->numberFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $scale);

return $this->numberFormatter->formatCurrency(
$money->getAmount()->toFloat(),
$money->getCurrency()->getCurrencyCode(),
);
}
}
30 changes: 30 additions & 0 deletions src/MoneyNumberFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Brick\Money;

use NumberFormatter;
use Override;

/**
* Basic convenience wrapper of \NumberFormatter.
*/
final class MoneyNumberFormatter implements MoneyFormatter
{
protected NumberFormatter $numberFormatter;

public function __construct(NumberFormatter $numberFormatter)
{
$this->numberFormatter = $numberFormatter;
}

#[Override]
public function format(Money $money): string
{
return $this->numberFormatter->formatCurrency(
$money->getAmount()->toFloat(),
$money->getCurrency()->getCurrencyCode(),
);
}
}
46 changes: 46 additions & 0 deletions tests/MoneyLocaleFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Brick\Money\Tests;

use Brick\Money\Context\CustomContext;
use Brick\Money\Money;
use Brick\Money\MoneyLocaleFormatter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;

class MoneyLocaleFormatterTest extends AbstractTestCase
{
/**
* @param array $money The money to test.
* @param string $locale The target locale.
* @param bool $allowWholeNumber Whether to allow formatting as a whole number if the amount has no fraction.
* @param string $expected The expected output.
*/
#[RequiresPhpExtension('intl')]
#[DataProvider('providerFormat')]
public function testFormatTo(array $money, string $locale, bool $allowWholeNumber, string $expected): void
{
$formatter = new MoneyLocaleFormatter($locale, $allowWholeNumber);
self::assertSame($expected, $formatter->format(Money::of(...$money)));
}

public static function providerFormat(): array
{
return [
[['1.23', 'USD'], 'en_US', false, '$1.23'],
[['1.23', 'USD'], 'fr_FR', false, '1,23 $US'],
[['1.23', 'EUR'], 'fr_FR', false, '1,23 €'],
[['1.234', 'EUR', new CustomContext(3)], 'fr_FR', false, '1,234 €'],
[['234.0', 'EUR', new CustomContext(1)], 'fr_FR', false, '234,0 €'],
[['234.0', 'EUR', new CustomContext(1)], 'fr_FR', true, '234 €'],
[['234.00', 'GBP'], 'en_GB', false, '£234.00'],
[['234.00', 'GBP'], 'en_GB', true, '£234'],
[['234.000', 'EUR', new CustomContext(3)], 'fr_FR', false, '234,000 €'],
[['234.000', 'EUR', new CustomContext(3)], 'fr_FR', true, '234 €'],
[['234.001', 'GBP', new CustomContext(3)], 'en_GB', false, '£234.001'],
[['234.001', 'GBP', new CustomContext(3)], 'en_GB', true, '£234.001'],
];
}
}
41 changes: 41 additions & 0 deletions tests/MoneyNumberFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Brick\Money\Tests;

use Brick\Money\Context\AutoContext;
use Brick\Money\Money;
use Brick\Money\MoneyNumberFormatter;
use NumberFormatter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;

class MoneyNumberFormatterTest extends AbstractTestCase
{
/**
* @param array $money The money to test.
* @param string $locale The target locale.
* @param string $symbol A decimal symbol to apply to the NumberFormatter.
* @param string $expected The expected output.
*/
#[RequiresPhpExtension('intl')]
#[DataProvider('providerFormat')]
public function testFormat(array $money, string $locale, string $symbol, string $expected): void
{
$numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$numberFormatter->setSymbol(NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $symbol);
$formatter = new MoneyNumberFormatter($numberFormatter);

$actual = $formatter->format(Money::of(...$money));
self::assertSame($expected, $actual);
}

public static function providerFormat(): array
{
return [
[['1.23', 'USD'], 'en_US', ';', '$1;23'],
[['1.7', 'EUR', new AutoContext()], 'fr_FR', '~', '1~70 €'],
];
}
}
13 changes: 13 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,19 @@ public static function providerFormatTo(): array
];
}

/**
* @param array $money The money to test.
* @param string $locale The target locale.
* @param bool $allowWholeNumber Whether to allow formatting as a whole number if the amount has no fraction.
* @param string $expected The expected output.
*/
#[RequiresPhpExtension('intl')]
#[DataProvider('providerFormatTo')]
public function testFormatToLocale(array $money, string $locale, bool $allowWholeNumber, string $expected): void
{
self::assertSame($expected, Money::of(...$money)->formatToLocale($locale, $allowWholeNumber));
}

public function testToRational(): void
{
$money = Money::of('12.3456', 'EUR', new AutoContext());
Expand Down