Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4437'
Browse files Browse the repository at this point in the history
Close #4437
  • Loading branch information
weierophinney committed May 7, 2013
2 parents 19ad7e9 + 4afc2e2 commit 11f305b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 31 deletions.
43 changes: 41 additions & 2 deletions library/Zend/I18n/View/Helper/CurrencyFormat.php
Expand Up @@ -40,6 +40,13 @@ class CurrencyFormat extends AbstractHelper
*/
protected $locale;

/**
* Currency pattern
*
* @var string
*/
protected $currencyPattern;

/**
* If set to true, the currency will be returned with two decimals
*
Expand All @@ -65,15 +72,17 @@ public function __construct()
*
* @param float $number
* @param string $currencyCode
* @param bool $showDecimals
* @param bool $showDecimals
* @param string $locale
* @param string $pattern
* @return string
*/
public function __invoke(
$number,
$currencyCode = null,
$showDecimals = null,
$locale = null
$locale = null,
$pattern = null
) {
if (null === $locale) {
$locale = $this->getLocale();
Expand All @@ -84,6 +93,9 @@ public function __invoke(
if (null !== $showDecimals) {
$this->setShouldShowDecimals($showDecimals);
}
if (null === $pattern) {
$pattern = $this->getCurrencyPattern();
}

$formatterId = md5($locale);

Expand All @@ -94,6 +106,10 @@ public function __invoke(
);
}

if ($pattern !== null) {
$this->formatters[$formatterId]->setPattern($pattern);
}

if ($this->shouldShowDecimals()) {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
} else {
Expand Down Expand Up @@ -127,6 +143,29 @@ public function getCurrencyCode()
return $this->currencyCode;
}


/**
* Set the currency pattern
*
* @param string $currencyPattern
* @return CurrencyFormat
*/
public function setCurrencyPattern($currencyPattern)
{
$this->currencyPattern = $currencyPattern;
return $this;
}

/**
* Get the currency pattern
*
* @return string
*/
public function getCurrencyPattern()
{
return $this->currencyPattern;
}

/**
* Set locale to use instead of the default
*
Expand Down
75 changes: 46 additions & 29 deletions tests/ZendTest/I18n/View/Helper/CurrencyFormatTest.php
Expand Up @@ -41,47 +41,64 @@ public function setUp()
public function currencyProvider()
{
return array(
// locale currency show decimals number expected
array('de_AT', 'EUR', true, 1234.56, '€ 1.234,56'),
array('de_AT', 'EUR', true, 0.123, '€ 0,12'),
array('de_DE', 'EUR', true, 1234567.891234567890000, '1.234.567,89 €'),
array('de_DE', 'RUR', true, 1234567.891234567890000, '1.234.567,89 RUR'),
array('ru_RU', 'EUR', true, 1234567.891234567890000, '1 234 567,89 €'),
array('ru_RU', 'RUR', true, 1234567.891234567890000, '1 234 567,89 р.'),
array('en_US', 'EUR', true, 1234567.891234567890000, '€1,234,567.89'),
array('en_US', 'RUR', true, 1234567.891234567890000, 'RUR1,234,567.89'),
array('en_US', 'USD', true, 1234567.891234567890000, '$1,234,567.89'),
array('de_AT', 'EUR', false, 1234.56, '€ 1.235'),
array('de_AT', 'EUR', false, 0.123, '€ 0'),
array('de_DE', 'EUR', false, 1234567.891234567890000, '1.234.568 €'),
//array('de_DE', 'RUB', false, 1234567.891234567890000, '1.234.567,89 RUB'),
//array('ru_RU', 'EUR', false, 1234567.891234567890000, '1 234 568 €'),
//array('ru_RU', 'RUR', false, 1234567.891234567890000, '1 234 567 р.'),
//array('en_US', 'EUR', false, 1234567.891234567890000, '€1,234,568'),
//array('en_US', 'EUR', false, 1234567.891234567890000, '€1,234,568'),
array('en_US', 'USD', false, 1234567.891234567890000, '$1,234,568'),
// locale currency show decimals number currencyPattern expected
array('de_AT', 'EUR', true, 1234.56, null, '€ 1.234,56'),
array('de_AT', 'EUR', true, 0.123, null, '€ 0,12'),
array('de_AT', 'EUR', true, 0.123, '#,##0.00 ¤', '0,12 €'),
array('de_AT', 'EUR', true, -0.123, '#,##0.00 ¤; ¤ - #,##0.00', ' € - 0,12'),
array('de_AT', 'EUR', true, -0.123, '¤ #,##0.00; - ¤ #,##0.00', '- € 0,12'),
array('de_DE', 'EUR', true, 1234567.891234567890000, null, '1.234.567,89 €'),
array('de_DE', 'RUR', true, 1234567.891234567890000, null, '1.234.567,89 RUR'),
array('ru_RU', 'EUR', true, 1234567.891234567890000, null, '1 234 567,89 €'),
array('ru_RU', 'RUR', true, 1234567.891234567890000, null, '1 234 567,89 р.'),
array('en_US', 'EUR', true, 1234567.891234567890000, null, '€1,234,567.89'),
array('en_US', 'RUR', true, 1234567.891234567890000, null, 'RUR1,234,567.89'),
array('en_US', 'USD', true, 1234567.891234567890000, null, '$1,234,567.89'),
array('de_AT', 'EUR', false, 1234.56, null, '€ 1.235'),
array('de_AT', 'EUR', false, 0.123, null, '€ 0'),
array('de_DE', 'EUR', false, 1234567.891234567890000, null, '1.234.568 €'),
array('de_DE', 'RUB', false, 1234567.891234567890000, null, '1.234.567,89 RUB'),
//array('ru_RU', 'EUR', false, 1234567.891234567890000, null, '1 234 568 €'),
//array('ru_RU', 'RUR', false, 1234567.891234567890000, null, '1 234 567 р.'),
//array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'),
//array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'),
array('en_US', 'USD', false, 1234567.891234567890000, null, '$1,234,568'),
);
}

/**
* @dataProvider currencyProvider
*/
public function testBasic($locale, $currencyCode, $showDecimals, $number, $expected)
public function testBasic($locale, $currencyCode, $showDecimals, $number, $currencyPattern, $expected)
{
$this->assertMbStringEquals($expected, $this->helper->__invoke(
$number, $currencyCode, $showDecimals, $locale
));
$this->assertMbStringEquals(
$expected,
$this->helper->__invoke(
$number,
$currencyCode,
$showDecimals,
$locale,
$currencyPattern
)
);
}

/**
* @dataProvider currencyProvider
*/
public function testSettersProvideDefaults($locale, $currencyCode, $showDecimals, $number, $expected)
{
public function testSettersProvideDefaults(
$locale,
$currencyCode,
$showDecimals,
$number,
$currencyPattern,
$expected
) {
$this->helper
->setLocale($locale)
->setShouldShowDecimals($showDecimals)
->setCurrencyCode($currencyCode);
->setLocale($locale)
->setShouldShowDecimals($showDecimals)
->setCurrencyCode($currencyCode)
->setCurrencyPattern($currencyPattern);

$this->assertMbStringEquals($expected, $this->helper->__invoke($number));
}
Expand All @@ -94,7 +111,7 @@ public function testDefaultLocale()
public function assertMbStringEquals($expected, $test, $message = '')
{
$expected = str_replace(array("\xC2\xA0", ' '), '', $expected);
$test = str_replace(array("\xC2\xA0", ' '), '', $test);
$test = str_replace(array("\xC2\xA0", ' '), '', $test);
$this->assertEquals($expected, $test, $message);
}
}

0 comments on commit 11f305b

Please sign in to comment.