Skip to content

Commit

Permalink
Fix number trim in case of multibyte characters
Browse files Browse the repository at this point in the history
  • Loading branch information
czigor committed Nov 30, 2018
1 parent 6514870 commit db57e62
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 5 additions & 3 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 @@ -96,7 +97,7 @@ public function format($number, $currencyCode, array $options = [])
$message = sprintf('The provided value "%s" is not a valid number or numeric string.', $number);
throw new InvalidArgumentException($message);
}

$negative = (Calculator::compare('0', $number, 12) == 1);
$this->validateOptions($options);
$options = array_replace($this->defaultOptions, $options);
$numberFormat = $this->getNumberFormat($options['locale']);
Expand All @@ -117,8 +118,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;
}
}

0 comments on commit db57e62

Please sign in to comment.