GetSetMethodNormalizer
extends AbstractObjectNormalizer
in package
Converts between objects with getter and setter methods and arrays.
The normalization process looks at all public methods and calls the ones which have a name starting with get and take no parameters. The result is a map from property names (method name stripped of the get prefix and converted to lower case) to property values. Property values are normalized through the serializer.
The denormalization first looks at the constructor of the given class to see if any of the parameters have the same name as one of the properties. The constructor is then called with all parameters or an exception is thrown if any required parameters were not present as properties. Then the denormalizer walks through the given map of property names to property values to see if a setter method exists for any of the properties. If a setter exists it is called with the property value. No automatic denormalization of the value takes place.
Tags
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.
- 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'
- Flag to control whether an empty object should be kept as an object (in JSON: {}) or converted to a list (in JSON: []).
- SKIP_NULL_VALUES = 'skip_null_values'
- Flag to control whether fields with the value `null` should be output when normalizing or omitted.
- SKIP_UNINITIALIZED_VALUES = 'skip_uninitialized_values'
- Flag to control whether uninitialized PHP>=7.4 typed class properties should be excluded when normalizing.
Properties
- $classDiscriminatorResolver : ClassDiscriminatorResolverInterface|null
- $classMetadataFactory : ClassMetadataFactoryInterface|null
- $defaultContext : mixed
- $nameConverter : NameConverterInterface|null
- $serializer : SerializerInterface
- $attributesCache : mixed
- $objectClassResolver : mixed
- $propertyTypeExtractor : mixed
- $setterAccessibleCache : mixed
- $typesCache : mixed
Methods
- __construct() : mixed
- Sets the {@link ClassMetadataFactoryInterface} to use.
- denormalize() : mixed
- {@inheritdoc}
- hasCacheableSupportsMethod() : bool
- {@inheritdoc}
- normalize() : mixed
- {@inheritdoc}
- setSerializer() : mixed
- supportsDenormalization() : bool
- {@inheritdoc}
- supportsNormalization() : bool
- {@inheritdoc}
- applyCallbacks() : mixed
- 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.
- 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.
- setAttributeValue() : mixed
- Sets attribute value.
- validateCallbackContext() : void
- Validate callbacks set in context.
- getAttributeDenormalizationContext() : array<string|int, mixed>
- Computes the denormalization context merged with current one. Metadata always wins over global context, as more specific.
- getAttributeMetadata() : AttributeMetadataInterface|null
- getAttributeNormalizationContext() : array<string|int, mixed>
- Computes the normalization context merged with current one. Metadata always wins over global context, as more specific.
- getCacheKey() : bool|string
- Builds the cache key for the attributes cache.
- getTypes() : array<string|int, Type>|null
- isGetMethod() : bool
- Checks if a method's name matches /^(get|is|has).+$/ and can be called non-statically without parameters.
- isMaxDepthReached() : bool
- Is the max depth reached for the given attribute?
- isUninitializedValueError() : bool
- This error may occur when specific object normalizer implementation gets attribute value by accessing a public uninitialized property or by calling a method accessing such property.
- supports() : bool
- Checks if the given class has any getter method.
- 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 (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.
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
Flag to control whether an empty object should be kept as an object (in JSON: {}) or converted to a list (in JSON: []).
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'
SKIP_UNINITIALIZED_VALUES
Flag to control whether uninitialized PHP>=7.4 typed class properties should be excluded when normalizing.
public
mixed
SKIP_UNINITIALIZED_VALUES
= 'skip_uninitialized_values'
Properties
$classDiscriminatorResolver
protected
ClassDiscriminatorResolverInterface|null
$classDiscriminatorResolver
$classMetadataFactory
protected
ClassMetadataFactoryInterface|null
$classMetadataFactory
$defaultContext
protected
mixed
$defaultContext
= [self::ALLOW_EXTRA_ATTRIBUTES => true, self::CIRCULAR_REFERENCE_HANDLER => null, self::CIRCULAR_REFERENCE_LIMIT => 1, self::IGNORED_ATTRIBUTES => []]
$nameConverter
protected
NameConverterInterface|null
$nameConverter
$serializer
protected
SerializerInterface
$serializer
$attributesCache
private
mixed
$attributesCache
= []
$objectClassResolver
private
mixed
$objectClassResolver
$propertyTypeExtractor
private
mixed
$propertyTypeExtractor
$setterAccessibleCache
private
static mixed
$setterAccessibleCache
= []
$typesCache
private
mixed
$typesCache
= []
Methods
__construct()
Sets the {@link ClassMetadataFactoryInterface} to use.
public
__construct([ClassMetadataFactoryInterface $classMetadataFactory = null ][, NameConverterInterface $nameConverter = null ][, array<string|int, mixed> $defaultContext = [] ]) : mixed
Parameters
- $classMetadataFactory : ClassMetadataFactoryInterface = null
- $nameConverter : NameConverterInterface = null
- $defaultContext : array<string|int, mixed> = []
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> = []
hasCacheableSupportsMethod()
{@inheritdoc}
public
hasCacheableSupportsMethod() : bool
Return values
boolnormalize()
{@inheritdoc}
public
normalize(mixed $object[, string $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $object : mixed
- $format : string = null
- $context : array<string|int, mixed> = []
setSerializer()
public
setSerializer(SerializerInterface $serializer) : mixed
Parameters
- $serializer : SerializerInterface
supportsDenormalization()
{@inheritdoc}
public
supportsDenormalization(mixed $data, string $type[, string $format = null ]) : bool
Parameters
- $data : mixed
- $type : string
- $format : string = null
Return values
boolsupportsNormalization()
{@inheritdoc}
public
supportsNormalization(mixed $data[, string $format = null ]) : bool
Parameters
- $data : mixed
- $format : string = null
Return values
boolapplyCallbacks()
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>
extractAttributes()
Extracts attributes to normalize from the class of the given object, format and context.
protected
extractAttributes(object $object[, string $format = null ][, 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>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|nullgetAllowedAttributes()
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>|boolgetAttributes()
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
getAttributeValue(object $object, string $attribute[, string $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $object : object
- $attribute : string
- $format : string = null
- $context : array<string|int, 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|nullgetGroups()
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
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
Return values
objectisAllowedAttribute()
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
boolisCircularReference()
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
boolprepareForDenormalization()
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>setAttributeValue()
Sets attribute value.
protected
setAttributeValue(object $object, string $attribute, mixed $value[, string $format = null ][, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $object : object
- $attribute : string
- $value : mixed
- $format : string = null
- $context : 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
getAttributeDenormalizationContext()
Computes the denormalization context merged with current one. Metadata always wins over global context, as more specific.
private
getAttributeDenormalizationContext(string $class, string $attribute, array<string|int, mixed> $context) : array<string|int, mixed>
Parameters
- $class : string
- $attribute : string
- $context : array<string|int, mixed>
Return values
array<string|int, mixed>getAttributeMetadata()
private
getAttributeMetadata(object|string $objectOrClass, string $attribute) : AttributeMetadataInterface|null
Parameters
- $objectOrClass : object|string
- $attribute : string
Return values
AttributeMetadataInterface|nullgetAttributeNormalizationContext()
Computes the normalization context merged with current one. Metadata always wins over global context, as more specific.
private
getAttributeNormalizationContext(object $object, string $attribute, array<string|int, mixed> $context) : array<string|int, mixed>
Parameters
- $object : object
- $attribute : string
- $context : array<string|int, mixed>
Return values
array<string|int, 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|stringgetTypes()
private
getTypes(string $currentClass, string $attribute) : array<string|int, Type>|null
Parameters
- $currentClass : string
- $attribute : string
Return values
array<string|int, Type>|nullisGetMethod()
Checks if a method's name matches /^(get|is|has).+$/ and can be called non-statically without parameters.
private
isGetMethod(ReflectionMethod $method) : bool
Parameters
- $method : ReflectionMethod
Return values
boolisMaxDepthReached()
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
boolisUninitializedValueError()
This error may occur when specific object normalizer implementation gets attribute value by accessing a public uninitialized property or by calling a method accessing such property.
private
isUninitializedValueError(Error $e) : bool
Parameters
- $e : Error
Return values
boolsupports()
Checks if the given class has any getter method.
private
supports(string $class) : bool
Parameters
- $class : string
Return values
boolupdateData()
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(array<string|int, Type> $types, string $currentClass, string $attribute, mixed $data, string|null $format, array<string|int, mixed> $context) : mixed
Parameters
- $types : array<string|int, Type>
- $currentClass : string
- $attribute : string
- $data : mixed
- $format : string|null
- $context : array<string|int, mixed>