class FloatConverter extends AbstractTypeConverter (View source)

Converter which transforms a float, integer or string to a float.

This is basically done by simply casting it, unless the input is a string and you provide some configuration options which will make this converter use Flow's locale parsing capabilities in order to respect deviating decimal separators.

Using NULL or an empty string as input will result in a NULL return value.

Advanced usage in action controller context

Using default locale::

protected function initializeCreateAction() { $this->arguments['newBid']->getPropertyMappingConfiguration()->forProperty('price')->setTypeConverterOption( \Neos\Flow\Property\TypeConverter\FloatConverter::class, 'locale', true ); }

Just providing true as option value will use the current default locale. In case that default locale is "DE" for Germany for example, where a comma is used as decimal separator, the mentioned code will return (float)15.5 when the input was (string)"15,50".

Using arbitrary locale::

protected function initializeCreateAction() { $this->arguments['newBid']->getPropertyMappingConfiguration()->forProperty('price')->setTypeConverterOption( \Neos\Flow\Property\TypeConverter\FloatConverter::class, 'locale', 'fr' ); }

Parsing mode

There are two parsing modes available, strict and lenient mode. Strict mode will check all constraints of the provided format, and if any of them are not fulfilled, the conversion will not take place. In Lenient mode the parser will try to extract the intended number from the string, even if it's not well formed. Default for strict mode is true.

Example setting lenient mode (abridged)::

->setTypeConverterOption( \Neos\Flow\Property\TypeConverter\FloatConverter::class, 'strictMode', false );

Format type

Format type can be decimal, percent or currency; represented as class constant FORMAT_TYPE_DECIMAL, FORMAT_TYPE_PERCENT or FORMAT_TYPE_CURRENCY of class Neos\Flow\I18n\Cldr\Reader\NumbersReader. Default, if none given, is FORMAT_TYPE_DECIMAL.

Example setting format type currency (abridged)::

->setTypeConverterOption( \Neos\Flow\Property\TypeConverter\FloatConverter::class, 'formatType', \Neos\Flow\I18n\Cldr\Reader\NumbersReader::FORMAT_TYPE_CURRENCY );

Format length

Format type can be default, full, long, medium or short; represented as class constant FORMAT_LENGTH_DEFAULT, FORMAT_LENGTH_FULL, FORMAT_LENGTH_LONG etc., of class Neos\Flow\I18n\Cldr\Reader\NumbersReader. The format length has a technical background in the CLDR repository, and specifies whether a different number pattern should be used. In most cases leaving this DEFAULT would be the correct choice.

Example setting format length (abridged)::

->setTypeConverterOption( \Neos\Flow\Property\TypeConverter\FloatConverter::class, 'formatLength', \Neos\Flow\I18n\Cldr\Reader\NumbersReader::FORMAT_LENGTH_FULL );

Properties

protected string[] $sourceTypes
protected string $targetType
protected int $priority
protected Service $localizationService
protected NumberParser $numberParser

Methods

string[]
getSupportedSourceTypes()

Returns the list of source types the TypeConverter can handle.

string
getSupportedTargetType()

Return the target type this TypeConverter converts to.

string
getTargetTypeForSource(mixed $source, string $originalTargetType, PropertyMappingConfigurationInterface $configuration = null)

Returns the $originalTargetType unchanged in this implementation.

int
getPriority()

Return the priority of this TypeConverter. TypeConverters with a high priority are chosen before low priority.

bool
canConvertFrom(mixed $source, string $targetType)

This implementation always returns true for this method.

array
getSourceChildPropertiesToBeConverted(mixed $source)

Returns an empty list of sub property names

string|null
getTypeOfChildProperty(string $targetType, string $propertyName, PropertyMappingConfigurationInterface $configuration)

This method is never called, as getSourceChildPropertiesToBeConverted() returns an empty array.

mixed|null|Error
convertFrom(mixed $source, string $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)

Actually convert from $source to $targetType, by doing a typecast.

float|Error
parseUsingLocaleIfConfigured(string $source, PropertyMappingConfigurationInterface $configuration)

Tries to parse the input using the NumberParser.

array
getConfigurationKeysAndValues(PropertyMappingConfigurationInterface $configuration, array $configurationKeys)

Helper method to collect configuration for this class.

Details

string[] getSupportedSourceTypes()

Returns the list of source types the TypeConverter can handle.

Must be PHP simple types, classes or object is not allowed.

Return Value

string[]

string getSupportedTargetType()

Return the target type this TypeConverter converts to.

Can be a simple type or a class name.

Return Value

string

string getTargetTypeForSource(mixed $source, string $originalTargetType, PropertyMappingConfigurationInterface $configuration = null)

Returns the $originalTargetType unchanged in this implementation.

Parameters

mixed $source

the source data

string $originalTargetType

the type we originally want to convert to

PropertyMappingConfigurationInterface $configuration

Return Value

string

int getPriority()

Return the priority of this TypeConverter. TypeConverters with a high priority are chosen before low priority.

Return Value

int

bool canConvertFrom(mixed $source, string $targetType)

This implementation always returns true for this method.

Parameters

mixed $source

the source data

string $targetType

the type to convert to.

Return Value

bool

true if this TypeConverter can convert from $source to $targetType, false otherwise.

array getSourceChildPropertiesToBeConverted(mixed $source)

Returns an empty list of sub property names

Parameters

mixed $source

Return Value

array

string|null getTypeOfChildProperty(string $targetType, string $propertyName, PropertyMappingConfigurationInterface $configuration)

This method is never called, as getSourceChildPropertiesToBeConverted() returns an empty array.

Parameters

string $targetType
string $propertyName
PropertyMappingConfigurationInterface $configuration

Return Value

string|null

the type of $propertyName in $targetType

mixed|null|Error convertFrom(mixed $source, string $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)

Actually convert from $source to $targetType, by doing a typecast.

Parameters

mixed $source
string $targetType
array $convertedChildProperties
PropertyMappingConfigurationInterface $configuration

Return Value

mixed|null|Error

the target type, or an error object if a user-error occurred

Exceptions

InvalidPropertyMappingConfigurationException

protected float|Error parseUsingLocaleIfConfigured(string $source, PropertyMappingConfigurationInterface $configuration)

Tries to parse the input using the NumberParser.

Parameters

string $source
PropertyMappingConfigurationInterface $configuration

Return Value

float|Error

Parsed float number or error

Exceptions

InvalidPropertyMappingConfigurationException

protected array getConfigurationKeysAndValues(PropertyMappingConfigurationInterface $configuration, array $configurationKeys)

Helper method to collect configuration for this class.

Parameters

PropertyMappingConfigurationInterface $configuration
array $configurationKeys

Return Value

array