PHPackages                             check24/apitk-dtomapper-bundle - 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. [API Development](/categories/api)
4. /
5. check24/apitk-dtomapper-bundle

ActiveSymfony-bundle[API Development](/categories/api)

check24/apitk-dtomapper-bundle
==============================

This bundle provides mapping helpers to map rest action to DTOs and render them serialized.

4.1.0(4y ago)23.2k3[1 PRs](https://github.com/byWulf/apitk-dtomapper-bundle/pulls)MITPHPPHP ^7.4 || ^8.0

Since Jul 20Pushed 4y agoCompare

[ Source](https://github.com/byWulf/apitk-dtomapper-bundle)[ Packagist](https://packagist.org/packages/check24/apitk-dtomapper-bundle)[ RSS](/packages/check24-apitk-dtomapper-bundle/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (9)Dependencies (16)Versions (19)Used By (0)

apitk-dtomapper-bundle - DTO handling
=====================================

[](#apitk-dtomapper-bundle---dto-handling)

Overview
--------

[](#overview)

This bundle adds versioned DTO support for RESTful API's.

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

[](#installation)

Install the package via composer:

```
composer require check24/apitk-dtomapper-bundle
```

Usage
-----

[](#usage)

### Setup

[](#setup)

Add this to your services.yaml so the bundle can automatically load and use the mapper services:

```
services:
    App\DtoMapper\:
        resource: '../src/DtoMapper'
        public: true
```

### Writing Mappers

[](#writing-mappers)

Create a mapper class in the folder `src/DtoMapper` (or whichever you configured) which implements the `MapperInterface` and transforms incoming data into a single DTO:

```
use Shopping\ApiTKDtoMapperBundle\DtoMapper\MapperInterface;

class UserV1Mapper implements MapperInterface
{
    /**
     * @param User $data
     * @return Dto\UserV1
     */
    public function map($data): Dto\UserV1
    {
        $userDto = new Dto\UserV1();
        $userDto->setId($data->getId())
            ->setUsername($data->getUsername())
            ->setEmail($data->getEmail());

        return $userDto;
    }
}
```

In your controller replace the `@Rest\View()` annotation with the corresponding `@Dto\View()` mentioning the mapper to use:

```
use FOS\RestBundle\Controller\Annotations as Rest;
use Shopping\ApiTKDtoMapperBundle\Annotation as DtoMapper;

/**
 * @Rest\Get("/v1/users")
 * @DtoMapper\View(dtoMapper="App\DtoMapper\UserV1Mapper")
 *
 * @param EntityManagerInterface $entityManager
 * @return User[]
 */
public function getUsersV1(EntityManagerInterface $entityManager)
{
    $userRepository = $entityManager->getRepository(User::class);

    $users = $userRepository->findAll();

    return $users;
}
```

The bundle now automatically transform whatever you return in the action with the help of the given mapper into an DTO. When you return an array of data in your controller, the mapper will be called on every single element. You don't have to worry about that.

You can throw a `UnmappableException` if you want to skip some elements of the array.

Also the bundle auto generates a swagger response with code 200 and the corresponding DTO scheme (respectively an array of DTOs), so you don't have to add the redundant `@SWG\Response()`. For this to work, just take care that your Mapper has a correct return typehint (f.e. `public function map($data): FoobarDto`) and that your controller action has a return annotation, which states if an array or object is returned (f.e. `* @return Foobar[]`). You can still overwrite this by your own `@SWG\Response()` annotation.

### Turning arrays into Collections

[](#turning-arrays-into-collections)

If you return an array in the controller it will be serialized like this. If you do not want to work with an array or can not work with arrays due to technological constraints (protobuf) you can instruct the bundle to turn arrays into collection-classes instead.

To turn an array returned from a controller into a collection, implement the MapperCollectionInterface additionally to the MapperInterface into your mapper.

Example:

```
use Shopping\ApiTKDtoMapperBundle\DtoMapper\MapperCollectionInterface;
use Shopping\ApiTKDtoMapperBundle\DtoMapper\MapperInterface;

class UserV1Mapper implements MapperInterface, MapperCollectionInterface
{
    /**
     * @param Dto\UserV1[] $items
     */
    public function mapCollection(array $items): Dto\UserV1Collection {
        $collection = new UserV1Collection();
        $collection->setItems($items);

        return $collection;
    }

    /**
     * @param Dto\UserV1 $data
     */
    public function map($data): Dto\UserV1
    {
        $userDto = new Dto\UserV1();
        $userDto->setId($data->getId())
            ->setUsername($data->getUsername())
            ->setEmail($data->getEmail());

        return $userDto;
    }
}
```

This will cause the bundle to call `mapCollection` as soon as all items have been mapped via `map`. You can initialize your collection class within the `mapCollection` method. The object returned here will replace the controller response's content.

### Serialized DTO view

[](#serialized-dto-view)

If you wish to return the DTOs in a `serialize($dto)` manner instead of json, implement the available dto view handler.

```
//fos_rest.yaml
fos_rest:
    view:
        mime_types:
            dto: ['application/vnd.dto'] # You can specify whatever mime type you want, just map it to "dto".
    service:
        view_handler: app.view_handler
    exception:
        serializer_error_renderer: true
```

```
//services.yaml
services:
    app.view_handler:
        autowire: true
        autoconfigure: false
        public: false
        parent: fos_rest.view_handler.default
        calls:
            - ['registerHandler', ['dto', ['@Shopping\ApiTKDtoMapperBundle\Handler\PhpViewHandler', 'createResponse']]]
```

When calling the API with the `Accept: application/vnd.dto` header, you will get the DTO as an unserializable string.

Exceptions will also be serialized. Stack Traces, filenames, line numbers and previous exceptions will be omitted when `kernel.debug` is set to `false` (= in productive environments) to avoid leaking potentially sensitive information.

### Serialized Protobuf view

[](#serialized-protobuf-view)

If you wish to return serialized Protobuf object.

```
//fos_rest.yaml
fos_rest:
    view:
        mime_types:
            proto: ['application/x-protobuf'] # You can specify whatever mime type you want, just map it to "proto".
    service:
        view_handler: app.view_handler
    exception:
        serializer_error_renderer: true
```

```
//services.yaml
services:
    app.view_handler:
        autowire: true
        autoconfigure: false
        public: false
        parent: fos_rest.view_handler.default
        calls:
            - ['registerHandler', ['proto', ['@Shopping\ApiTKDtoMapperBundle\Handler\ProtobufViewHandler', 'createResponse']]]
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 55.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~90 days

Recently: every ~140 days

Total

15

Last Release

1587d ago

Major Versions

0.9.0 → 1.0.02018-08-16

1.0.5 → 2.0.02020-03-23

2.0.2 → 3.0.02020-06-26

3.2.0 → 4.0.02021-11-09

PHP version history (4 changes)0.9.0PHP ^7.1.3

3.0.0PHP ^7.2

3.2.0PHP ^7.2 || ^8.0

4.0.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/caaeaf0a1a74fef634f9eea17d585684d3758882c622ac42f8ef6973b1ef1910?d=identicon)[byWulf](/maintainers/byWulf)

![](https://www.gravatar.com/avatar/82e22e3d09adca0634ae909f6e3a8cbb82e193d6436ca7e304e2ff666097e58a?d=identicon)[ofeige](/maintainers/ofeige)

![](https://www.gravatar.com/avatar/f94057f8465d9062e73c8a219f4edb5b36612d624db53240b9c2c7b6616b6c55?d=identicon)[alexdo](/maintainers/alexdo)

---

Top Contributors

[![byWulf](https://avatars.githubusercontent.com/u/135651?v=4)](https://github.com/byWulf "byWulf (46 commits)")[![alexdo](https://avatars.githubusercontent.com/u/690188?v=4)](https://github.com/alexdo "alexdo (21 commits)")[![tronsha](https://avatars.githubusercontent.com/u/2240180?v=4)](https://github.com/tronsha "tronsha (8 commits)")[![markuspoerschke](https://avatars.githubusercontent.com/u/1222377?v=4)](https://github.com/markuspoerschke "markuspoerschke (6 commits)")[![Macavity](https://avatars.githubusercontent.com/u/300609?v=4)](https://github.com/Macavity "Macavity (1 commits)")[![manjencic](https://avatars.githubusercontent.com/u/3406389?v=4)](https://github.com/manjencic "manjencic (1 commits)")

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/check24-apitk-dtomapper-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/check24-apitk-dtomapper-bundle/health.svg)](https://phpackages.com/packages/check24-apitk-dtomapper-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[sulu/headless-bundle

Bundle that provides controllers and services for using Sulu as headless content management system

55133.7k2](/packages/sulu-headless-bundle)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

7310.3k29](/packages/open-dxp-opendxp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
