Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/Formatter/CurrencyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CommerceGuys\Intl\Exception\UnknownCurrencyException;
use CommerceGuys\Intl\NumberFormat\NumberFormat;
use CommerceGuys\Intl\NumberFormat\NumberFormatRepositoryInterface;
use CommerceGuys\Intl\Calculator;

/**
* Formats currency amounts using locale-specific patterns.
Expand Down Expand Up @@ -97,6 +98,7 @@ public function format($number, $currencyCode, array $options = [])
throw new InvalidArgumentException($message);
}

$negative = (Calculator::compare('0', (string) $number, 12) == 1);
$this->validateOptions($options);
$options = array_replace($this->defaultOptions, $options);
$numberFormat = $this->getNumberFormat($options['locale']);
Expand All @@ -117,8 +119,9 @@ public function format($number, $currencyCode, array $options = [])
$number = str_replace('¤', $currency->getCurrencyCode(), $number);
} else {
// No symbol should be displayed. Remove leftover whitespace.
$number = str_replace('¤', '', $number);
$number = trim($number, " \xC2\xA0");
$negative_sign = $negative ? $numberFormat->getMinusSign() : '';
$pattern = '/^' . $negative_sign . '¤*\s*(.*?)\s*¤*$/us';
$number = preg_replace($pattern, $negative_sign . '$1', $number);
}

return $number;
Expand Down
12 changes: 12 additions & 0 deletions tests/Formatter/CurrencyFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CommerceGuys\Intl\Formatter\CurrencyFormatter;
use CommerceGuys\Intl\NumberFormat\NumberFormat;
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
use CommerceGuys\Intl\Tests\NumberFormat\CustomNumberFormatRepository;

/**
* @coversDefaultClass \CommerceGuys\Intl\Formatter\CurrencyFormatter
Expand Down Expand Up @@ -184,6 +185,17 @@ public function testParse($locale, $currencyCode, $number, $expectedNumber)
$this->assertSame($expectedNumber, $parsedNumber);
}

/**
* @covers ::format
*/
public function testFormatUtf8()
{
include __DIR__ . '/../NumberFormat/CustomNumberFormatRepository.php';
$numberFormatRepository = new CustomNumberFormatRepository();
$formatter = new CurrencyFormatter($numberFormatRepository, new CurrencyRepository());
$this->assertSame("\xc2\xb1950.00", $formatter->format('-950.000000', 'USD', ['currency_display' => 'none', 'locale' => 'tst']));
}

/**
* Provides the number format, currency format, number style, value and expected formatted value.
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/NumberFormat/CustomNumberFormatRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CommerceGuys\Intl\Tests\NumberFormat;

use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;

/**
* Provides a custom number format.
*/
class CustomNumberFormatRepository extends NumberFormatRepository
{
/**
* {@inheritdoc}
*/
protected function getDefinitions()
{
$return = parent::getDefinitions();
$return['tst'] = [
'minus_sign' => "\xc2\xb1",
];
return $return;
}
}