Skip to main content

Getting Started Guide

Installation

Use Composer to install PHP AutoMapper into your project:

composer require backbrain/php-automapper

Usage

<?php
// php docs/example/01_getting_started_usage.php

use Backbrain\Automapper\Contract\Builder\Options;
use Backbrain\Automapper\Contract\Builder\Config;
use Backbrain\Automapper\MapperConfiguration;

require_once __DIR__ . '/../../vendor/autoload.php';

$config = new MapperConfiguration(fn (Config $config) => $config
->createMap(AccountDTO::class, ProfileDTO::class)
->forMember(
'fullName',
fn (Options $opts) => $opts->mapFrom(
fn (AccountDTO $source) => sprintf('%s %s', $source->givenName, $source->familyName)
)
)
);

$account = new AccountDTO();
$account->givenName = 'John';
$account->familyName = 'Doe';

$autoMapper = $config->createMapper();
$profile = $autoMapper->map($account, ProfileDTO::class);

dump($profile);