Metadata Configuration
Metadata allows you to configure rules for processing objects and supplementing information. There are several metadata formats, but their capabilities are almost identical. You can choose any format that suits you best, such as PHP Attributes, configuration file in YAML format, or any other format.
Class Metadata
There are several rules that may apply to a specific class. Detailed information on these rules is provided below.
Normalize As Array
This option is responsible for converting an object to an associative array during serialization.
In case of the
truevalue specified, the object will be converted to an associative array during normalizationIn case of the
falsevalue specified, the object will be converted to an object (instance ofStdClass) during normalizationIf you omit this option, the default value specified in the configuration will be used
Discriminator Map
A map discriminator is a set of rules for inferring a type based on the value of a field.
For example, we have the following class hierarchy.
In this case, when trying to denormalize the class UserInfo, an error will occur since there will be an attempt to create an abstract class.
А similar error will occur when instantiating an interface.
To solve this problem, there is a discriminator map, with the help of which you can specify which class the data belongs to.
For example, we want that:
When passing a
'type' => 'admin', an objectAdminUserInfois createdWhen passing a
'type' => 'guest', an objectGuestUserInfois created
The metadata configuration for this rule will look like this:
Default Type
When specifying such a configuration, another error may occur if the "type" field is not passed or an incorrect value is passed.
To solve such problems, you can specify a default value (default type) that will be used in case of an incorrect or missing field in the discriminator map.
Let's define that if the map discriminator fails, then we will create a GuestUserInfo instance.
Properties
For each class, you can specify a list of properties and their types that will be involved in normalization and denormalization process.
For example, we have the following class:
To specify properties and their types, you can use the following settings
As you can see, the #[MapType] attribute can be set on both properties and property hooks. Depending on where you specify the attribute, the corresponding rule will be applied.
For promoted properties, you can also use parameter (@param) PHPDoc annotations.
Besides this, as you can see, an annotations can be set on arguments, properties and property hooks. Depending on where you specify the annotation, the corresponding rule will be applied.
To use reflection, simply specify the types. Please note that only public properties are read.
Property Metadata
As you may have noticed above, a class can contain a collection of properties. However, for each property, you can also specify a list of additional rules. One such rule is the type, the configuration information for which is available above in "properties" section.
Let's look at other configuration rules.
Strict Types
For each property, you can also separately specify "type strictness" rules, more details about which can be found either in the configuration or in the section with type coercers.
The "strict types" option can take one of two possible values, or may not be specified:
In case of the
truevalue specified, then "strict types" will be applied for the propertyIn case of the
falsevalue specified, then "strict types" will be disabled for the propertyIf you omit this option, the default value specified in the configuration will be used
Let's take the following class and apply different strictness rules to each property:
Rename (Alias)
You can specify the name of the property to be used during normalization. This way, when normalizing an object with property, it will be "published" under a different name.
Custom Type Error Message
In some cases, you may need to override the error message when type errors occur. You can do this explicitly for a specific class property.
Please note that you also have access to template variables that substitute specific runtime values:
{{field}}- Property name (or public alias){{expected}}- Expected type{{value}}- Actual value{{path}}- The path to the property where the error occurred{{file}}- PHP file in which the error occurred{{line}}- The line in the PHP code where the error occurred{{code}}- An error code
Let's specify an error message for property string $name, like:
Thus, if you pass an empty array ([]) to "name" property, the following error should occur:
Custom Undefined Error Message
If a required field was not passed, a different error may occur. You can also customize it using the corresponding configuration rules.
Skip
During normalization, there may be cases where a property needs to be excluded based on certain criteria. For this, you can use "skip" metadata configuration rules.
When Empty
If you want to exclude empty properties (for example, empty arrays), you can use the "when empty" rules
When Null
If you want to exclude properties that are strictly null, you should use the "when null" setting.
Expression
In some cases, you may need to use more complex pass criteria. For these conditions, use the symfony/expression-language package.