boshurik/mapper

Mapper

Maintainers

👁 BoShurik

Package info

github.com/BoShurik/mapper

pkg:composer/boshurik/mapper

Statistics

Installs: 3 660

Dependents: 1

Suggesters: 0

Stars: 2

Open Issues: 0

0.2.2 2021-07-08 13:12 UTC

Requires

  • php: ^7.2 | ^8.0

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 195bef940e9decacfbd08c947b1cc0087b9f3999

  • BoShurik <boshurik.woop@gmail.com>

This package is auto-updated.

Last update: 2026-06-08 23:25:25 UTC


README

Library for mapping given object to another (e.g. to DTO and back)

Usage

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