Types
Types are runtime objects (not compiled or cached) that are responsible for checking a value for conformance and for converting from one value to another.
When calling the
Mapper::normalize()orMapper::denormalize()method, theTypeInterface::cast()[1] method will be called.[1]: This isn't always the case. For example, union types additionally call the
match()method to determine the appropriate type.
When calling the
Mapper::isNormalizable()orMapper::isDenormalizable()method, theTypeInterface::match()method will be called.
Definitions
The type cannot be explicitly registered in the mapper (in the platform). Instead of registering types, type builders are used that analyze the "type definition" and create a specific type based on it.
For example:
As you can see, the signature (the "type definition") of the type is different. The mapper notices this and selects the appropriate type instance based on it.
Directions
It is worth noting that when using the mapper there are two directions:
Normalization
Denormalization
Thus, some types may have multiple implementations depending on which direction the transformation occurs. For example, a built-in "backed enum" type:
A type can be responsible for one thing, for example, converting from an enum to a string. However, if desired, the direction can be determined at runtime within the type itself. However, this method is not recommended.