Skip to content

Commit

Permalink
Some work towards getting Number::currency fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 1, 2014
1 parent 6b5f2c9 commit 32ed363
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
47 changes: 35 additions & 12 deletions src/Utility/Number.php
Expand Up @@ -84,18 +84,18 @@ class Number {
protected static $_formatters = [];

/**
* Default currency used by Number::currency()
* A list of currency formatters indexed by locale
*
* @var string
* @var array
*/
protected static $_defaultCurrency = 'USD';
protected static $_currencyFormatters = [];

/**
* If native number_format() should be used. If >= PHP5.4
* Default currency used by Number::currency()
*
* @var bool
* @var string
*/
protected static $_numberFormatSupport = null;
protected static $_defaultCurrency = 'USD';

/**
* Formats a number with a level of precision.
Expand Down Expand Up @@ -309,17 +309,40 @@ public static function formatDelta($value, array $options = array()) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
*/
public static function currency($value, $currency = null, array $options = array()) {
$default = static::$_currencyDefaults;
$value = (float)$value;

if (isset($options['zero']) && !$value) {
return $options['zero'];
}

$locale = isset($options['locale']) ? $options['locale'] : ini_get('intl.default_locale');

if (!$locale) {
$locale = 'en_US';
}

if (!isset(static::$_currencyFormatters[$locale])) {
static::$_currencyFormatters[$locale] = new NumberFormatter(
$locale,
NumberFormatter::CURRENCY
);
}

if ($currency === null) {
$currency = static::defaultCurrency();
}

if (isset(static::$_currencies[$currency])) {
$default = static::$_currencies[$currency];
} elseif (is_string($currency)) {
$options['before'] = $currency;
$formatter = static::$_currencyFormatters[$locale];

if (!empty($options['pattern'])) {
$formatter->setPattern($options['pattern']);
}

return $formatter->formatCurrency($value, $currency);

$default = static::$_currencyDefaults;


$options += $default;

if (isset($options['before']) && $options['before'] !== '') {
Expand All @@ -332,7 +355,7 @@ public static function currency($value, $currency = null, array $options = array
$result = $options['before'] = $options['after'] = null;

$symbolKey = 'whole';
$value = (float)$value;
;
if (!$value) {
if ($options['zero'] !== 0) {
return $options['zero'];
Expand Down
38 changes: 27 additions & 11 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -145,30 +145,46 @@ public function testCurrency() {
$expected = '$100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, '#');
$expected = '#100,100,100.00';
$result = $this->Number->currency($value, 'USD');
$expected = '$100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, false);
$expected = '100,100,100.00';
$result = $this->Number->currency($value, 'EUR');
$expected = '100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, 'USD');
$result = $this->Number->currency($value, 'EUR', ['locale' => 'de_DE']);
$expected = '100.100.100,00 €';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, 'USD', ['locale' => 'de_DE']);
$expected = '100.100.100,00 $';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, 'USD', ['locale' => 'en_US']);
$expected = '$100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, 'EUR');
$expected = '100.100.100,00';
$result = $this->Number->currency($value, 'USD', ['locale' => 'en_CA']);
$expected = 'US$100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->currency($value, 'GBP');
$expected = '£100,100,100.00';
$this->assertEquals($expected, $result);

$options = array('thousands' => ' ', 'wholeSymbol' => 'EUR ', 'wholePosition' => 'before',
'decimals' => ',', 'zero' => 'Gratuit');
$result = $this->Number->currency($value, '', $options);
$expected = 'EUR 100 100 100,00';
$result = $this->Number->currency($value, 'GBP', ['locale' => 'da_DK']);
$expected = '100.100.100,00 £';
$this->assertEquals($expected, $result);

$options = ['locale' => 'fr_FR', 'pattern' => 'EUR #,###.00'];
$result = $this->Number->currency($value, 'EUR', $options);
$expected = 'EUR 100 100 100,00';
$this->assertEquals($expected, $result);

$options = ['locale' => 'fr_FR', 'pattern' => '#,###.00 EUR'];
$result = $this->Number->currency($value, 'EUR', $options);
$expected = '100 100 100,00 EUR';
$this->assertEquals($expected, $result);

$options = array('after' => 'øre', 'before' => 'Kr.', 'decimals' => ',', 'thousands' => '.');
Expand Down

0 comments on commit 32ed363

Please sign in to comment.