diff --git a/cake/libs/view/helpers/number.php b/cake/libs/view/helpers/number.php index 4117736818f..c9226b52468 100644 --- a/cake/libs/view/helpers/number.php +++ b/cake/libs/view/helpers/number.php @@ -31,6 +31,39 @@ */ class NumberHelper extends AppHelper { +/** + * Currencies supported by the helper. You can add additional currency formats + * with NumberHelper::addFormat + * + * @var array + * @access protected + **/ + var $_currencies = array( + 'USD' => array( + 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',', + 'decimals' => '.', 'negative' => '()', 'escape' => true + ), + 'GBP' => array( + 'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',', + 'decimals' => '.', 'negative' => '()','escape' => false + ), + 'EUR' => array( + 'before'=>'€', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.', + 'decimals' => ',', 'negative' => '()', 'escape' => false + ) + ); + +/** + * Default options for currency formats + * + * @var array + * @access protected + **/ + var $_currencyDefaults = array( + 'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',', + 'decimals' => '.','negative' => '()', 'escape' => true + ); + /** * Formats a number with a level of precision. * @@ -127,27 +160,10 @@ function format($number, $options = false) { * @return string Number formatted as a currency. */ function currency($number, $currency = 'USD', $options = array()) { - $default = array( - 'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',', - 'decimals' => '.','negative' => '()', 'escape' => true - ); - $currencies = array( - 'USD' => array( - 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',', - 'decimals' => '.', 'negative' => '()', 'escape' => true - ), - 'GBP' => array( - 'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',', - 'decimals' => '.', 'negative' => '()','escape' => false - ), - 'EUR' => array( - 'before'=>'€', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.', - 'decimals' => ',', 'negative' => '()', 'escape' => false - ) - ); - - if (isset($currencies[$currency])) { - $default = $currencies[$currency]; + $default = $this->_currencyDefaults; + + if (isset($this->_currencies[$currency])) { + $default = $this->_currencies[$currency]; } elseif (is_string($currency)) { $options['before'] = $currency; } @@ -184,5 +200,33 @@ function currency($number, $currency = 'USD', $options = array()) { } return $result; } + +/** + * Add a currency format to the Number helper. Makes reusing + * currency formats easier. + * + * {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}} + * + * You can now use `NOK` as a shortform when formatting currency amounts. + * + * {{{ $number->currency($value, 'NOK'); }}} + * + * Added formats are merged with the following defaults. + * + * {{{ + * array( + * 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',', + * 'decimals' => '.', 'negative' => '()', 'escape' => true + * ) + * }}} + * + * @param string $formatName The format name to be used in the future. + * @param array $options The array of options for this format. + * @return void + **/ + function addFormat($formatName, $options) { + $this->_currencies[$formatName] = $options + $this->_currencyDefaults; + } + } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/view/helpers/number.test.php b/cake/tests/cases/libs/view/helpers/number.test.php index 99435fe1ba3..9502cdd1a2f 100644 --- a/cake/tests/cases/libs/view/helpers/number.test.php +++ b/cake/tests/cases/libs/view/helpers/number.test.php @@ -64,7 +64,7 @@ function endTest() { * @access public * @return void */ - function testFormatAndCurrency() { + function testFormat() { $value = '100100100'; $result = $this->Number->format($value, '#'); @@ -91,6 +91,8 @@ function testFormatAndCurrency() { * @return void */ function testCurrency() { + $value = '100100100'; + $result = $this->Number->currency($value); $expected = '$100,100,100.00'; $this->assertEqual($expected, $result); @@ -139,13 +141,19 @@ function testCurrency() { * @return void */ function testCurrencyAddFormat() { - if ($this->skipIf(true, 'NumberHelper::addFormat Not implemented')) { - return; - } - $this->Number->addFormat('NOK',array('before'=>'Kr. ')); - $result = $this->Number->currency(1000,'NOK'); + $this->Number->addFormat('NOK', array('before' => 'Kr. ')); + $result = $this->Number->currency(1000, 'NOK'); $expected = 'Kr. 1,000.00'; $this->assertEqual($expected,$result); + + $this->Number->addFormat('Other', array('before' => '$$ ', 'after' => 'c!')); + $result = $this->Number->currency(0.22, 'Other'); + $expected = '22c!'; + $this->assertEqual($expected,$result); + + $result = $this->Number->currency(-10, 'Other'); + $expected = '($$ 10.00)'; + $this->assertEqual($expected,$result); } /**