Skip to content

Commit

Permalink
Adding NumberHelper::addFormat and test cases. Refs #5630
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 11, 2009
1 parent 818e734 commit 9c41efd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
86 changes: 65 additions & 21 deletions cake/libs/view/helpers/number.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

}
?>
20 changes: 14 additions & 6 deletions cake/tests/cases/libs/view/helpers/number.test.php
Expand Up @@ -64,7 +64,7 @@ function endTest() {
* @access public
* @return void
*/
function testFormatAndCurrency() {
function testFormat() {
$value = '100100100';

$result = $this->Number->format($value, '#');
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 9c41efd

Please sign in to comment.