AbstractObjectNormalizer
extends AbstractNormalizer
in package
Base class for a normalizer dealing with objects.
Tags
Table of Contents
- 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 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.
- DEEP_OBJECT_TO_POPULATE = 'deep_object_to_populate'
- Flag to tell the denormalizer to also populate existing objects on attributes of the main object.
- DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments'
- Hashmap of default values for constructor arguments.
- DEPTH_KEY_PATTERN = 'depth_%s::%s'
- How to track the current depth in the context.
- DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement'
- While denormalizing, we can verify that types match.
- ENABLE_MAX_DEPTH = 'enable_max_depth'
- Set to true to respect the max depth metadata on fields.
- EXCLUDE_FROM_CACHE_KEY = 'exclude_from_cache_key'
- Specify which context key are not relevant to determine which attributes of an object to (de)normalize.
- 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.
- MAX_DEPTH_HANDLER = 'max_depth_handler'
- Callback to allow to set a value for an attribute when the max depth has been reached.
- OBJECT_TO_POPULATE = 'object_to_populate'
- Instead of creating a new instance of an object, update the specified object.
- PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects'
- SKIP_NULL_VALUES = 'skip_null_values'
- Flag to control whether fields with the value `null` should be output when normalizing or omitted.
- $callbacks : mixed
- $camelizedAttributes : mixed
- $circularReferenceHandler : callable|null
- $circularReferenceLimit : mixed
- $classDiscriminatorResolver : ClassDiscriminatorResolverInterface|null
- $classMetadataFactory : ClassMetadataFactoryInterface|null
- $defaultContext : mixed
- $ignoredAttributes : mixed
- $nameConverter : NameConverterInterface|null
- $serializer : SerializerInterface
- $attributesCache : mixed
- $maxDepthHandler : callable|null
- $objectClassResolver : mixed
- $propertyTypeExtractor : mixed
- $typesCache : mixed
- __construct() : mixed
- Sets the {@link ClassMetadataFactoryInterface} to use.
- denormalize() : mixed
- {@inheritdoc}
- hasCacheableSupportsMethod() : bool
- normalize() : mixed
- {@inheritdoc}
- setCallbacks() : self
- Sets normalization callbacks.
- setCircularReferenceHandler() : self
- Sets circular reference handler.
- setCircularReferenceLimit() : self
- Sets circular reference limit.
- setIgnoredAttributes() : self
- Sets ignored attributes for normalization and denormalization.
- setMaxDepthHandler() : void
- Sets a handler function that will be called when the max depth is reached.
- setSerializer() : mixed
- supportsDenormalization() : mixed
- {@inheritdoc}
- supportsNormalization() : mixed
- {@inheritdoc}
- extractAttributes() : array<string|int, string>
- Extracts attributes to normalize from the class of the given object, format and context.
- 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.
- getAttributes() : array<string|int, string>
- Gets and caches attributes for the given object, format and context.
- getAttributeValue() : mixed
- Gets the attribute value.
- getConstructor() : ReflectionMethod|null
- Returns the method to use to construct an object. This method must be either the object constructor or static.
- 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.
- setAttributeValue() : mixed
- Sets attribute value.
- getCacheKey() : bool|string
- Builds the cache key for the attributes cache.
- getTypes() : array<string|int, Type>|null
- isMaxDepthReached() : bool
- Is the max depth reached for the given attribute?
- updateData() : array<string|int, mixed>
- Sets an attribute and apply the name converter if necessary.
- validateAndDenormalize() : mixed
- Validates the submitted data and denormalizes it.
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 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 $object the whole object being normalized
- string $attributeName name of the attribute being 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.
DEEP_OBJECT_TO_POPULATE
Flag to tell the denormalizer to also populate existing objects on attributes of the main object.
public
mixed
DEEP_OBJECT_TO_POPULATE
= 'deep_object_to_populate'
Setting this to true is only useful if you also specify the root object in OBJECT_TO_POPULATE.
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.
DEPTH_KEY_PATTERN
How to track the current depth in the context.
public
mixed
DEPTH_KEY_PATTERN
= 'depth_%s::%s'
DISABLE_TYPE_ENFORCEMENT
While denormalizing, we can verify that types match.
public
mixed
DISABLE_TYPE_ENFORCEMENT
= 'disable_type_enforcement'
You can disable this by setting this flag to true.
ENABLE_MAX_DEPTH
Set to true to respect the max depth metadata on fields.
public
mixed
ENABLE_MAX_DEPTH
= 'enable_max_depth'
EXCLUDE_FROM_CACHE_KEY
Specify which context key are not relevant to determine which attributes of an object to (de)normalize.
public
mixed
EXCLUDE_FROM_CACHE_KEY
= 'exclude_from_cache_key'
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.
MAX_DEPTH_HANDLER
Callback to allow to set a value for an attribute when the max depth has been reached.
public
mixed
MAX_DEPTH_HANDLER
= 'max_depth_handler'
If no callback is given, the attribute is skipped. If a callable is given, its return value is used (even if null).
The arguments are:
- mixed $attributeValue value of this field
- object $object the whole object being normalized
- string $attributeName name of the attribute being normalized
- string $format the requested format
- array $context the serialization context
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.
PRESERVE_EMPTY_OBJECTS
public
mixed
PRESERVE_EMPTY_OBJECTS
= 'preserve_empty_objects'
SKIP_NULL_VALUES
Flag to control whether fields with the value `null` should be output when normalizing or omitted.
public
mixed
SKIP_NULL_VALUES
= 'skip_null_values'
Properties
$callbacks
protected
mixed
$callbacks
= []
Tags
$camelizedAttributes
protected
mixed
$camelizedAttributes
= []
Tags
$circularReferenceHandler
protected
callable|null
$circularReferenceHandler
Tags
$circularReferenceLimit
protected
mixed
$circularReferenceLimit
= 1
Tags
$classDiscriminatorResolver
protected
ClassDiscriminatorResolverInterface|null
$classDiscriminatorResolver
$classMetadataFactory
protected
ClassMetadataFactoryInterface|null
$classMetadataFactory
$defaultContext
protected
mixed
$defaultContext
= [self::ALLOW_EXTRA_ATTRIBUTES => true, self::CIRCULAR_REFERENCE_LIMIT => 1, self::IGNORED_ATTRIBUTES => []]
$ignoredAttributes
protected
mixed
$ignoredAttributes
= []
Tags
$nameConverter
protected
NameConverterInterface|null
$nameConverter
$serializer
protected
SerializerInterface
$serializer
$attributesCache
private
mixed
$attributesCache
= []
$maxDepthHandler
private
callable|null
$maxDepthHandler
Tags
$objectClassResolver
private
mixed
$objectClassResolver
$propertyTypeExtractor
private
mixed
$propertyTypeExtractor
$typesCache
private
mixed
$typesCache
= []
Methods
__construct()
Sets the {@link ClassMetadataFactoryInterface} to use.
public
__construct([ClassMetadataFactoryInterface $classMetadataFactory = null ][, NameConverterInterface $nameConverter = null ][, PropertyTypeExtractorInterface $propertyTypeExtractor = null ][, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null ][, callable $objectClassResolver = null ][, array<string|int, mixed> $defaultContext = [] ]) : mixed
Parameters
- $classMetadataFactory : ClassMetadataFactoryInterface = null
- $nameConverter : NameConverterInterface = null
- $propertyTypeExtractor : PropertyTypeExtractorInterface = null
- $classDiscriminatorResolver : ClassDiscriminatorResolverInterface = null
- $objectClassResolver : callable = null
- $defaultContext : array<string|int, mixed> = []
Return values
mixed —denormalize()
{@inheritdoc}
public
denormalize(mixed $data, mixed $type[, mixed $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $data : mixed
- $type : mixed
- $format : mixed = null
- $context : array<string|int, mixed> = []
Return values
mixed —hasCacheableSupportsMethod()
public
hasCacheableSupportsMethod() : bool
Return values
bool —normalize()
{@inheritdoc}
public
normalize(mixed $object[, mixed $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $object : mixed
- $format : mixed = null
- $context : array<string|int, mixed> = []
Return values
mixed —setCallbacks()
Sets normalization callbacks.
public
setCallbacks(array<string|int, callable> $callbacks) : self
Parameters
- $callbacks : array<string|int, callable>
-
Help normalize the result
Tags
Return values
self —setCircularReferenceHandler()
Sets circular reference handler.
public
setCircularReferenceHandler(callable $circularReferenceHandler) : self
Parameters
- $circularReferenceHandler : callable
Tags
Return values
self —setCircularReferenceLimit()
Sets circular reference limit.
public
setCircularReferenceLimit(int $circularReferenceLimit) : self
Parameters
- $circularReferenceLimit : int
-
Limit of iterations for the same object
Tags
Return values
self —setIgnoredAttributes()
Sets ignored attributes for normalization and denormalization.
public
setIgnoredAttributes(array<string|int, mixed> $ignoredAttributes) : self
Parameters
- $ignoredAttributes : array<string|int, mixed>
Tags
Return values
self —setMaxDepthHandler()
Sets a handler function that will be called when the max depth is reached.
public
setMaxDepthHandler(callable|null $handler) : void
Parameters
- $handler : callable|null
Tags
Return values
void —setSerializer()
public
setSerializer(SerializerInterface $serializer) : mixed
Parameters
- $serializer : SerializerInterface
Return values
mixed —supportsDenormalization()
{@inheritdoc}
public
supportsDenormalization(mixed $data, mixed $type[, mixed $format = null ]) : mixed
Parameters
- $data : mixed
- $type : mixed
- $format : mixed = null
Return values
mixed —supportsNormalization()
{@inheritdoc}
public
supportsNormalization(mixed $data[, mixed $format = null ]) : mixed
Parameters
- $data : mixed
- $format : mixed = null
Return values
mixed —extractAttributes()
Extracts attributes to normalize from the class of the given object, format and context.
protected
abstract extractAttributes(object $object[, string|null $format = null ][, array<string|int, mixed> $context = [] ]) : array<string|int, string>
Parameters
- $object : object
- $format : string|null = null
- $context : array<string|int, mixed> = []
Return values
array<string|int, string> —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 —an object if things check out, null otherwise
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
-
Tags
Return values
array<string|int, string>|array<string|int, AttributeMetadataInterface>|bool —getAttributes()
Gets and caches attributes for the given object, format and context.
protected
getAttributes(object $object, string|null $format, array<string|int, mixed> $context) : array<string|int, string>
Parameters
- $object : object
- $format : string|null
- $context : array<string|int, mixed>
Return values
array<string|int, string> —getAttributeValue()
Gets the attribute value.
protected
abstract getAttributeValue(object $object, string $attribute[, string|null $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $object : object
- $attribute : string
- $format : string|null = null
- $context : array<string|int, mixed> = []
Return values
mixed —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 —handleCircularReference()
Handles a circular reference.
protected
handleCircularReference(object $object) : mixed
If a circular reference handler is set, it will be called. Otherwise, a CircularReferenceException will be thrown.
Parameters
- $object : object
Tags
Return values
mixed —instantiateObject()
Instantiates an object using constructor parameters when needed.
protected
instantiateObject(array<string|int, mixed> &$data, mixed $class, array<string|int, mixed> &$context, ReflectionClass $reflectionClass, mixed $allowedAttributes[, string $format = null ]) : object
Parameters
- $data : array<string|int, mixed>
- $class : mixed
- $context : array<string|int, mixed>
- $reflectionClass : ReflectionClass
- $allowedAttributes : mixed
- $format : string = null
Return values
object —isAllowedAttribute()
Is this attribute allowed?
protected
isAllowedAttribute(object|string $classOrObject, string $attribute[, string|null $format = null ][, array<string|int, mixed> $context = [] ]) : bool
Parameters
- $classOrObject : object|string
- $attribute : string
- $format : string|null = 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
Return values
bool —prepareForDenormalization()
Normalizes the given data to an array. It's particularly useful during the denormalization process.
protected
prepareForDenormalization(object|array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : object|array<string|int, mixed>
Return values
array<string|int, mixed> —setAttributeValue()
Sets attribute value.
protected
abstract setAttributeValue(object $object, string $attribute, mixed $value[, string|null $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $object : object
- $attribute : string
- $value : mixed
- $format : string|null = null
- $context : array<string|int, mixed> = []
Return values
mixed —getCacheKey()
Builds the cache key for the attributes cache.
private
getCacheKey(string|null $format, array<string|int, mixed> $context) : bool|string
The key must be different for every option in the context that could change which attributes should be handled.
Parameters
- $format : string|null
- $context : array<string|int, mixed>
Return values
bool|string —getTypes()
private
getTypes(string $currentClass, string $attribute) : array<string|int, Type>|null
Parameters
- $currentClass : string
- $attribute : string
Return values
array<string|int, Type>|null —isMaxDepthReached()
Is the max depth reached for the given attribute?
private
isMaxDepthReached(array<string|int, AttributeMetadataInterface> $attributesMetadata, string $class, string $attribute, array<string|int, mixed> &$context) : bool
Parameters
- $attributesMetadata : array<string|int, AttributeMetadataInterface>
- $class : string
- $attribute : string
- $context : array<string|int, mixed>
Return values
bool —updateData()
Sets an attribute and apply the name converter if necessary.
private
updateData(array<string|int, mixed> $data, string $attribute, mixed $attributeValue, string $class, string|null $format, array<string|int, mixed> $context) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
- $attribute : string
- $attributeValue : mixed
- $class : string
- $format : string|null
- $context : array<string|int, mixed>
Return values
array<string|int, mixed> —validateAndDenormalize()
Validates the submitted data and denormalizes it.
private
validateAndDenormalize(string $currentClass, string $attribute, mixed $data, string|null $format, array<string|int, mixed> $context) : mixed
Parameters
- $currentClass : string
- $attribute : string
- $data : mixed
- $format : string|null
- $context : array<string|int, mixed>