[FEATURE] CurrencyViewhelper can prepend the currency sign
authorKilian Hann <khann@fconnection.com>
Fri, 13 Apr 2012 20:23:43 +0000 (22:23 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 21 Jul 2012 08:57:46 +0000 (10:57 +0200)
With the flag $prependCurrency = TRUE the currency sign can be prepended
instead of appended (e.g. CHF 123.00)
The additional parameter $separateCurrency determines whether the currency
sign should be separated by a space (e.g. $123.00).

Change-Id: I5dd13de5fd40ba165fdc2dbfa17c7a6d224dc296
Fixes: #33854
Releases: 1.4
FLOW3 Issue: #36014
Reviewed-on: http://review.typo3.org/10444
Reviewed-by: Ingo Pfennigstorf
Tested-by: Ingo Pfennigstorf
Reviewed-by: Matthias Nitsch
Reviewed-by: Daniel Lorenz
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/fluid/Classes/ViewHelpers/Format/CurrencyViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/CurrencyViewHelperTest.php

index 1b5e672..53d98db 100644 (file)
  * </output>
  *
  * <code title="All parameters">
- * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator=",">54321</f:format.currency>
+ * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="TRUE" separateCurrency="FALSE">54321</f:format.currency>
  * </code>
  * <output>
- * 54,321.00 $
+ * $54,321.00
  * </output>
  *
  * <code title="Inline notation">
@@ -46,14 +46,22 @@ class Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper extends Tx_Fluid_Core_ViewH
         * @param string $currencySign (optional) The currency sign, eg $ or €.
         * @param string $decimalSeparator (optional) The separator for the decimal point.
         * @param string $thousandsSeparator (optional) The thousands separator.
+        * @param boolean $prependCurrency (optional) Select if the curreny sign should be prepended
+        * @param boolean $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
         * @return string the formatted amount.
         * @api
         */
-       public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.') {
+       public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE) {
                $stringToFormat = $this->renderChildren();
                $output = number_format($stringToFormat, 2, $decimalSeparator, $thousandsSeparator);
-               if($currencySign !== '') {
-                       $output.= ' ' . $currencySign;
+
+               if ($currencySign !== ''){
+                       $currencySeparator = ($separateCurrency) ? ' ':'';
+                       if ($prependCurrency === TRUE){
+                               $output = $currencySign . $currencySeparator . $output;
+                       }else{
+                               $output = $output . $currencySeparator . $currencySign;
+                       }
                }
                return $output;
        }
index d12ad83..afe476c 100644 (file)
@@ -34,6 +34,27 @@ class Tx_Fluid_Tests_Unit_ViewHelpers_Format_CurrencyViewHelperTest extends Tx_E
                $this->assertEquals('123,00 foo', $actualResult);
        }
 
+       /**
+        * @test
+        */
+       public function viewHelperRendersPrependedCurrencySign() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
+               $actualResult = $viewHelper->render('foo',',', '.',TRUE);
+               $this->assertEquals('foo 123,00', $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function viewHelperRespectsCurrencySeparator() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
+               $actualResult = $viewHelper->render('foo', ',', '.', TRUE,FALSE);
+               $this->assertEquals('foo123,00', $actualResult);
+       }
+
+
        /**
         * @test
         */
@@ -74,4 +95,4 @@ class Tx_Fluid_Tests_Unit_ViewHelpers_Format_CurrencyViewHelperTest extends Tx_E
                $this->assertEquals('-123,46', $actualResult);
        }
 }
-?>
\ No newline at end of file
+?>