class ObjectNormalizer extends AbstractObjectNormalizer

Converts between objects and arrays using the PropertyAccess component.

Traits

Constants

CIRCULAR_REFERENCE_LIMIT

OBJECT_TO_POPULATE

GROUPS

ATTRIBUTES

ALLOW_EXTRA_ATTRIBUTES

ENABLE_MAX_DEPTH

DEPTH_KEY_PATTERN

DISABLE_TYPE_ENFORCEMENT

Properties

protected SerializerInterface $serializer from SerializerAwareTrait
protected int $circularReferenceLimit from AbstractNormalizer
protected callable $circularReferenceHandler from AbstractNormalizer
protected ClassMetadataFactoryInterface|null $classMetadataFactory from AbstractNormalizer
protected NameConverterInterface|null $nameConverter from AbstractNormalizer
protected array $callbacks from AbstractNormalizer
protected array $ignoredAttributes from AbstractNormalizer
protected array $camelizedAttributes from AbstractNormalizer
protected $propertyAccessor

Methods

setSerializer(SerializerInterface $serializer)

Sets the serializer.

object|null
extractObjectToPopulate(string $class, array $context, string $key = null)

Extract the object_to_populate field from the context if it exists and is an instance of the provided $class.

__construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)

Sets the {@link ClassMetadataFactoryInterface} to use.

setCircularReferenceLimit(int $circularReferenceLimit)

Set circular reference limit.

setCircularReferenceHandler(callable $circularReferenceHandler)

Set circular reference handler.

setCallbacks(array $callbacks)

Set normalization callbacks.

setIgnoredAttributes(array $ignoredAttributes)

Set ignored attributes for normalization and denormalization.

bool
isCircularReference(object $object, array $context)

Detects if the configured circular reference limit is reached.

mixed
handleCircularReference(object $object)

Handles a circular reference.

string[]|AttributeMetadataInterface[]|bool
getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false)

Gets attributes to normalize using groups.

bool
isAllowedAttribute(object|string $classOrObject, string $attribute, string|null $format = null, array $context = array())

Is this attribute allowed?

array
prepareForDenormalization(object|array $data)

Normalizes the given data to an array. It's particularly useful during the denormalization process.

getConstructor(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $allowedAttributes)

Returns the method to use to construct an object. This method must be either the object constructor or static.

object
instantiateObject(array $data, $class, array $context, ReflectionClass $reflectionClass, $allowedAttributes)

Instantiates an object using constructor parameters when needed.

array
createChildContext(array $parentContext, string $attribute)

No description

bool
supportsNormalization(mixed $data, string $format = null)

Checks whether the given class is supported for normalization by this normalizer.

array|string|int|float|bool
normalize(mixed $object, string $format = null, array $context = array())

Normalizes an object into a set of arrays/scalars.

string[]
getAttributes(object $object, string|null $format = null, array $context)

Gets and caches attributes for the given object, format and context.

string[]
extractAttributes(object $object, string|null $format = null, array $context = array())

Extracts attributes to normalize from the class of the given object, format and context.

mixed
getAttributeValue(object $object, string $attribute, string|null $format = null, array $context = array())

Gets the attribute value.

bool
supportsDenormalization(mixed $data, string $type, string $format = null)

Checks whether the given class is supported for denormalization by this normalizer.

object
denormalize(mixed $data, string $class, string $format = null, array $context = array())

Denormalizes data back into an object of the given class.

setAttributeValue(object $object, string $attribute, mixed $value, string|null $format = null, array $context = array())

Sets attribute value.

Details

in SerializerAwareTrait at line 31
setSerializer(SerializerInterface $serializer)

Sets the serializer.

Parameters

SerializerInterface $serializer A SerializerInterface instance

in ObjectToPopulateTrait at line 27
protected object|null extractObjectToPopulate(string $class, array $context, string $key = null)

Extract the object_to_populate field from the context if it exists and is an instance of the provided $class.

Parameters

string $class The class the object should be
array $context The denormalization context
string $key They in which to look for the object to populate. Keeps backwards compatibility with AbstractNormalizer.

Return Value

object|null an object if things check out, null otherwise

at line 31
__construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)

Sets the {@link ClassMetadataFactoryInterface} to use.

Parameters

ClassMetadataFactoryInterface $classMetadataFactory
NameConverterInterface $nameConverter
PropertyAccessorInterface $propertyAccessor
PropertyTypeExtractorInterface $propertyTypeExtractor

in AbstractNormalizer at line 89
AbstractNormalizer setCircularReferenceLimit(int $circularReferenceLimit)

Set circular reference limit.

Parameters

int $circularReferenceLimit Limit of iterations for the same object

Return Value

AbstractNormalizer

in AbstractNormalizer at line 103
AbstractNormalizer setCircularReferenceHandler(callable $circularReferenceHandler)

Set circular reference handler.

Parameters

callable $circularReferenceHandler

Return Value

AbstractNormalizer

in AbstractNormalizer at line 119
AbstractNormalizer setCallbacks(array $callbacks)

Set normalization callbacks.

Parameters

array $callbacks Help normalize the result

Return Value

AbstractNormalizer

Exceptions

InvalidArgumentException if a non-callable callback is set

in AbstractNormalizer at line 136
AbstractNormalizer setIgnoredAttributes(array $ignoredAttributes)

Set ignored attributes for normalization and denormalization.

Parameters

array $ignoredAttributes

Return Value

AbstractNormalizer

in AbstractNormalizer at line 153
protected bool isCircularReference(object $object, array $context)

Detects if the configured circular reference limit is reached.

Parameters

object $object
array $context

Return Value

bool

Exceptions

CircularReferenceException

in AbstractNormalizer at line 184
protected mixed handleCircularReference(object $object)

Handles a circular reference.

If a circular reference handler is set, it will be called. Otherwise, a {@class CircularReferenceException} will be thrown.

Parameters

object $object

Return Value

mixed

Exceptions

CircularReferenceException

in AbstractNormalizer at line 204
protected string[]|AttributeMetadataInterface[]|bool getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false)

Gets attributes to normalize using groups.

Parameters

string|object $classOrObject
array $context
bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}

Return Value

string[]|AttributeMetadataInterface[]|bool

Exceptions

LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided

in AbstractNormalizer at line 246
protected bool isAllowedAttribute(object|string $classOrObject, string $attribute, string|null $format = null, array $context = array())

Is this attribute allowed?

Parameters

object|string $classOrObject
string $attribute
string|null $format
array $context

Return Value

bool

in AbstractNormalizer at line 272
protected array prepareForDenormalization(object|array $data)

Normalizes the given data to an array. It's particularly useful during the denormalization process.

Parameters

object|array $data

Return Value

array

in AbstractNormalizer at line 289
protected ReflectionMethod|null getConstructor(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $allowedAttributes)

Returns the method to use to construct an object. This method must be either the object constructor or static.

Parameters

array $data
string $class
array $context
ReflectionClass $reflectionClass
array|bool $allowedAttributes

Return Value

ReflectionMethod|null

in AbstractNormalizer at line 313
protected object instantiateObject(array $data, $class, array $context, ReflectionClass $reflectionClass, $allowedAttributes)

Instantiates an object using constructor parameters when needed.

This method also allows to denormalize data into an existing object if it is present in the context with the object_to_populate. This object is removed from the context before being returned to avoid side effects when recursively normalizing an object graph.

Parameters

array $data
$class
array $context
ReflectionClass $reflectionClass
$allowedAttributes

Return Value

object

Exceptions

RuntimeException

in AbstractNormalizer at line 401
protected array createChildContext(array $parentContext, string $attribute)

Parameters

array $parentContext
string $attribute

Return Value

array

bool supportsNormalization(mixed $data, string $format = null)

Checks whether the given class is supported for normalization by this normalizer.

Parameters

mixed $data Data to normalize
string $format The format being (de-)serialized from or into

Return Value

bool

array|string|int|float|bool normalize(mixed $object, string $format = null, array $context = array())

Normalizes an object into a set of arrays/scalars.

Parameters

mixed $object Object to normalize
string $format Format the normalization result will be encoded as
array $context Context options for the normalizer

Return Value

array|string|int|float|bool

Exceptions

InvalidArgumentException Occurs when the object given is not an attempted type for the normalizer
CircularReferenceException Occurs when the normalizer detects a circular reference when no circular reference handler can fix it
LogicException Occurs when the normalizer is not called in an expected context

protected string[] getAttributes(object $object, string|null $format = null, array $context)

Gets and caches attributes for the given object, format and context.

Parameters

object $object
string|null $format
array $context

Return Value

string[]

at line 45
protected string[] extractAttributes(object $object, string|null $format = null, array $context = array())

Extracts attributes to normalize from the class of the given object, format and context.

Parameters

object $object
string|null $format
array $context

Return Value

string[]

at line 101
protected mixed getAttributeValue(object $object, string $attribute, string|null $format = null, array $context = array())

Gets the attribute value.

Parameters

object $object
string $attribute
string|null $format
array $context

Return Value

mixed

bool supportsDenormalization(mixed $data, string $type, string $format = null)

Checks whether the given class is supported for denormalization by this normalizer.

Parameters

mixed $data Data to denormalize from
string $type The class to which the data should be denormalized
string $format The format being deserialized from

Return Value

bool

object denormalize(mixed $data, string $class, string $format = null, array $context = array())

Denormalizes data back into an object of the given class.

Parameters

mixed $data Data to restore
string $class The expected class to instantiate
string $format Format the given data was extracted from
array $context Options available to the denormalizer

Return Value

object

Exceptions

BadMethodCallException Occurs when the normalizer is not called in an expected context
InvalidArgumentException Occurs when the arguments are not coherent or not supported
UnexpectedValueException Occurs when the item cannot be hydrated with the given data
ExtraAttributesException Occurs when the item doesn't have attribute to receive given data
LogicException Occurs when the normalizer is not supposed to denormalize
RuntimeException Occurs if the class cannot be instantiated

at line 109
protected setAttributeValue(object $object, string $attribute, mixed $value, string|null $format = null, array $context = array())

Sets attribute value.

Parameters

object $object
string $attribute
mixed $value
string|null $format
array $context