[TASK] Add Double2Converter
This commit is contained in:
parent
db5c79cef6
commit
1cd9c2567e
88
Classes/Property/TypeConverter/Double2Converter.php
Normal file
88
Classes/Property/TypeConverter/Double2Converter.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
namespace Cjel\TemplatesAide\Property\TypeConverter;
|
||||||
|
|
||||||
|
/***
|
||||||
|
*
|
||||||
|
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the
|
||||||
|
* LICENSE.txt file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* (c) 2020 Philipp Dieter <philippdieter@attic-media.net>
|
||||||
|
*
|
||||||
|
***/
|
||||||
|
|
||||||
|
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<string>
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
## 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(
|
call_user_func(
|
||||||
function()
|
function()
|
||||||
{
|
{
|
||||||
@ -43,6 +46,9 @@ call_user_func(
|
|||||||
'Default Config'
|
'Default Config'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
ExtensionUtility::registerTypeConverter(Double2Converter::class);
|
||||||
|
|
||||||
if (TYPO3_MODE == 'BE') {
|
if (TYPO3_MODE == 'BE') {
|
||||||
//$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
|
//$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
|
||||||
// \TYPO3\CMS\Core\Page\PageRenderer::class
|
// \TYPO3\CMS\Core\Page\PageRenderer::class
|
||||||
|
Loading…
x
Reference in New Issue
Block a user