[BUGFIX] EXT:form - avoid number reduction in currency filter 75/43475/5
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Tue, 22 Sep 2015 13:16:10 +0000 (15:16 +0200)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Sun, 25 Oct 2015 22:15:56 +0000 (23:15 +0100)
Avoid a number reduction each time the filter is applied.
Preserve the decimal places.

Resolves: #69807
Releases: master
Change-Id: I1aed27489ea9a7b7d1f3a3e593803bf075ea25a9
Reviewed-on: https://review.typo3.org/43475
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
typo3/sysext/form/Classes/Domain/Filter/CurrencyFilter.php
typo3/sysext/form/Tests/Unit/Filter/CurrencyFilterTest.php

index c556686..94d2da8 100644 (file)
@@ -89,7 +89,21 @@ class CurrencyFilter extends AbstractFilter implements FilterInterface
      */
     public function filter($value)
     {
-        $value = (double) ((string)$value);
+        $value = str_replace(
+            array(
+                $this->thousandSeparator,
+                $this->decimalsPoint,
+            ),
+            array(
+                '',
+                '.'
+            ),
+            (string)$value
+        );
+
+        // replace all non numeric characters, decimalPoint and negativ sign
+        $value = preg_replace("/[^0-9.-]/", "", $value);
+        $value = (double)$value;
         return number_format($value, 2, $this->decimalsPoint, $this->thousandSeparator);
     }
 }
index 2bd6198..9cf49fc 100644 (file)
@@ -33,29 +33,59 @@ class CurrencyFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         return array(
             '1200 => 1.200,00' => array(
-                1200, // input
+                '1200', // input
                 '.', // thousand separator
                 ',', // decimal point
                 '1.200,00' // expected
             ),
             '0 => 0,00' => array(
-                0,
+                '0',
                 null,
                 ',',
                 '0,00'
             ),
             '3333.33 => 3,333.33' => array(
-                3333.33,
+                '3333.33',
                 ',',
                 '.',
                 '3,333.33'
             ),
             '1099.33 => 1 099,33' => array(
-                1099.33,
+                '1099.33',
                 ' ',
                 ',',
                 '1 099,33'
             ),
+            '1200,00 => 1.200,00' => array(
+                '1200,00', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
+            '1.200,00 => 1.200,00' => array(
+                '1.200,00', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
+            '1.200 => 1.200,00' => array(
+                '1.200', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
+            '-1 => -1,00' => array(
+                '-1', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '-1,00' // expected
+            ),
+            '1.200 => 1.200,00' => array(
+                '1.200', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
         );
     }