shooktea / request-deserializer-bundle
Small bundle for deserializing and validating requests from JSON to objects
Package info
github.com/ShookTea/RequestDeserializerBundle
Type:symfony-bundle
pkg:composer/shooktea/request-deserializer-bundle
1.0.0
2022-01-24 11:44 UTC
Requires
- php: 8.*
- symfony/framework-bundle: 6.*
- symfony/serializer: 6.*
- symfony/validator: 6.*
README
Tested on PHP 8.1 with Symfony 6.0.
Installation
Install with Composer:
composer require shooktea/request-deserializer-bundle
And add bundle to bundles.php (if was not added automatically by Symfony Flex):
<?php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], // ... ShookTea\RequestDeserializerBundle\ShookTeaRequestDeserializerBundle::class => ['all' => true], ];
How to use it?
- Create a class implementing
ShookTea\RequestDeserializerBundle\Query\QueryInterface(for GET parameters) orShookTea\RequestDeserializerBundle\Request\RequestInterface(for POST parameters) - add annotations from
symfony/serializerandsymfony/validatorcomponents if necessary - inject your class to controller method
And that's it!
Example query class: (for GET parameters)
<?php use ShookTea\RequestDeserializerBundle\Query\QueryInterface; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Serializer\Annotation\SerializedName; class ExampleQuery implements QueryInterface { #[SerializedName('id')] #[Assert\Uuid] public string $uuid; }
Usage in controller:
class ExampleController extends AbstractController { #[Route('/some/route')] public function someAction(ExampleQuery $query) { // do something with query } }
After requesting /some/route?id=217c26de-132e-49f7-a17d-fd80a70e3fe3, in someAction() method of controller,
object $query will have a $uuid with value "217c26de-132e-49f7-a17d-fd80a70e3fe3".