‪TYPO3CMS  9.5
FloatConverter.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 {
27  const ‪CONFIGURATION_THOUSANDS_SEPARATOR = 'thousandsSeparator';
28 
32  const ‪CONFIGURATION_DECIMAL_POINT = 'decimalPoint';
33 
37  protected ‪$sourceTypes = ['float', 'integer', 'string'];
38 
42  protected ‪$targetType = 'float';
43 
47  protected ‪$priority = 10;
48 
58  public function ‪convertFrom($source, ‪$targetType, array $convertedChildProperties = [], \‪TYPO3\CMS\‪Extbase\Property\‪PropertyMappingConfigurationInterface $configuration = null)
59  {
60  if ($source === null || (string)$source === '') {
61  return null;
62  }
63 
64  // We won't backport the full flavored locale parsing of floats from Flow here
65 
66  if (is_string($source) && $configuration !== null) {
67  $thousandsSeparator = $configuration->getConfigurationValue(self::class, self::CONFIGURATION_THOUSANDS_SEPARATOR);
68  $decimalPoint = $configuration->getConfigurationValue(self::class, self::CONFIGURATION_DECIMAL_POINT);
69  $source = str_replace([$thousandsSeparator, $decimalPoint], ['', '.'], $source);
70  }
71 
72  if (!is_numeric($source)) {
73  return new \TYPO3\CMS\Extbase\Error\Error('"%s" cannot be converted to a float value.', 1332934124, [$source]);
74  }
75  return (float)$source;
76  }
77 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:21
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:22
‪TYPO3\CMS\Extbase\Property\TypeConverter
Definition: AbstractFileCollectionConverter.php:2
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter\$sourceTypes
‪array< string > $sourceTypes
Definition: FloatConverter.php:36
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter
Definition: FloatConverter.php:23
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter\convertFrom
‪float TYPO3 CMS Extbase Error Error convertFrom($source, $targetType, array $convertedChildProperties=[], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
Definition: FloatConverter.php:55
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter\CONFIGURATION_DECIMAL_POINT
‪const CONFIGURATION_DECIMAL_POINT
Definition: FloatConverter.php:32
‪TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
Definition: AbstractTypeConverter.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter\$targetType
‪string $targetType
Definition: FloatConverter.php:40
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter\CONFIGURATION_THOUSANDS_SEPARATOR
‪const CONFIGURATION_THOUSANDS_SEPARATOR
Definition: FloatConverter.php:27
‪TYPO3\CMS\Extbase\Property\TypeConverter\FloatConverter\$priority
‪int $priority
Definition: FloatConverter.php:44