PHP code example of onmoon / dto-mapper

1. Go to this page and download the library: Download onmoon/dto-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/ */

    

onmoon / dto-mapper example snippets


public function showPetById(int $petId) : ShowPetByIdResponseDto
{
    $pet   = $this->pets->getById($petId);

    $mapper = new OnMoon\DtoMapper\DtoMapper();
    return $mapper->map($pet, ShowPetByIdResponseDto::class);
}

$mapper->map($pet, ShowPetByIdResponseDto::class, function ($propertyName, $context) { 
    $fullName = implode('->', [...$context, $propertyName]);
    if ($fullName === 'subObject->s2->property1') {
       return 'realProperty1';
    }
    return $propertyName;
});