[TASK] Streamline phpdoc annotations in EXT:extbase
[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 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
17 /**
18 * Converter which transforms a simple type to a float.
19 *
20 * This is basically done by simply casting it.
21 */
22 class FloatConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
23 {
24 /**
25 * @var string
26 */
27 const CONFIGURATION_THOUSANDS_SEPARATOR = 'thousandsSeparator';
28
29 /**
30 * @var string
31 */
32 const CONFIGURATION_DECIMAL_POINT = 'decimalPoint';
33
34 /**
35 * @var array<string>
36 */
37 protected $sourceTypes = ['float', 'integer', 'string'];
38
39 /**
40 * @var string
41 */
42 protected $targetType = 'float';
43
44 /**
45 * @var int
46 */
47 protected $priority = 10;
48
49 /**
50 * Actually convert from $source to $targetType, by doing a typecast.
51 *
52 * @param mixed $source
53 * @param string $targetType
54 * @param array $convertedChildProperties
55 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
56 * @return float|\TYPO3\CMS\Extbase\Error\Error
57 */
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 }