boshurik / mapper
Mapper
Installs: 3 397
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.2 | ^8.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-11-08 19:57:17 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());