class ObjectConverter extends AbstractTypeConverter (View source)

This converter transforms arrays to simple objects (POPO) by setting properties.

This converter will only be used on target types that are not entities or value objects (for those the PersistentObjectConverter is used).

The target type can be overridden in the source by setting the __type key to the desired value.

The converter will return an instance of the target type with all properties given in the source array set to the respective values. For the mechanics used to set the values see ObjectAccess::setProperty().

Constants

CONFIGURATION_TARGET_TYPE

CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED

Properties

protected string[] $sourceTypes
protected string $targetType
protected int $priority
protected ObjectManagerInterface $objectManager
protected ReflectionService $reflectionService
protected array $constructorReflectionFirstLevelCache

As it is very likely that the constructor arguments are needed twice we should cache them for the request.

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)

Determines the target type based on the source's (optional) __type key.

int
getPriority()

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

bool
canConvertFrom(mixed $source, string $targetType)

Only convert non-persistent types

array
getSourceChildPropertiesToBeConverted(mixed $source)

Convert all properties in the source array

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

The type of a property is determined by the reflection service.

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

Convert an object from $source to an object.

object
buildObject(array $possibleConstructorArgumentValues, string $objectType)

Builds a new instance of $objectType with the given $possibleConstructorArgumentValues.

array[]
getConstructorArgumentsForClass(string $className)

Get the constructor argument reflection for the given object type.

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)

Determines the target type based on the source's (optional) __type key.

Parameters

mixed $source

the source data

string $originalTargetType

the type we originally want to convert to

PropertyMappingConfigurationInterface $configuration

Return Value

string

Exceptions

InvalidDataTypeException
InvalidPropertyMappingConfigurationException
InvalidArgumentException

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)

Only convert non-persistent types

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)

Convert all properties in the source array

Parameters

mixed $source

Return Value

array

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

The type of a property is determined by the reflection service.

Parameters

string $targetType
string $propertyName
PropertyMappingConfigurationInterface $configuration

Return Value

string|null

the type of $propertyName in $targetType

Exceptions

InvalidTargetException

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

Convert an object from $source to an object.

Parameters

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

Return Value

mixed|Error

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

Exceptions

InvalidTargetException
InvalidDataTypeException
InvalidPropertyMappingConfigurationException

protected object buildObject(array $possibleConstructorArgumentValues, string $objectType)

Builds a new instance of $objectType with the given $possibleConstructorArgumentValues.

If constructor argument values are missing from the given array the method looks for a default value in the constructor signature.

Furthermore, the constructor arguments are removed from $possibleConstructorArgumentValues

Parameters

array $possibleConstructorArgumentValues
string $objectType

Return Value

object

The created instance

Exceptions

InvalidTargetException

protected array[] getConstructorArgumentsForClass(string $className)

Get the constructor argument reflection for the given object type.

Parameters

string $className

Return Value

array[]