Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mops1k/request-object-resolver-bundle

This bundle can help you to deserialize incoming request parameters from symfomy http request object to your DTO objects. This is a better alternative for new symfony request DTO resolvers.
https://github.com/mops1k/request-object-resolver-bundle

bundle php symfony symfony-bundle

Last synced: about 2 months ago
JSON representation

This bundle can help you to deserialize incoming request parameters from symfomy http request object to your DTO objects. This is a better alternative for new symfony request DTO resolvers.

Awesome Lists containing this project

README

        

# RequestObjectResolverBundle
This bundle can help you to deserialize incoming request parameters from symfomy http request object to your DTO objects.

Deserialized objects are validated via [symfony/validator](https://symfony.com/doc/current/validation.html), so when using such objects in
controllers, we can be sure that the data format and their set in the object are correct and ready for further processing.

Bundle can deserialize:
- route parameters (attribute `RequestObjectResolverBundle\Attribute\Path`)
- query parameters (attribute `RequestObjectResolverBundle\Attribute\Query`)
- content body (supports all symfony serializer formats) (attribute `RequestObjectResolverBundle\Attribute\Content`)
- form parameters (attribute `RequestObjectResolverBundle\Attribute\Form`)
- uploaded files (attribute `RequestObjectResolverBundle\Attribute\Form`)

## Install
```bash
composer require mops1k/request-object-resolver-bundle
```

## Use
Example:

```php
$exampleRequest->id,
'name' => $exampleRequest->name,
]);
}
}
```

## Map field to another name
Whole library attributes have a map parameter. With this parameter you can map from one field name to another.

Example:

```php
'title']), Path] ExampleRequest $exampleRequest): JsonResponse
{
// some logic with $exampleRequest

return new JsonResponse([
'id' => $exampleRequest->id,
'title' => $exampleRequest->name,
]);
}
}
```

## Skip dto validation
If your logic does not need automatic validation of the request object for some reason, then you can disable it
with `RequestObjectResolverBundle\Attribute\SkipValidation` attribute.

Example:

```php
$exampleRequest->id,
'name' => $exampleRequest->name,
]);
}
}
```

## Validation groups
If you want to use validation groups, then use attribute `\RequestObjectResolverBundle\Attribute\ValidationGroups`.

Example:

```php
$exampleRequest->id,
'name' => $exampleRequest->name,
]);
}
}
```

## Serialization context
If you want to add some serialization context, then in attributes you can set `serializationContext` property.

Example:

```php
$exampleRequest->id,
'name' => $exampleRequest->name, // will throw error as uninitialized property
]);
}
}
```

If you want to set context to all request parts which you want to deserialize to object,
use `\RequestObjectResolverBundle\Attribute\SerializerContext` attribute.

```php
$exampleRequest->id,
'name' => $exampleRequest->name, // will throw error as uninitialized property
]);
}
}
```

## Overriding values with request parts combination
These are table of request parts priority overriding (if have same key name):

Request part|Priority (lower value = higher priority)
------------|----------------------------------------
Query|30
Path|20
Form|10
Content|0

Example. If you handle `Path`, `Query` and `Content` in same object and all of them have same field (id for example),
then resulting field value will be from `Form` request part.