Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

I18n currencyFormat helper: add the currencyPattern attribute and extend the unittest #4437

Closed
wants to merge 1 commit into from

1 participant

@MarkusM85

To change the currency output by the optional pattern.

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4437'
Close #4437
418f302
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-i18n
@weierophinney weierophinney Merge pull request zendframework/zf2#4437 from MarkusM85/develop
I18n currencyFormat helper: add the currencyPattern attribute and extend the unittest
ff67d39
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-i18n
@weierophinney weierophinney Merge branch 'hotfix/4437' 8649d44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 7, 2013
This page is out of date. Refresh to see the latest.
View
43 library/Zend/I18n/View/Helper/CurrencyFormat.php
@@ -41,6 +41,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
*
* @var bool
@@ -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();
@@ -84,6 +93,9 @@ public function __invoke(
if (null !== $showDecimals) {
$this->setShouldShowDecimals($showDecimals);
}
+ if (null === $pattern) {
+ $pattern = $this->getCurrencyPattern();
+ }
$formatterId = md5($locale);
@@ -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 {
@@ -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
*
View
75 tests/ZendTest/I18n/View/Helper/CurrencyFormatTest.php
@@ -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));
}
@@ -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);
}
}
Something went wrong with that request. Please try again.