Skip to content
Permalink
Browse files

Adding NumberHelper::addFormat and test cases. Refs #5630

  • Loading branch information...
markstory committed Sep 11, 2009
1 parent 818e734 commit 9c41efd6fefbed88db4ac97607ce9372a4704eb2
Showing with 79 additions and 27 deletions.
  1. +65 −21 cake/libs/view/helpers/number.php
  2. +14 −6 cake/tests/cases/libs/view/helpers/number.test.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;
}
}
?>
@@ -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);
}
/**

0 comments on commit 9c41efd

Please sign in to comment.
You can’t perform that action at this time.