[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Property / TypeConverter / FloatConverter.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Property\TypeConverter;
3
4 /* *
5 * This script belongs to the Extbase framework *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * Converter which transforms a simple type to a float.
25 *
26 * This is basically done by simply casting it.
27 *
28 * @api
29 */
30 class FloatConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
31 {
32 /**
33 * @var string
34 */
35 const CONFIGURATION_THOUSANDS_SEPARATOR = 'thousandsSeparator';
36
37 /**
38 * @var string
39 */
40 const CONFIGURATION_DECIMAL_POINT = 'decimalPoint';
41
42 /**
43 * @var array<string>
44 */
45 protected $sourceTypes = ['float', 'integer', 'string'];
46
47 /**
48 * @var string
49 */
50 protected $targetType = 'float';
51
52 /**
53 * @var int
54 */
55 protected $priority = 1;
56
57 /**
58 * Actually convert from $source to $targetType, by doing a typecast.
59 *
60 * @param mixed $source
61 * @param string $targetType
62 * @param array $convertedChildProperties
63 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
64 * @return float|\TYPO3\CMS\Extbase\Error\Error
65 * @api
66 */
67 public function convertFrom($source, $targetType, array $convertedChildProperties = [], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = null)
68 {
69 if ($source === null || (string)$source === '') {
70 return null;
71 }
72
73 // We won't backport the full flavored locale parsing of floats from Flow here
74
75 if (is_string($source) && $configuration !== null) {
76 $thousandsSeparator = $configuration->getConfigurationValue(self::class, self::CONFIGURATION_THOUSANDS_SEPARATOR);
77 $decimalPoint = $configuration->getConfigurationValue(self::class, self::CONFIGURATION_DECIMAL_POINT);
78 $source = str_replace($thousandsSeparator, '', $source);
79 $source = str_replace($decimalPoint, '.', $source);
80 }
81
82 if (!is_numeric($source)) {
83 return new \TYPO3\CMS\Extbase\Error\Error('"%s" cannot be converted to a float value.', 1332934124, [$source]);
84 }
85 return (float)$source;
86 }
87 }