PHPackages                             nikita2206/symfony-request-converter - 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. nikita2206/symfony-request-converter

AbandonedArchivedLibrary

nikita2206/symfony-request-converter
====================================

Request converter converts request data to data-objects

07[4 issues](https://github.com/nikita2206/symfony-request-converter/issues)PHP

Since Sep 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/nikita2206/symfony-request-converter)[ Packagist](https://packagist.org/packages/nikita2206/symfony-request-converter)[ RSS](/packages/nikita2206-symfony-request-converter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Request converter for Symfony2
==============================

[](#request-converter-for-symfony2)

[![Build Status](https://camo.githubusercontent.com/5e0886e3374b9fd20cdd3a6ab122ce0e85146fe0b439b0018d1940856be9f5fb/68747470733a2f2f7472617669732d63692e6f72672f6e696b697461323230362f73796d666f6e792d726571756573742d636f6e7665727465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/nikita2206/symfony-request-converter)[![Coverage Status](https://camo.githubusercontent.com/5b6ae6764e7d346360c5ceeeea84b069c217dc3e428674cba49e70755299bbc2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6e696b697461323230362f73796d666f6e792d726571756573742d636f6e7665727465722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/nikita2206/symfony-request-converter?branch=master)

Convert API requests from plain PHP-arrays to request objects.

### Example

[](#example)

In your controller action you can have:

```
public function addBlogPostAction(PostRequest $post)
{
    $this->commandBus->execute(new NewPost($post));
}
```

Request will automatically be converted from plain array form to the `PostRequest` class instance (created without calling the constructor) and validated:

```
use RequestConverter\Annotation\Type;
use RequestConverter\Annotation\Optional;
use RequestConverter\Annotation\Request;

/**
 * We need to use this annotation in order to tell request converter that it should pick up action
 *   parameter type-hinted with this class
 * @Request()
 **/
class PostRequest
{
    /**
     * @Type("string")
     **/
    private $title;

    /**
     * @Type("string")
     **/
    private $text;

    /**
     * @Type("string")
     * @Optional()
     **/
    private $author;
}
```

### How to set up

[](#how-to-set-up)

After requiring `nikita2206/symfony-request-converter` in your `composer.json` you'll need to register [exception listener](http://symfony.com/doc/current/cookbook/event_dispatcher/event_listener.html)in order to render `RequestConverter\Exception\BadRequestException` which will be thrown if the request isn't valid.

This is a basic example of said listener, you'll of course want to make it adhere to your API error specs. Or, as a better solution, delegate rendering of an error to another controller just like it's done in `Symfony\Component\HttpKernel\EventListener\ExceptionListener`.

```
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use RequestConverter\Exception as Ex;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionListener
{
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if ( ! $event->getException() instanceof Ex\BadRequestException) {
            return;
        }

        $errors = [];

        // this exception is thrown when there was a problem while mapping input data on the object:
        //   * missing field
        //   * incompatible type (string when array is expected f.e.)
        //   * uncoercible value (string "45zz" when integer is expected)
        if ($event->getException() instanceof Ex\RequestConversionException) {
            foreach ($event->getException()->getErrors() as $err) {
               $errors[] = ["field" => $err->getField(), "kind" => get_class($err)];
            }

        // this exception is the result of symfony/validation component, it has ConstraintViolationListInterface
        } elseif ($event->getException() instanceof Ex\RequestValidationException) {
            foreach ($event->getException()->getViolations() as $v) {
                $errors[] = ["field" => $v->getPropertyPath(), "kind" => $v->getMessage()];
            }
        }

        $event->setResponse(new Response(\json_encode($errors), 400));
    }
}
```

### How to use

[](#how-to-use)

All root-request classes will need to be marked with the `RequestConverter\Annotation\Request` annotation. And every member of said request class can be marked with the `RequestConverter\Annotation\Type` annotation in order to force it to be coerced to said type, below is the reference on all possible types. Also you can mark them with the `RequestConverter\Annotation\Optional` annotation to ignore the absence of the field in the request payload.

By default RequestConverter will use the body of the request, but you can switch it to use the query for some of the action's parameters. In order to configure this you'll need to use `Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter` annotation with `options={"query"=true}` argument, like this:

```
/**
 * @ParamConverter("filter", options={"query"=true})
 */
public function filterPostsAction(Filter $filter)
{

}

```

#### Type annotation

[](#type-annotation)

There are several types you can use, those are: int, bool, float, string, array, map and or a class name. If the incoming data is not of the right type, we will try to coerce it if possible, here's a list of possible types and their coercion compatibility with others.

###### Int type

[](#int-type)

Apart from `int` it can take a numeric `string` that represents integer. Otherwise if it's a text string or if it represents float, you'll get `UncoercibleValueError`.

###### Bool type

[](#bool-type)

Can also accept `int`, `float` and `string`. For `int` and `float` as well as for numeric strings will yield `false` for `0` and `true` for all other numbers. For non-numeric strings it will first try to compare them with the preset values of `[yes, true, Y, T]` and `[no, false, N, F]`, if the match isn't found it should silently cast a string with `(bool)$value`.

###### Float type

[](#float-type)

Can also accept `int` and `string`. Will try to coerce `string` to `float` if the string represents float/integer value.

###### String type

[](#string-type)

Will accept `string`, `int` and `float` and will cast it to `string`.

###### Array type

[](#array-type)

Accepts array only.

This type is parametrized: for an array of `T` you can use `array` syntax (resembles syntax for generics in popular languages). If the `T` is a class name it will be mapped and validated as well.

###### Map type

[](#map-type)

Accepts array only.

This type is parametrized and has two overloads: `Map` and `Map`. The `K` type parameter can only be either `string` or `int`.

###### Class name type

[](#class-name-type)

If you use your own class name in the `RequestConverter\Annotation\Type` annotation or as an argument to the `array`or `Map` types, RequestConverter will try to map input data to the newly created object of said class.

#### Validation

[](#validation)

You can also use validation constraints for `symfony/validator` component. If conversion goes without errors the validator is applied with the default validation group.

### Extending RequestConverter

[](#extending-requestconverter)

You can add your own data types to be used with `Type` annotation and parametrized types - for that you'll need to create a new implementation of `RequestConverter\Coercion\TypeCoercer` interface and register it in the `RequestConverter\Coercer` service. Currently this service is not configurable so you'll have to use Symfony DI's feature of overriding service definitions and override the definition of the `request_converter.coercer` service.

### Contributing

[](#contributing)

All kinds of contributions are welcome: documentation, code or tests. The project mainly adheres to PSR-2 style guide.

You can run tests with `./vendor/bin/phpunit` command from the root of the project.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

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

---

Top Contributors

[![nikita2206](https://avatars.githubusercontent.com/u/1030688?v=4)](https://github.com/nikita2206 "nikita2206 (19 commits)")

### Embed Badge

![Health badge](/badges/nikita2206-symfony-request-converter/health.svg)

```
[![Health](https://phpackages.com/badges/nikita2206-symfony-request-converter/health.svg)](https://phpackages.com/packages/nikita2206-symfony-request-converter)
```

PHPackages © 2026

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