Browse Source

More improvents to Number::currency()

Removed the feature to auto format negative numbers into parenthesis
Removed the ability to set a custom currency symbol as it can be done with pattern
Jose Lorenzo Rodriguez 11 years ago
parent
commit
617b98e999
2 changed files with 17 additions and 92 deletions
  1. 9 59
      src/Utility/Number.php
  2. 8 33
      tests/TestCase/Utility/NumberTest.php

+ 9 - 59
src/Utility/Number.php

@@ -66,17 +66,6 @@ class Number {
 	);
 
 /**
- * Default options for currency formats
- *
- * @var array
- */
-	protected static $_currencyDefaults = array(
-		'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
-		'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
-		'fractionExponent' => 2
-	);
-
-/**
  * A list of number formatters indexed by locale
  *
  * @var array
@@ -306,6 +295,7 @@ class Number {
  */
 	public static function currency($value, $currency = null, array $options = array()) {
 		$value = (float)$value;
+		$currency = $currency ?: static::defaultCurrency();
 
 		if (isset($options['zero']) && !$value) {
 			return $options['zero'];
@@ -324,61 +314,21 @@ class Number {
 			);
 		}
 
-		if ($currency === null) {
-			$currency = static::defaultCurrency();
-		}
-
 		$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'] !== '') {
-			$options['wholeSymbol'] = $options['before'];
-		}
-		if (isset($options['after']) && !$options['after'] !== '') {
-			$options['fractionSymbol'] = $options['after'];
-		}
-
-		$result = $options['before'] = $options['after'] = null;
-
-		$symbolKey = 'whole';
-		;
-		if (!$value) {
-			if ($options['zero'] !== 0) {
-				return $options['zero'];
-			}
-		} elseif ($value < 1 && $value > -1) {
-			if ($options['fractionSymbol'] !== false) {
-				$multiply = pow(10, $options['fractionExponent']);
-				$value = $value * $multiply;
-				$options['places'] = null;
-				$symbolKey = 'fraction';
-			}
+		if (!empty($options['fractionSymbol']) && $value > 0 && $value < 1) {
+			$places = isset($options['places']) ? $options['places'] : 2;
+			$value = $value * pow(10, $places);
+			$pos = isset($options['fractionPosition']) ? $options['fractionPosition'] : 'after';
+			return static::format($value, ['places' => 0, $pos => $options['fractionSymbol']]);
 		}
 
-		$position = $options[$symbolKey . 'Position'] !== 'after' ? 'before' : 'after';
-		$options[$position] = $options[$symbolKey . 'Symbol'];
-
-		$abs = abs($value);
-		$result = static::format($abs, $options);
-
-		if ($value < 0) {
-			if ($options['negative'] === '()') {
-				$result = '(' . $result . ')';
-			} else {
-				$result = $options['negative'] . $result;
-			}
-		}
-		return $result;
+		$before = isset($options['before']) ? $options['before'] : null;
+		$after = isset($options['after']) ? $options['after'] : null;
+		return $before . $formatter->formatCurrency($value, $currency) . $after;
 	}
 
 /**

+ 8 - 33
tests/TestCase/Utility/NumberTest.php

@@ -192,46 +192,25 @@ class NumberTest extends TestCase {
 		$expected = '(€1 235,03)';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Number->currency(0.5, 'USD');
+		$result = $this->Number->currency(0.5, 'USD', ['locale' => 'en_US', 'fractionSymbol' => 'c']);
 		$expected = '50c';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Number->currency(0.5, null, array('after' => 'øre'));
-		$expected = '50øre';
-		$this->assertEquals($expected, $result);
-
-		$options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => ' cents');
-		$result = $this->Number->currency(0.2, null, $options);
+		$options = ['fractionSymbol' => ' cents'];
+		$result = $this->Number->currency(0.2, 'USD', $options);
 		$expected = '20 cents';
 		$this->assertEquals($expected, $result);
 
-		$options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => 'cents ',
-			'fractionPosition' => 'before');
+		$options = ['fractionSymbol' => 'cents ', 'fractionPosition' => 'before'];
 		$result = $this->Number->currency(0.2, null, $options);
 		$expected = 'cents 20';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after'));
-		$expected = '311.00$';
-		$this->assertEquals($expected, $result);
-
 		$result = $this->Number->currency(0.2, 'EUR');
-		$expected = '€0,20';
+		$expected = '€0.20';
 		$this->assertEquals($expected, $result);
 
-		$options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
-			'fractionPosition' => 'after');
-		$result = $this->Number->currency(12, null, $options);
-		$expected = '12.00 dollars';
-		$this->assertEquals($expected, $result);
-
-		$options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
-			'fractionPosition' => 'after');
-		$result = $this->Number->currency(0.12, null, $options);
-		$expected = '12 cents';
-		$this->assertEquals($expected, $result);
-
-		$options = array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$');
+		$options = ['fractionSymbol' => false, 'fractionPosition' => 'before'];
 		$result = $this->Number->currency(0.5, null, $options);
 		$expected = '$0.50';
 		$this->assertEquals($expected, $result);
@@ -248,16 +227,12 @@ class NumberTest extends TestCase {
 		$expected = '£0.00';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Number->currency('-2.23300', 'JPY');
-		$expected = '(¥2.23)';
-		$this->assertEquals($expected, $result);
-
 		$result = $this->Number->currency('22.389', 'CAD');
-		$expected = '$22.39';
+		$expected = 'CA$22.39';
 		$this->assertEquals($expected, $result);
 
 		$result = $this->Number->currency('4.111', 'AUD');
-		$expected = '$4.11';
+		$expected = 'A$4.11';
 		$this->assertEquals($expected, $result);
 	}