Last active
May 18, 2022 10:31
-
-
Save enleur/5348eeef144d51e3fb7b0dc45ce46255 to your computer and use it in GitHub Desktop.
Symfony json body to request object auto mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Request\ParamConverter; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Validator\ConstraintViolationListInterface; | |
final class ConstraintViolationsConverter implements ParamConverterInterface | |
{ | |
/** | |
* @param Request $request | |
* @param ParamConverter $configuration | |
* | |
* @return bool|void | |
*/ | |
public function apply(Request $request, ParamConverter $configuration) | |
{ | |
$violations = $request->attributes->get('_violations'); | |
$request->attributes->remove('_violations'); | |
$request->attributes->set($configuration->getName(), $violations); | |
} | |
/** | |
* @param ParamConverter $configuration | |
* | |
* @return bool | |
*/ | |
public function supports(ParamConverter $configuration) | |
{ | |
return ConstraintViolationListInterface::class === $configuration->getClass(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
final class ExampleController extends AbstractController | |
{ | |
public function registerAction( | |
RegistrationRequest $registrationRequest, | |
ConstraintViolationListInterface $violations, | |
Registration $registration | |
): Response { | |
if ($violations->count() > 0) { | |
return $this->validationError($violations); | |
} | |
//... | |
return $this->json(..); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Request\ParamConverter; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Serializer\SerializerInterface; | |
use Symfony\Component\Validator\Validator\ValidatorInterface; | |
final class JsonBodySerializableConverter implements ParamConverterInterface | |
{ | |
/** | |
* @var SerializerInterface | |
*/ | |
private $serializer; | |
/** | |
* @var ValidatorInterface | |
*/ | |
private $validator; | |
/** | |
* @param SerializerInterface $serializer | |
* @param ValidatorInterface $validator | |
*/ | |
public function __construct(SerializerInterface $serializer, ValidatorInterface $validator) | |
{ | |
$this->serializer = $serializer; | |
$this->validator = $validator; | |
} | |
/** | |
* @param Request $request | |
* @param ParamConverter $configuration | |
* | |
* @return bool|void | |
*/ | |
public function apply(Request $request, ParamConverter $configuration) | |
{ | |
$body = $request->getContent(); | |
$obj = $this->serializer->deserialize($body, $configuration->getClass(), 'json'); | |
$violationList = $this->validator->validate($obj); | |
$request->attributes->set('_violations', $violationList); | |
$request->attributes->set($configuration->getName(), $obj); | |
} | |
/** | |
* @param ParamConverter $configuration | |
* | |
* @return bool | |
*/ | |
public function supports(ParamConverter $configuration) | |
{ | |
$class = $configuration->getClass(); | |
if (!is_string($class)) { | |
return false; | |
} | |
return in_array(JsonBodySerializableInterface::class, class_implements($class)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Request; | |
use App\Request\ParamConverter\JsonBodySerializableInterface; | |
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface; | |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | |
use Symfony\Component\Validator\Constraints as Assert; | |
final class RegistrationRequest implements JsonBodySerializableInterface | |
{ | |
/** | |
* @Assert\NotBlank | |
* @Assert\Type("string") | |
* | |
* @var string | |
*/ | |
public $username; | |
/** | |
* @Assert\NotBlank | |
* @Assert\Type("string") | |
* | |
* @var string | |
*/ | |
public $email; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
both JsonBodySerializableInterface is missing but thanks for sharing the converters anyway