1. Go to this page and download the library: Download skraeda/laravel-automapper 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/ */
skraeda / laravel-automapper example snippets
namespace App\Models;
class Employee
{
protected $firstName;
protected $lastName;
protected $birthYear;
public function __construct($firstName = null, $lastName = null, $birthYear = null)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->birthYear = $birthYear;
}
public function getFirstName() { return $this->firstName; }
public function getLastName() { return $this->lastName; }
public function getBirthYear() { return $this->birthYear; }
}
class EmployeeDto
{
protected $firstName;
protected $lastName;
protected $birthYear;
}
namespace App\Providers;
use App\Models\Employee;
use App\Models\EmployeeDto;
use Illuminate\Support\ServiceProvider;
use Skraeda\AutoMapper\Support\Facades\AutoMapperFacade as AutoMapper;;
class AppServiceProvider extends ServiceProvider
{
/**
* Register AutoMapper mappings.
*
* @see https://github.com/mark-gerarts/automapper-plus#registering-mappings
* @return void
*/
public function boot()
{
AutoMapper::getConfiguration()
->registerMapping(Employee::class, EmployeeDto::class)
->reverseMap();
}
}
use Skraeda\AutoMapper\Support\Facades\AutoMapperFacade as AutoMapper
$dto = AutoMapper::map(new Employee("John", "Doe", 1980), EmployeeDto::class);
$john = AutoMapper::map($dto, Employee::class);
class EmployeeDto
{
protected $fullName;
protected $age;
public function setFullName($fullName) { $this->fullName = $fullName; }
public function setAge($age) { $this->age = $age; }
}
namespace App\Mappings;
use AutoMapperPlus\CustomMapper\CustomMapper;
use Illuminate\Support\Carbon;
class EmployeeToDtoMapper extends CustomMapper
{
/**
* Map properties of source to destination.
*
* @param array|object $source
* @param object $destination
* @param array $context
* @return mixed
*/
public function mapToObject($source, $destination, array $context = [])
{
$destination->setFullName(sprintf("%s %s", $source->getFirstName(), $source->getLastName()));
$destination->setAge(Carbon::now()->subYears($source->getBirthYear())->year);
return $destination;
}
}
namespace App\Mappings;
use App\Models\Employee;
use App\Models\EmployeeDto;
use Skraeda\AutoMapper\Attributes\Maps;
#[Maps(Employee::class, EmployeeDto::class)]
class EmployeeToDtoMapper extends CustomMapper
{
// Code..
}
use Illuminate\Support\Collection;
$employees = Collection::make([new Employee("John", "Doe", 1980), new Employee("Jane", "Doe", 1985)]);
$dtos = $employees->autoMap(EmployeeDto::class);
public function map($source, $targetClass, array $context = []);
public function mapToObject($source, $target, array $context = []);
public function mapMultiple($collection, string $targetClass, array $context = []): \Illuminate\Support\Collection;
public function getConfiguration(): \AutoMapperPlus\Configuration\AutoMapperConfigInterface;
public function registerCustomMapper(string $mapper, string $source, string $target): void;