[FEATURE] Add configuration options to FloatConverter 29/47829/3
authorNicole Cordes <typo3@cordes.co>
Wed, 20 Apr 2016 21:41:35 +0000 (23:41 +0200)
committerPhilipp Gampe <philipp.gampe@typo3.org>
Thu, 21 Apr 2016 12:20:57 +0000 (14:20 +0200)
Converting a string to a float value needs some specific format of the
string (no thousand separator and '.' as decimal point).

So it should be possible to define some pre-sanitation for the
FloatConverter to be able to convert strings like 123,42 to float.

This patch introduces configuration options and sanitizes the string
before parsing it to float.

Resolves: #75827
Releases: master
Change-Id: I716a5a6d3b7dd4f9f8dd0f01db850a2ee82cd376
Reviewed-on: https://review.typo3.org/47829
Reviewed-by: Stephan Großberndt <stephan@grossberndt.de>
Tested-by: Stephan Großberndt <stephan@grossberndt.de>
Reviewed-by: Philipp Gampe <philipp.gampe@typo3.org>
Tested-by: Philipp Gampe <philipp.gampe@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-75827-AddConfigurationOptionsToFloatConverter.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Property/TypeConverter/FloatConverter.php
typo3/sysext/extbase/Tests/Unit/Property/TypeConverter/FloatConverterTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-75827-AddConfigurationOptionsToFloatConverter.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-75827-AddConfigurationOptionsToFloatConverter.rst
new file mode 100644 (file)
index 0000000..bb5f8a4
--- /dev/null
@@ -0,0 +1,27 @@
+=======================================================================================================
+Feature: #75827 - Add configuration options to \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter
+=======================================================================================================
+
+Description
+===========
+
+It is now possible to define thousand separator and decimal point for FloatConverter.
+This can be used to ensure proper sanitation before converting a string to a float.
+
+You can define the configuration for every property like this:
+
+.. code-block:: php
+
+   $this->arguments['<argumentName>']
+      ->getPropertyMappingConfiguration()
+      ->forProperty('<propertyName>') // this line can be skipped in order to specify the format for all properties
+      ->setTypeConverterOption(
+         \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::class,
+         \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::CONFIGURATION_THOUSANDS_SEPARATOR,
+         '.'
+      )
+      ->setTypeConverterOption(
+         \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::class,
+         \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::CONFIGURATION_DECIMAL_POINT,
+         ','
+      );
index 806fd75..992ab91 100644 (file)
@@ -30,6 +30,16 @@ namespace TYPO3\CMS\Extbase\Property\TypeConverter;
 class FloatConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
 {
     /**
+     * @var string
+     */
+    const CONFIGURATION_THOUSANDS_SEPARATOR = 'thousandsSeparator';
+
+    /**
+     * @var string
+     */
+    const CONFIGURATION_DECIMAL_POINT = 'decimalPoint';
+
+    /**
      * @var array<string>
      */
     protected $sourceTypes = array('float', 'integer', 'string');
@@ -59,11 +69,19 @@ class FloatConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractT
         if ($source === null || (string)$source === '') {
             return null;
         }
+
         // We won't backport the full flavored locale parsing of floats from Flow here
 
+        if (is_string($source) && $configuration !== null) {
+            $thousandsSeparator = $configuration->getConfigurationValue(FloatConverter::class, self::CONFIGURATION_THOUSANDS_SEPARATOR);
+            $decimalPoint = $configuration->getConfigurationValue(FloatConverter::class, self::CONFIGURATION_DECIMAL_POINT);
+            $source = str_replace($thousandsSeparator, '', $source);
+            $source = str_replace($decimalPoint, '.', $source);
+        }
+
         if (!is_numeric($source)) {
             return new \TYPO3\CMS\Extbase\Error\Error('"%s" cannot be converted to a float value.', 1332934124, array($source));
         }
-        return (float) $source;
+        return (float)$source;
     }
 }
index 430377a..e55017a 100644 (file)
@@ -73,6 +73,25 @@ class FloatConverterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @test
      */
+    public function convertFromShouldRespectConfiguration()
+    {
+        $mockMappingConfiguration = $this->getMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
+        $mockMappingConfiguration
+            ->expects($this->at(0))
+            ->method('getConfigurationValue')
+            ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::CONFIGURATION_THOUSANDS_SEPARATOR)
+            ->willReturn('.');
+        $mockMappingConfiguration
+            ->expects($this->at(1))
+            ->method('getConfigurationValue')
+            ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter::CONFIGURATION_DECIMAL_POINT)
+            ->willReturn(',');
+        $this->assertSame(1024.42, $this->converter->convertFrom('1.024,42', 'float', array(), $mockMappingConfiguration));
+    }
+
+    /**
+     * @test
+     */
     public function convertFromReturnsAnErrorIfSpecifiedStringIsNotNumeric()
     {
         $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $this->converter->convertFrom('not numeric', 'float'));