From 19189b42645c061d334890ae416a5c1acfd991fa Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Sat, 4 Jul 2020 01:24:54 +0200 Subject: [PATCH] Fix toCurrency to correctly strip currency sign Zend_Currency::toCurrency has an option to return the currency formatted without the currency sign. It should also trim any whitespaces before or after the currency sign. However it did strip every whitespace from the formatted string, even if the given locale uses spaces to separate thousands groups. This commit fixes this error. Signed-off-by: Nagy Attila Gabor --- library/Zend/Currency.php | 26 ++++++++++++++++++++++---- tests/Zend/CurrencyTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/library/Zend/Currency.php b/library/Zend/Currency.php index 33408c498e..62686aaa54 100644 --- a/library/Zend/Currency.php +++ b/library/Zend/Currency.php @@ -194,12 +194,11 @@ public function toCurrency($value = null, array $options = array()) 'precision' => $options['precision'])); if ($options['position'] !== self::STANDARD) { - $value = str_replace('¤', '', $value); $space = ''; - if (iconv_strpos($value, ' ') !== false) { - $value = str_replace(' ', '', $value); + if (iconv_strpos($value, '¤ ') !== false || iconv_strpos($value, ' ¤') !== false) { $space = ' '; } + $value = $this->_stripCurrencyPattern($value); if ($options['position'] == self::LEFT) { $value = '¤' . $space . $value; @@ -232,7 +231,7 @@ public function toCurrency($value = null, array $options = array()) default: $sign = ''; - $value = str_replace(' ', '', $value); + $value = $this->_stripCurrencyPattern($value); break; } } @@ -241,6 +240,25 @@ public function toCurrency($value = null, array $options = array()) return $value; } + /** + * Strip the currency pattern an any sorrounding + * whitespace from a string value + * + * @param string $value + * @return string + */ + private function _stripCurrencyPattern($value) { + if (iconv_strpos($value, '¤ ') === 0) { + return iconv_substr($value, 2); + } + else if (iconv_strpos($value, ' ¤') === iconv_strlen($value) - 2) { + return iconv_substr($value, 0, iconv_strlen($value) - 2); + } + else { + return str_replace('¤', '', $value); + } + } + /** * Internal method to extract the currency pattern * when a choice is given based on the given value diff --git a/tests/Zend/CurrencyTest.php b/tests/Zend/CurrencyTest.php index adfe8c09ba..2163706a79 100644 --- a/tests/Zend/CurrencyTest.php +++ b/tests/Zend/CurrencyTest.php @@ -303,6 +303,33 @@ public function testToCurrency() $this->assertSame('-₹ 3,00', $INR->toCurrency(-3)); } + /* + * testing space separators + */ + public function testSpaceSeparatorWithNoSymbol() + { + $HUF = new Zend_Currency('HUF','hu_HU'); + $EUR = new Zend_Currency('USD','de_AT'); + $USD = new Zend_Currency('USD','en_US'); + + $options = array( + 'display' => Zend_Currency::NO_SYMBOL + ); + $this->assertSame('53 292,18', $HUF->toCurrency(53292.18, $options)); + $this->assertSame('53.292,18', $EUR->toCurrency(53292.18, $options)); + $this->assertSame('53,292.18', $USD->toCurrency(53292.18, $options)); + + $options['position'] = Zend_Currency::LEFT; + $this->assertSame('53 292,18', $HUF->toCurrency(53292.18, $options)); + $this->assertSame('53.292,18', $EUR->toCurrency(53292.18, $options)); + $this->assertSame('53,292.18', $USD->toCurrency(53292.18, $options)); + + $options['position'] = Zend_Currency::RIGHT; + $this->assertSame('53 292,18', $HUF->toCurrency(53292.18, $options)); + $this->assertSame('53.292,18', $EUR->toCurrency(53292.18, $options)); + $this->assertSame('53,292.18', $USD->toCurrency(53292.18, $options)); + } + /** * testing setFormat *