PHPackages                             artyuum/request-dto-mapper-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. artyuum/request-dto-mapper-bundle

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

artyuum/request-dto-mapper-bundle
=================================

This bundle provides an easy way to automatically map the incoming request data to a DTO and optionally validate it.

v1.0.0(3y ago)515.8k[1 PRs](https://github.com/artyuum/request-dto-mapper-bundle/pulls)MITPHPPHP ^8.0

Since Feb 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/artyuum/request-dto-mapper-bundle)[ Packagist](https://packagist.org/packages/artyuum/request-dto-mapper-bundle)[ RSS](/packages/artyuum-request-dto-mapper-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (16)Versions (5)Used By (0)

Request DTO Mapper Bundle
=========================

[](#request-dto-mapper-bundle)

[![image](https://user-images.githubusercontent.com/17199757/193117824-e5eec5b6-f4c0-4c96-af9b-fa2bc6096806.png)](https://user-images.githubusercontent.com/17199757/193117824-e5eec5b6-f4c0-4c96-af9b-fa2bc6096806.png)

This bundle provides an easy way to automatically map the incoming request data to a DTO and optionally validate it. It's using the powerful [Serializer](https://symfony.com/doc/current/components/serializer.html) component under the hood along with the [Validator](https://symfony.com/doc/current/components/validator.html) component (optional).

Requirements
------------

[](#requirements)

- PHP ^8.0
- Symfony ^5.0 or ^6.0

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

[](#installation)

```
composer require artyuum/request-dto-mapper-bundle
```

Configuration
-------------

[](#configuration)

```
# config/packages/artyum_request_dto_mapper.yaml
artyum_request_dto_mapper:

    # Used if the attribute does not specify any (must be a FQCN implementing "\Artyum\RequestDtoMapperBundle\Extractor\ExtractorInterface").
    default_extractor: null # Example: Artyum\RequestDtoMapperBundle\Extractor\JsonExtractor

    # The configuration related to the denormalizer (https://symfony.com/doc/current/components/serializer.html).
    denormalizer:

        # Used when mapping the request data to the DTO if the attribute does not set any.
        default_options: []

        # Used when mapping the request data to the DTO (merged with the values passed by the attribute or "default_options").
        additional_options: []

    # The configuration related to the validator (https://symfony.com/doc/current/validation.html).
    validation:

        # Whether to validate the DTO after mapping it.
        enabled: false

        # Used when validating the DTO if the attribute does not set any.
        default_groups: []

        # Used when validating the DTO (merged with the values passed by the attribute or "default_groups").
        additional_groups: []

        # Whether to throw an exception if the DTO validation failed (constraint violations).
        throw_on_violation: true
```

Usage
-----

[](#usage)

This is a simple step-by-step example of how to make a DTO that will be used by the bundle.

1. Create the DTO that represents the structure of the content the user will send to your controller.

```
class PostPayload {
    /**
     * @Assert\Sequentially({
     *     @Assert\NotBlank,
     *     @Assert\Type("string")
     * })
     *
     * @var string|null
     */
    public $content;
}
```

2. Inject the DTO into your controller &amp; configure it using the [Dto attribute](/src/Attribute/Dto.php).

```
use Artyum\RequestDtoMapperBundle\Attribute\Dto;
use Artyum\RequestDtoMapperBundle\Extractor\JsonExtractor;

class CreatePostController extends AbstractController
{
    #[Dto(extractor: JsonExtractor::class, subject: PostPayload::class, validate: true)]
    public function __invoke(PostPayload $postPayload): Response
    {
        // At this stage, your DTO has automatically been mapped (from the JSON input) and validated.
        // Your controller can safely be executed knowing that the submitted content
        // matches your requirements (defined in your DTO through the validator constraints).
    }
}
```

**Alternatively**, you can set the attribute directly on the argument:

```
public function __invoke(#[Dto(extractor: JsonExtractor::class, validate: true)] PostPayload $postPayload): Response
{
}
```

If you have set some default options in the configuration file (the default extractor to use, whether to enable the validation), you can even make it shorter:

```
public function __invoke(#[Dto] PostPayload $postPayload): Response
{
}
```

3. That's it!

Attribute
---------

[](#attribute)

The [Dto attribute](src/Attribute/Dto.php) has the following seven properties:

### 1. Extractor

[](#1-extractor)

The FQCN (Fully-Qualified Class Name) of a class that implements the `ExtractorInterface`. It basically contains the extraction logic and it's called by the mapper in order to extract the data from the request.

The bundle already comes with 3 built-in extractors that should meet most of your use-cases:

- [BodyParameterExtractor](/src/Extractor/BodyParameterExtractor.php) (extracts the data from `$request->request->all()`)
- [JsonExtractor](/src/Extractor/JsonExtractor.php) (extracts the data from `$request->toArray()`)
- [QueryStringExtractor](/src/Extractor/QueryStringExtractor.php) (extracts the data from `$request->query->all()`)

If an error occurs when the `extract()` method is called from the extractor class, the [ExtractionFailedException](src/Exception/ExtractionFailedException.php) will be thrown.

If these built-in extractor classes don't meet your needs, you can implement your own extractor like this:

```
use Artyum\RequestDtoMapperBundle\Extractor\ExtractorInterface;
use Symfony\Component\HttpFoundation\Request;

class CustomExtractor implements ExtractorInterface
{
    // you can optionally inject dependencies
    public function __construct() {
    }

    public function extract(Request $request): array
    {
        // your custom extraction logic here
    }
}
```

Then pass it to the `Dto` attribute like this:

```
#[Dto(extractor: CustomExtractor::class)]
```

If you don't set any value, the default value (defined in the bundle's configuration file) will be used.

**Note:** All classes implementing `ExtractorInterface` are automatically tagged as "artyum\_request\_dto\_mapper.extractor", and this is needed by the mapper in order to retrieve the needed extractor class instance from the container.

### 2. Subject

[](#2-subject)

The FQCN (Fully-Qualified Class Name) of the DTO you want to map (it must be present as your controller argument).

The "subject" property is required **only** if you're setting the attribute directly on the method. Example:

```
#[Dto(subject: PostPayload::class)]
public function __invoke(PostPayload $postPayload): Response
{
}
```

If you're setting the attribute on the method argument instead, the "subject" value can be omitted and won't be read by the mapper. Example:

```
public function __invoke(#[Dto] PostPayload $postPayload): Response
{
}
```

### 3. Methods

[](#3-methods)

It can contain a single or an array of HTTP methods that will "enable" the mapping/validation depending on the current HTTP method. In the following example, the DTO will be mapped &amp; validated only if the request method is "GET".

```
#[Dto(methods: 'GET')]
```

or

```
#[Dto(methods: ['GET'])]
```

If the array is empty (this is the default value), the mapper will always map the DTO and validate it.

### 4. Denormalization Options

[](#4-denormalization-options)

The options that will be passed to the [Denormalizer](https://symfony.com/doc/current/components/serializer.html) before mapping the DTO.

Example:

```
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

#[Dto(denormalizerOptions: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true])]
```

If an error occurs when the `denormalize()` method is called from the Denormalizer, the [DtoMappingFailedException](src/Exception/DtoMappingFailedException.php) will be thrown.

### 5. Validate

[](#5-validate)

Whether to validate the DTO (once the mapping is done). Internally, the [validator component](https://symfony.com/doc/current/validation.html) will be used, and if you do not have it installed a `LogicException` will be thrown.

Example:

```
#[Dto(validate: true)]
```

If the validation failed (due to the constraint violations), the constraint violations will be available as request attribute:

```
$request->attributes->get('_constraint_violations')
```

If you don't set any value, the configured value (defined in the bundle's configuration file) will be used.

### 6. Validation Groups

[](#6-validation-groups)

The [validation groups](https://symfony.com/doc/current/form/validation_groups.html) to pass to the validator.

Example:

```
#[Dto(validationGroups: ['creation'])]
```

If you don't set any value, the configured value (defined in the bundle's configuration file) will be used.

### 7. Throw on violation

[](#7-throw-on-violation)

When the validation failed, the [DtoValidationFailedException](/src/Exception/DtoValidationFailedException.php) will be thrown, and you will be able to get a list of these violations by calling the `getViolations()` method.

Setting the value to `false` will prevent the exception from being thrown, and your controller will still be executed.

Example:

```
#[Dto(throwOnViolation: false)]
```

If you don't set any value, the configured value (defined in the bundle's configuration file) will be used.

Events
------

[](#events)

- [PreDtoMappingEvent](/src/Event/PreDtoMappingEvent.php) - dispatched before the mapping is made.
- [PostDtoMappingEvent](/src/Event/PostDtoMappingEvent.php) - dispatched once the mapping is made.
- [PreDtoValidationEvent](/src/Event/PreDtoValidationEvent.php) - dispatched before the validation is made (if the validation is enabled).
- [PostDtoValidationEvent](/src/Event/PostDtoValidationEvent.php) - dispatched once the validation is made (if the validation is enabled).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 95.8% 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 ~329 days

Total

3

Last Release

1263d ago

Major Versions

v0.0.2 → v1.0.02022-12-02

PHP version history (3 changes)v0.0.1PHP ^7.4

v0.0.2PHP ^7.4 || ^8.0

v1.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9d57dc1e2de89690f5f7c75fa82af0a525cee76ddf5dd02ecbbe2652390f43b0?d=identicon)[Artyuum](/maintainers/Artyuum)

---

Top Contributors

[![artyuum](https://avatars.githubusercontent.com/u/17199757?v=4)](https://github.com/artyuum "artyuum (23 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

apibundledtodto-mapperobject-mapperphpphp8symfonysymfony-bundleapisymfonybundlemapperdtorequest-mapper

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/artyuum-request-dto-mapper-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/artyuum-request-dto-mapper-bundle/health.svg)](https://phpackages.com/packages/artyuum-request-dto-mapper-bundle)
```

###  Alternatives

[sylius/sylius

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

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

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[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)[web-auth/webauthn-framework

FIDO2/Webauthn library for PHP and Symfony Bundle.

50570.7k1](/packages/web-auth-webauthn-framework)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)

PHPackages © 2026

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