PHPackages                             symfonycasts/micro-mapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. symfonycasts/micro-mapper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

symfonycasts/micro-mapper
=========================

A tiny, underwhelming data mapper to map one object to another!

v0.3.0(5mo ago)82597.6k↓19.2%8[6 issues](https://github.com/SymfonyCasts/micro-mapper/issues)[1 PRs](https://github.com/SymfonyCasts/micro-mapper/pulls)MITPHPPHP &gt;=8.1CI passing

Since Aug 21Pushed 3mo ago9 watchersCompare

[ Source](https://github.com/SymfonyCasts/micro-mapper)[ Packagist](https://packagist.org/packages/symfonycasts/micro-mapper)[ RSS](/packages/symfonycasts-micro-mapper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (9)Used By (0)

MicroMapper: The Tiny, Underwhelming Data Mapper for Symfony!
=============================================================

[](#micromapper-the-tiny-underwhelming-data-mapper-for-symfony)

[![CI](https://github.com/SymfonyCasts/micro-mapper/actions/workflows/ci.yaml/badge.svg)](https://github.com/SymfonyCasts/micro-mapper/actions/workflows/ci.yaml)

Need to map one object (e.g. a Doctrine entity) to another object (e.g. a DTO) and *love* writing the mapping code manually? Then this library is for you!

Define a "mapper" class:

```
use App\Entity\Dragon;
use App\DTO\DragonDTO;

#[AsMapper(from: Dragon::class, to: DragonDTO::class)]
class DragonEntityToDtoMapper implements MapperInterface
{
    public function load(object $from, string $toClass, array $context): object
    {
        $entity = $from;

        return new DragonDTO($entity->getId());
    }

    public function populate(object $from, object $to, array $context): object
    {
        $entity = $from;
        $dto = $to;

        $dto->name = $entity->getName();
        $dto->firePower = $entity->getFirePower();

        return $dto;
    }
}
```

Then... map!

```
$dragon = $dragonRepository->find(1);
$dragonDTO = $microMapper->map($dragon, DragonDTO::class);
```

MicroMapper is similar to other data mappers, like [jolicode/automapper](https://github.com/jolicode/automapper), except... less impressive! Jane's Automapper is awesome and handles a lot of heavy lifting. With MicroMapper, *you* do the heavy lifting. Let's review with a table!

FeatureMicroMapperJane's AutomapperSome of the mapping is automatic❌✅Extensible✅✅Handles nested objects✅✅Small &amp; Dead-simple✅(not SO simple)Support us &amp; Symfony
------------------------

[](#support-us--symfony)

Is this package useful! We're *thrilled* 😍!

A lot of time &amp; effort from the Symfonycasts team &amp; the Symfony community goes into creating and maintaining these packages. You can support us + Symfony (and learn a bucket-load) by grabbing a subscription to [SymfonyCasts](https://symfonycasts.com)!

Installation
------------

[](#installation)

```
composer require symfonycasts/micro-mapper
```

If you're using Symfony, you're done! If not, see [Stand-alone Library Setup](#stand-alone-library-setup).

Usage
-----

[](#usage)

Suppose you have a `Dragon` entity, and you want to map it to a `DragonApi` object (perhaps to use with API Platform, like we do in our [Api Platform EP3 Tutorial](https://symfonycasts.com/screencast/api-platform3-extending)).

### Step 1: Create the Mapper Class

[](#step-1-create-the-mapper-class)

To do this, create a "mapper" class that defines how to map:

```
namespace App\Mapper;

use App\Entity\Dragon;
use App\ApiResource\DragonApi;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;

#[AsMapper(from: Dragon::class, to: DragonApi::class)]
class DragonEntityToApiMapper implements MapperInterface
{
    public function load(object $from, string $toClass, array $context): object
    {
        $entity = $from;
        assert($entity instanceof Dragon); // helps your editor know the type

        return new DragonApi($entity->getId());
    }

    public function populate(object $from, object $to, array $context): object
    {
        $entity = $from;
        $dto = $to;
        // helps your editor know the types
        assert($entity instanceof Dragon);
        assert($dto instanceof DragonApi);

        $dto->name = $entity->getName();
        $dto->firePower = $entity->getFirePower();

        return $dto;
    }
}
```

The mapper class has three parts:

1. `#[AsMapper]` attribute: defines the "from" and "to" classes (needed for Symfony usage only).
2. `load()` method: creates/loads the "to" object - e.g. load it from the database or create it and populate just the identifier.
3. `populate()` method: populates the "to" object with data from the "from" object.

### Step 2: Use the MicroMapper Service

[](#step-2-use-the-micromapper-service)

To use the mapper, you can fetch the `MicroMapperInterface` service. For example, from a controller:

```
