For simple native types, the mapper will use casting to convert the data to the correct type.
When requesting an array type, the mapper will call itself with the type of the array elements for each of the elements in the array.
For object types, some magic happens. On the very first run for each unique class, the mapper will use reflection to
analyse the class and build a mapper function based on that information.
This function takes a mapper and an array of data that is passed to the mapper. For each of the properties or constructor parameters,
the mapper function will cast or map the value whilst taking into account default values and other configuration.
The goal is to have as much and as simple mapping as possible in these generated functions without having to go back to the mapper to reach the best performance.
As an example, this is one of the testing classes in this repository and its (slightly simplified) generated mapper function:
#[PostMapping('post')]
class UserDto
{
/** First name and last name */
public string $name;
/** @var array<self> */
public array $friends = [];
public ?SuitEnum $favoriteSuit = null;
public function __construct(string $name)
{
$this->name = $name;
}
public function post(): void
{
$this->name = \ucfirst($this->name);
}
}
function jmapper_8cf8f45dc33c7f58ab728699ac3ebec3(Jerodev\DataMapper\Mapper $mapper, array $data)
{
$x = new Jerodev\DataMapper\Tests\_Mocks\UserDto((string) $data['name']);
$x->friends = (\array_key_exists('friends', $data) ? \array_map(static fn ($x6462755ab00b1) => $mapper->map('Jerodev\DataMapper\Tests\_Mocks\UserDto', $x6462755ab00b1), $data['friends']) : []);
$x->favoriteSuit = (\array_key_exists('favoriteSuit', $data) ? Jerodev\DataMapper\Tests\_Mocks\SuitEnum::from($data['favoriteSuit']) : NULL);
$x->post($data, $x);
return $x;
}