Ne-Lexa /
RequestDtoBundle
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Nelexa\RequestDtoBundle\ArgumentResolver; |
||
| 6 | |||
| 7 | use Nelexa\RequestDtoBundle\Dto\RequestDtoInterface; |
||
| 8 | use Nelexa\RequestDtoBundle\Transform\RequestDtoTransform; |
||
| 9 | use Symfony\Component\HttpFoundation\Request; |
||
| 10 | use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; |
||
| 11 | use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
||
| 12 | use Symfony\Component\HttpKernel\Exception\HttpException; |
||
| 13 | use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
||
| 14 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
||
| 15 | |||
| 16 | final class RequestDtoValueResolver implements ArgumentValueResolverInterface |
||
| 17 | { |
||
| 18 | private RequestDtoTransform $transformer; |
||
| 19 | |||
| 20 | private ValidatorInterface $validator; |
||
| 21 | |||
| 22 | 26 | public function __construct(RequestDtoTransform $transformer, ValidatorInterface $validator) |
|
| 23 | { |
||
| 24 | 26 | $this->transformer = $transformer; |
|
| 25 | 26 | $this->validator = $validator; |
|
| 26 | 26 | } |
|
| 27 | |||
| 28 | 26 | public function supports(Request $request, ArgumentMetadata $argument): bool |
|
| 29 | { |
||
| 30 | 26 | return is_a($argument->getType(), RequestDtoInterface::class, true); |
|
| 31 | } |
||
| 32 | |||
| 33 | 25 | private function getSerializeFormat(Request $request): string |
|
| 34 | { |
||
| 35 | 25 | static $supportFormats = ['json', 'xml']; |
|
| 36 | 25 | static $defaultFormat = 'json'; |
|
| 37 | |||
| 38 | 25 | $format = $request->getContentType() ?? $defaultFormat; |
|
| 39 | |||
| 40 | 25 | if (!\in_array($format, $supportFormats, true)) { |
|
| 41 | 10 | $format = $defaultFormat; |
|
| 42 | } |
||
| 43 | |||
| 44 | 25 | return $format; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return iterable |
||
| 49 | */ |
||
| 50 | 25 | public function resolve(Request $request, ArgumentMetadata $argument) |
|
| 51 | { |
||
| 52 | 25 | $format = $this->getSerializeFormat($request); |
|
| 53 | |||
| 54 | try { |
||
| 55 | 25 | $obj = $this->transformer->transform($request, $argument->getType(), $format); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 56 | 3 | } catch (\TypeError | NotEncodableValueException $e) { |
|
| 57 | 2 | $problemMimeType = 'application/problem+' . $format; |
|
| 58 | |||
| 59 | 2 | throw new HttpException( |
|
| 60 | 2 | 400, |
|
| 61 | 2 | 'Bad Request', |
|
| 62 | $e, |
||
| 63 | [ |
||
| 64 | 2 | 'Content-Type' => $problemMimeType, |
|
| 65 | ] |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | 22 | $request->attributes->set( |
|
| 70 | 22 | ConstraintViolationListValueResolver::REQUEST_ATTR_KEY, |
|
| 71 | 22 | $this->validator->validate($obj) |
|
| 72 | ); |
||
| 73 | |||
| 74 | 22 | yield $obj; |
|
| 75 | 22 | } |
|
| 76 | } |
||
| 77 |