boshurik/mapper
Lightweight Laravel/PHP object mapper for converting between arrays and DTOs/entities. Helps map input data to typed objects and back with minimal boilerplate, supporting custom mapping rules and nested structures for clean data transformations.
Library for mapping given object to another (e.g. to DTO and back)
$registry = new MappingRegistry();
$registry->add(User::class, UserDto::class, function(User $user, MapperInterface $mapper, array $context) {
$dto = $context[Mapper::DESTINATION_CONTEXT] ?? new UserDto();
$dto->name = $user->getName();
return $dto;
});
$mapper = new Mapper($registry);
$user = new User('name');
$dto = $mapper->map($user, UserDto::class);
// Map to existing object. You can get it from $context[Mapper::DESTINATION_CONTEXT]
$dto = $mapper->map($user, new UserDto());
How can I help you explore Laravel packages today?