mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding NumberHelper::addFormat and test cases. Refs #5630
This commit is contained in:
parent
818e7340f7
commit
9c41efd6fe
2 changed files with 78 additions and 26 deletions
|
@ -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 @@ class NumberHelper extends AppHelper {
|
|||
* @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
|
||||
)
|
||||
);
|
||||
$default = $this->_currencyDefaults;
|
||||
|
||||
if (isset($currencies[$currency])) {
|
||||
$default = $currencies[$currency];
|
||||
if (isset($this->_currencies[$currency])) {
|
||||
$default = $this->_currencies[$currency];
|
||||
} elseif (is_string($currency)) {
|
||||
$options['before'] = $currency;
|
||||
}
|
||||
|
@ -184,5 +200,33 @@ class NumberHelper extends AppHelper {
|
|||
}
|
||||
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 @@ class NumberHelperTest extends CakeTestCase {
|
|||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testFormatAndCurrency() {
|
||||
function testFormat() {
|
||||
$value = '100100100';
|
||||
|
||||
$result = $this->Number->format($value, '#');
|
||||
|
@ -91,6 +91,8 @@ class NumberHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testCurrency() {
|
||||
$value = '100100100';
|
||||
|
||||
$result = $this->Number->currency($value);
|
||||
$expected = '$100,100,100.00';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
@ -139,13 +141,19 @@ class NumberHelperTest extends CakeTestCase {
|
|||
* @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');
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue