[BUGFIX] Speed up f:format.currency
authorKilian Hann <khann@fconnection.com>
Fri, 13 Apr 2012 20:09:14 +0000 (22:09 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 22 Jul 2012 22:19:10 +0000 (00:19 +0200)
If the children evaluate to an empty string, they will be initialized
with 0.00 which speeds up the performance of number_format()

Change-Id: I43176199a5568943483925472ed1323373cdacd1
Fixes: #34519
Releases: 6.0
Related: #36207
Reviewed-on: http://review.typo3.org/10443
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 53d98db..16aab56 100644 (file)
@@ -52,17 +52,25 @@ class Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper extends Tx_Fluid_Core_ViewH
         * @api
         */
        public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE) {
-               $stringToFormat = $this->renderChildren();
-               $output = number_format($stringToFormat, 2, $decimalSeparator, $thousandsSeparator);
+               $floatToFormat = $this->renderChildren();
 
-               if ($currencySign !== ''){
-                       $currencySeparator = ($separateCurrency) ? ' ':'';
-                       if ($prependCurrency === TRUE){
+               if (empty($floatToFormat)) {
+                       $floatToFormat = 0.00;
+               } else {
+                       $floatToFormat = floatval($floatToFormat);
+               }
+
+               $output = number_format($floatToFormat, 2, $decimalSeparator, $thousandsSeparator);
+
+               if ($currencySign !== '') {
+                       $currencySeparator = ($separateCurrency) ? ' ' : '';
+                       if ($prependCurrency === TRUE) {
                                $output = $currencySign . $currencySeparator . $output;
-                       }else{
+                       } else {
                                $output = $output . $currencySeparator . $currencySign;
                        }
                }
+
                return $output;
        }
 }
index afe476c..349d29f 100644 (file)
@@ -88,11 +88,51 @@ class Tx_Fluid_Tests_Unit_ViewHelpers_Format_CurrencyViewHelperTest extends Tx_E
        /**
         * @test
         */
+       public function viewHelperRendersEmptyString() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
+               $actualResult = $viewHelper->render();
+               $this->assertEquals('0,00', $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function viewHelperRendersZeroValues() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(0));
+               $actualResult = $viewHelper->render();
+               $this->assertEquals('0,00', $actualResult);
+       }
+
+       /**
+        * @test
+        */
        public function viewHelperRendersNegativeAmounts() {
                $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
                $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(-123.456));
                $actualResult = $viewHelper->render();
                $this->assertEquals('-123,46', $actualResult);
        }
+
+       /**
+        * @test
+        */
+       public function viewHelperRendersStringsToZeroValueFloat() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('TYPO3'));
+               $actualResult = $viewHelper->render();
+               $this->assertEquals('0,00', $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function viewHelperRendersCommaValuesToValueBeforeComma() {
+               $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('12,34.00'));
+               $actualResult = $viewHelper->render();
+               $this->assertEquals('12,00', $actualResult);
+       }
 }
 ?>