boshurik/mapper

0.2.2 2021-07-08 13:12 UTC

This package is auto-updated.

Last update: 2024-09-08 19:38:23 UTC


README

将给定对象映射到另一个对象(例如,到DTO并返回)的库

使用方法

$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());