class StringConverter extends AbstractTypeConverter (View source)

Converter which transforms simple types to a string.

  • If the source is a DateTime instance, it will be formatted as string. The format can be set via CONFIGURATION_DATE_FORMAT.
  • If the source is an array, it will be converted to a CSV string or JSON, depending on CONFIGURATION_ARRAY_FORMAT.

For array to CSV string, the delimiter can be set via CONFIGURATION_CSV_DELIMITER.

Constants

CONFIGURATION_DATE_FORMAT

DEFAULT_DATE_FORMAT

The default date format is "YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00" according to the W3C standard http://www.w3.org/TR/NOTE-datetime.html

CONFIGURATION_ARRAY_FORMAT

DEFAULT_ARRAY_FORMAT

ARRAY_FORMAT_CSV

ARRAY_FORMAT_JSON

CONFIGURATION_CSV_DELIMITER

DEFAULT_CSV_DELIMITER

Properties

protected string[] $sourceTypes
protected string $targetType
protected int $priority

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
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, taking into account the fully built $convertedChildProperties and $configuration.

string
getDateFormat(PropertyMappingConfigurationInterface $configuration = null)

Determines the date format to use for the conversion.

string
getCsvDelimiter(PropertyMappingConfigurationInterface $configuration = null)

Determines the delimiter to use for the conversion from array to CSV format.

string
getArrayFormat(PropertyMappingConfigurationInterface $configuration = null)

Determines the format to use for the conversion from array to string.

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 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

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, taking into account the fully built $convertedChildProperties and $configuration.

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 string getDateFormat(PropertyMappingConfigurationInterface $configuration = null)

Determines the date format to use for the conversion.

If no format is specified in the mapping configuration DEFAULT_DATE_FORMAT is used.

Parameters

PropertyMappingConfigurationInterface $configuration

Return Value

string

Exceptions

InvalidPropertyMappingConfigurationException

protected string getCsvDelimiter(PropertyMappingConfigurationInterface $configuration = null)

Determines the delimiter to use for the conversion from array to CSV format.

If no delimiter is specified in the mapping configuration DEFAULT_CSV_DELIMITER is used.

Parameters

PropertyMappingConfigurationInterface $configuration

Return Value

string

Exceptions

InvalidPropertyMappingConfigurationException

protected string getArrayFormat(PropertyMappingConfigurationInterface $configuration = null)

Determines the format to use for the conversion from array to string.

If no format is specified in the mapping configuration DEFAULT_ARRAY_FORMAT is used.

Parameters

PropertyMappingConfigurationInterface $configuration

Return Value

string

Exceptions

InvalidPropertyMappingConfigurationException