Documentation

JsonSerializableNormalizer extends AbstractNormalizer
in package

A normalizer that uses an objects own JsonSerializable implementation.

Tags
author

Fred Cox mcfedr@gmail.com

Table of Contents

Constants

ALLOW_EXTRA_ATTRIBUTES  = 'allow_extra_attributes'
If ATTRIBUTES are specified, and the source has fields that are not part of that list, either ignore those attributes (true) or throw an ExtraAttributesException (false).
ATTRIBUTES  = 'attributes'
Limit (de)normalize to the specified names.
CALLBACKS  = 'callbacks'
Hashmap of field name => callable to (de)normalize this field.
CIRCULAR_REFERENCE_HANDLER  = 'circular_reference_handler'
Handler to call when a circular reference has been detected.
CIRCULAR_REFERENCE_LIMIT  = 'circular_reference_limit'
How many loops of circular reference to allow while normalizing.
DEFAULT_CONSTRUCTOR_ARGUMENTS  = 'default_constructor_arguments'
Hashmap of default values for constructor arguments.
GROUPS  = 'groups'
Only (de)normalize attributes that are in the specified groups.
IGNORED_ATTRIBUTES  = 'ignored_attributes'
Skip the specified attributes when normalizing an object tree.
OBJECT_TO_POPULATE  = 'object_to_populate'
Instead of creating a new instance of an object, update the specified object.

Properties

$classMetadataFactory  : ClassMetadataFactoryInterface|null
$defaultContext  : mixed
$nameConverter  : NameConverterInterface|null
$serializer  : SerializerInterface

Methods

__construct()  : mixed
Sets the {@link ClassMetadataFactoryInterface} to use.
denormalize()  : mixed
{@inheritdoc}
hasCacheableSupportsMethod()  : bool
normalize()  : array<string|int, mixed>|string|int|float|bool|ArrayObject|null
{@inheritdoc}
setSerializer()  : mixed
supportsDenormalization()  : bool
{@inheritdoc}
supportsNormalization()  : bool
{@inheritdoc}
applyCallbacks()  : mixed
extractObjectToPopulate()  : object|null
Extract the `object_to_populate` field from the context if it exists and is an instance of the provided $class.
getAllowedAttributes()  : array<string|int, string>|array<string|int, AttributeMetadataInterface>|bool
Gets attributes to normalize using groups.
getConstructor()  : ReflectionMethod|null
Returns the method to use to construct an object. This method must be either the object constructor or static.
getGroups()  : array<string|int, mixed>
handleCircularReference()  : mixed
Handles a circular reference.
instantiateObject()  : object
Instantiates an object using constructor parameters when needed.
isAllowedAttribute()  : bool
Is this attribute allowed?
isCircularReference()  : bool
Detects if the configured circular reference limit is reached.
prepareForDenormalization()  : array<string|int, mixed>
Normalizes the given data to an array. It's particularly useful during the denormalization process.
validateCallbackContext()  : void
Validate callbacks set in context.

Constants

ALLOW_EXTRA_ATTRIBUTES

If ATTRIBUTES are specified, and the source has fields that are not part of that list, either ignore those attributes (true) or throw an ExtraAttributesException (false).

public mixed ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes'

ATTRIBUTES

Limit (de)normalize to the specified names.

public mixed ATTRIBUTES = 'attributes'

For nested structures, this list needs to reflect the object tree.

CALLBACKS

Hashmap of field name => callable to (de)normalize this field.

public mixed CALLBACKS = 'callbacks'

The callable is called if the field is encountered with the arguments:

  • mixed $attributeValue value of this field
  • object|string $object the whole object being normalized or the object's class being denormalized
  • string $attributeName name of the attribute being (de)normalized
  • string $format the requested format
  • array $context the serialization context

CIRCULAR_REFERENCE_HANDLER

Handler to call when a circular reference has been detected.

public mixed CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler'

If you specify no handler, a CircularReferenceException is thrown.

The method will be called with ($object, $format, $context) and its return value is returned as the result of the normalize call.

CIRCULAR_REFERENCE_LIMIT

How many loops of circular reference to allow while normalizing.

public mixed CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit'

The default value of 1 means that when we encounter the same object a second time, we consider that a circular reference.

You can raise this value for special cases, e.g. in combination with the max depth setting of the object normalizer.

DEFAULT_CONSTRUCTOR_ARGUMENTS

Hashmap of default values for constructor arguments.

public mixed DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments'

The names need to match the parameter names in the constructor arguments.

GROUPS

Only (de)normalize attributes that are in the specified groups.

public mixed GROUPS = 'groups'

IGNORED_ATTRIBUTES

Skip the specified attributes when normalizing an object tree.

public mixed IGNORED_ATTRIBUTES = 'ignored_attributes'

This list is applied to each element of nested structures.

Note: The behaviour for nested structures is different from ATTRIBUTES for historical reason. Aligning the behaviour would be a BC break.

OBJECT_TO_POPULATE

Instead of creating a new instance of an object, update the specified object.

public mixed OBJECT_TO_POPULATE = 'object_to_populate'

If you have a nested structure, child objects will be overwritten with new instances unless you set DEEP_OBJECT_TO_POPULATE to true.

Properties

$defaultContext

protected mixed $defaultContext = [self::ALLOW_EXTRA_ATTRIBUTES => true, self::CIRCULAR_REFERENCE_HANDLER => null, self::CIRCULAR_REFERENCE_LIMIT => 1, self::IGNORED_ATTRIBUTES => []]

Methods

denormalize()

{@inheritdoc}

public denormalize(mixed $data, string $type[, string $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
$data : mixed
$type : string
$format : string = null
$context : array<string|int, mixed> = []

normalize()

{@inheritdoc}

public normalize(mixed $object[, string $format = null ][, array<string|int, mixed> $context = [] ]) : array<string|int, mixed>|string|int|float|bool|ArrayObject|null
Parameters
$object : mixed
$format : string = null
$context : array<string|int, mixed> = []
Return values
array<string|int, mixed>|string|int|float|bool|ArrayObject|null

supportsDenormalization()

{@inheritdoc}

public supportsDenormalization(mixed $data, string $type[, string $format = null ]) : bool
Parameters
$data : mixed
$type : string
$format : string = null
Return values
bool

supportsNormalization()

{@inheritdoc}

public supportsNormalization(mixed $data[, string $format = null ]) : bool
Parameters
$data : mixed
$format : string = null
Return values
bool

applyCallbacks()

protected final applyCallbacks(mixed $value, object|string $object, string $attribute, string|null $format, array<string|int, mixed> $context) : mixed
Parameters
$value : mixed
$object : object|string
$attribute : string
$format : string|null
$context : array<string|int, mixed>

extractObjectToPopulate()

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

protected extractObjectToPopulate(string $class, array<string|int, mixed> $context[, string|null $key = null ]) : object|null
Parameters
$class : string

The class the object should be

$context : array<string|int, mixed>
$key : string|null = null

They in which to look for the object to populate. Keeps backwards compatibility with AbstractNormalizer.

Return values
object|null

getAllowedAttributes()

Gets attributes to normalize using groups.

protected getAllowedAttributes(string|object $classOrObject, array<string|int, mixed> $context[, bool $attributesAsString = false ]) : array<string|int, string>|array<string|int, AttributeMetadataInterface>|bool
Parameters
$classOrObject : string|object
$context : array<string|int, mixed>
$attributesAsString : bool = false

If false, return an array of

Tags
throws
LogicException

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

Return values
array<string|int, string>|array<string|int, AttributeMetadataInterface>|bool

getConstructor()

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

protected getConstructor(array<string|int, mixed> &$data, string $class, array<string|int, mixed> &$context, ReflectionClass $reflectionClass, array<string|int, mixed>|bool $allowedAttributes) : ReflectionMethod|null
Parameters
$data : array<string|int, mixed>
$class : string
$context : array<string|int, mixed>
$reflectionClass : ReflectionClass
$allowedAttributes : array<string|int, mixed>|bool
Return values
ReflectionMethod|null

getGroups()

protected getGroups(array<string|int, mixed> $context) : array<string|int, mixed>
Parameters
$context : array<string|int, mixed>
Return values
array<string|int, mixed>

handleCircularReference()

Handles a circular reference.

protected handleCircularReference(object $object[, string $format = null ][, array<string|int, mixed> $context = [] ]) : mixed

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

Parameters
$object : object
$format : string = null
$context : array<string|int, mixed> = []
Tags
final
throws
CircularReferenceException

instantiateObject()

Instantiates an object using constructor parameters when needed.

protected instantiateObject(array<string|int, mixed> &$data, string $class, array<string|int, mixed> &$context, ReflectionClass $reflectionClass, array<string|int, mixed>|bool $allowedAttributes[, string $format = null ]) : object

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
$data : array<string|int, mixed>
$class : string
$context : array<string|int, mixed>
$reflectionClass : ReflectionClass
$allowedAttributes : array<string|int, mixed>|bool
$format : string = null
Tags
throws
RuntimeException
throws
MissingConstructorArgumentsException
Return values
object

isAllowedAttribute()

Is this attribute allowed?

protected isAllowedAttribute(object|string $classOrObject, string $attribute[, string $format = null ][, array<string|int, mixed> $context = [] ]) : bool
Parameters
$classOrObject : object|string
$attribute : string
$format : string = null
$context : array<string|int, mixed> = []
Return values
bool

isCircularReference()

Detects if the configured circular reference limit is reached.

protected isCircularReference(object $object, array<string|int, mixed> &$context) : bool
Parameters
$object : object
$context : array<string|int, mixed>
Tags
throws
CircularReferenceException
Return values
bool

prepareForDenormalization()

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

protected prepareForDenormalization(mixed $data) : array<string|int, mixed>
Parameters
$data : mixed
Return values
array<string|int, mixed>

validateCallbackContext()

Validate callbacks set in context.

protected final validateCallbackContext(array<string|int, mixed> $context[, string $contextType = '' ]) : void
Parameters
$context : array<string|int, mixed>
$contextType : string = ''

Used to specify which context is invalid in exceptions

Tags
throws
InvalidArgumentException

        
On this page

Search results