class ObjectNormalizer extends AbstractNormalizer

Converts between objects and arrays using the PropertyAccess component.

Constants

CIRCULAR_REFERENCE_LIMIT

OBJECT_TO_POPULATE

GROUPS

Properties

protected SerializerInterface $serializer from SerializerAwareNormalizer
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 owning Serializer object.

__construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = 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.

setCamelizedAttributes(array $camelizedAttributes) deprecated

Set attributes to be camelized on denormalize.

bool
isCircularReference(object $object, array $context)

Detects if the configured circular reference limit is reached.

mixed
handleCircularReference(object $object)

Handles a circular reference.

string
formatAttribute(string $attributeName) deprecated

Format an attribute name, for example to convert a snake_case name to camelCase.

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

Gets attributes to normalize using groups.

array
prepareForDenormalization(object|array $data)

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

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

Instantiates an object using constructor parameters when needed.

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

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

array|scalar
normalize(mixed $object, string $format = null, array $context = array())

Normalizes an object into a set of arrays/scalars.

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.

Details

setSerializer(SerializerInterface $serializer)

Sets the owning Serializer object.

Parameters

SerializerInterface $serializer

at line 34
__construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null)

Sets the {@link ClassMetadataFactoryInterface} to use.

Parameters

ClassMetadataFactoryInterface $classMetadataFactory
NameConverterInterface $nameConverter
PropertyAccessorInterface $propertyAccessor

in AbstractNormalizer at line 85
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 101
AbstractNormalizer setCircularReferenceHandler(callable $circularReferenceHandler)

Set circular reference handler.

Parameters

callable $circularReferenceHandler

Return Value

AbstractNormalizer

Exceptions

InvalidArgumentException

in AbstractNormalizer at line 121
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 138
AbstractNormalizer setIgnoredAttributes(array $ignoredAttributes)

Set ignored attributes for normalization and denormalization.

Parameters

array $ignoredAttributes

Return Value

AbstractNormalizer

in AbstractNormalizer at line 156
AbstractNormalizer setCamelizedAttributes(array $camelizedAttributes) deprecated

deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.

Set attributes to be camelized on denormalize.

Parameters

array $camelizedAttributes

Return Value

AbstractNormalizer

Exceptions

LogicException

in AbstractNormalizer at line 186
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 217
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 235
protected string formatAttribute(string $attributeName) deprecated

deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.

Format an attribute name, for example to convert a snake_case name to camelCase.

Parameters

string $attributeName

Return Value

string

in AbstractNormalizer at line 251
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

in AbstractNormalizer at line 275
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 298
protected object instantiateObject(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $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
string $class
array $context
ReflectionClass $reflectionClass
array|bool $allowedAttributes

Return Value

object

Exceptions

RuntimeException

at line 48
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

at line 58
array|scalar 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|scalar

at line 102
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

at line 110
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