class PersistentObjectConverter extends ObjectConverter (View source)

This converter transforms arrays or strings to persistent objects. It does the following:

  • If the input is string, it is assumed to be a UUID. Then, the object is fetched from persistence.

  • If the input is array, we check if it has an identity property.

  • If the input has NO identity property, but additional properties, we create a new object and return it. However, we only do this if the configuration option "CONFIGURATION_CREATION_ALLOWED" is true.

  • If the input has an identity property AND the configuration option "CONFIGURATION_IDENTITY_CREATION_ALLOWED" is set, we fetch the object from persistent or create a new object if none was found and then set the sub-properties.

  • If the input has an identity property and NO additional properties, we fetch the object from persistence.

  • If the input has an identity property AND additional properties, we fetch the object from persistence, and set the sub-properties. We only do this if the configuration option "CONFIGURATION_MODIFICATION_ALLOWED" is true.

Constants

CONFIGURATION_TARGET_TYPE

CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED

PATTERN_MATCH_UUID

CONFIGURATION_MODIFICATION_ALLOWED

CONFIGURATION_CREATION_ALLOWED

CONFIGURATION_IDENTITY_CREATION_ALLOWED

Properties

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

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

from  ObjectConverter
protected PersistenceManagerInterface $persistenceManager

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)

We can only convert if the $targetType is either tagged with entity or value object.

array
getSourceChildPropertiesToBeConverted(mixed $source)

All properties in the source array except __identity are sub-properties.

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 entity or a value 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.

handleArrayData(array $source, class-string $targetType, array $convertedChildProperties, PropertyMappingConfigurationInterface $configuration = null)

Handle the case if $source is an array.

void
setIdentity(object $object, string|array $identity)

Set the given $identity on the created $object.

object|null
fetchObjectFromPersistence(mixed $identity, class-string $targetType)

Fetch an object from persistence layer.

object|null
findObjectByIdentityProperties(array $identityProperties, string $type)

Finds an object from the repository by searching for its identity properties.

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)

We can only convert if the $targetType is either tagged with entity or value object.

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)

All properties in the source array except __identity are sub-properties.

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 entity or a value 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

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

protected object|TargetNotFoundError handleArrayData(array $source, class-string $targetType, array $convertedChildProperties, PropertyMappingConfigurationInterface $configuration = null)

Handle the case if $source is an array.

Parameters

array $source
class-string $targetType
array $convertedChildProperties
PropertyMappingConfigurationInterface $configuration

Return Value

object|TargetNotFoundError

Exceptions

InvalidPropertyMappingConfigurationException

protected void setIdentity(object $object, string|array $identity)

Set the given $identity on the created $object.

set identity properly if it is composite or custom property

Parameters

object $object
string|array $identity

Return Value

void

protected object|null fetchObjectFromPersistence(mixed $identity, class-string $targetType)

Fetch an object from persistence layer.

Parameters

mixed $identity
class-string $targetType

Return Value

object|null

Exceptions

DuplicateObjectException

protected object|null findObjectByIdentityProperties(array $identityProperties, string $type)

Finds an object from the repository by searching for its identity properties.

Parameters

array $identityProperties

Property names and values to search for

string $type

The object type to look for

Return Value

object|null

Either the object matching the identity or NULL if no object was found

Exceptions

DuplicateObjectException