PHP code example of boshurik / mapper

1. Go to this page and download the library: Download boshurik/mapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

boshurik / mapper example snippets


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