From 1cd9c2567eb88a8e16a4d70553898b58f677a10a Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Sun, 4 Oct 2020 23:48:22 +0200 Subject: [PATCH] [TASK] Add Double2Converter --- .../TypeConverter/Double2Converter.php | 88 +++++++++++++++++++ ext_tables.php | 6 ++ 2 files changed, 94 insertions(+) create mode 100644 Classes/Property/TypeConverter/Double2Converter.php diff --git a/Classes/Property/TypeConverter/Double2Converter.php b/Classes/Property/TypeConverter/Double2Converter.php new file mode 100644 index 0000000..06ad658 --- /dev/null +++ b/Classes/Property/TypeConverter/Double2Converter.php @@ -0,0 +1,88 @@ + + * + ***/ + +use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface; +use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter; + +/** + * Converter which transforms arrays to arrays. + */ +class Double2Converter extends AbstractTypeConverter +{ + /** + * @var array + */ + protected $sourceTypes = ['integer', 'string']; + + /** + * @var string + */ + protected $targetType = 'double2'; + + /** + * @var int + */ + protected $priority = 10; + + /** + * @param mixed $source + * @param string $targetType + * @return bool + * @internal only to be used within Extbase, not part of TYPO3 Core API. + */ + public function canConvertFrom($source, $targetType) + { + return is_string($source) ||is_integer($source); + } + + /** + * Copied from + * TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval + * + * @param string|array $source + * @param string $targetType + * @param array $convertedChildProperties + * @param PropertyMappingConfigurationInterface $configuration + * @return array + */ + public function convertFrom( + $source, + $targetType, + array $convertedChildProperties = [], + PropertyMappingConfigurationInterface $configuration = null + ) { + $value = preg_replace('/[^0-9,\\.-]/', '', $source); + $negative = $value[0] === '-'; + $value = strtr($value, [',' => '.', '-' => '']); + if (strpos($value, '.') === false) { + $value .= '.0'; + } + $valueArray = explode('.', $value); + $dec = array_pop($valueArray); + $value = implode('', $valueArray) . '.' . $dec; + if ($negative) { + $value *= -1; + } + $value = number_format($value, 2, '.', ''); + //\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump( + // $source, null, 3 + //); + //if (is_string($source)) { + // if ($source === '') { + // $source = []; + // } + //} + return $value; + } +} diff --git a/ext_tables.php b/ext_tables.php index 2106264..778d708 100644 --- a/ext_tables.php +++ b/ext_tables.php @@ -20,6 +20,9 @@ call_user_func( ); ## EXTENSION BUILDER DEFAULTS END TOKEN - Everything BEFORE this line is overwritten with the defaults of the extension builder +use Cjel\TemplatesAide\Property\TypeConverter\Double2Converter; +use TYPO3\CMS\Extbase\Utility\ExtensionUtility; + call_user_func( function() { @@ -43,6 +46,9 @@ call_user_func( 'Default Config' ); + + ExtensionUtility::registerTypeConverter(Double2Converter::class); + if (TYPO3_MODE == 'BE') { //$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( // \TYPO3\CMS\Core\Page\PageRenderer::class