|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpKernel\Attribute\MapQueryString; |
| 16 | +use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; |
| 17 | +use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; |
| 18 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 19 | +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; |
| 20 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 21 | +use Symfony\Component\Validator\Exception\ValidationFailedException; |
| 22 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 23 | + |
| 24 | +final class MapQueryStringValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface |
| 25 | +{ |
| 26 | + private const CONTEXT = [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true]; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + private readonly DenormalizerInterface $normalizer, |
| 30 | + private readonly ?ValidatorInterface $validator, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @deprecated since Symfony 6.2, use resolve() instead |
| 36 | + */ |
| 37 | + public function supports(Request $request, ArgumentMetadata $argument): bool |
| 38 | + { |
| 39 | + @trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__); |
| 40 | + |
| 41 | + return 1 === \count($argument->getAttributes(MapQueryString::class, ArgumentMetadata::IS_INSTANCEOF)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return iterable<object> |
| 46 | + */ |
| 47 | + public function resolve(Request $request, ArgumentMetadata $argument): iterable |
| 48 | + { |
| 49 | + $attributes = $argument->getAttributes(MapQueryString::class, ArgumentMetadata::IS_INSTANCEOF); |
| 50 | + |
| 51 | + if (!$attributes) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + /** @var MapQueryString $attribute */ |
| 56 | + $attribute = $attributes[0]; |
| 57 | + |
| 58 | + $type = $argument->getType(); |
| 59 | + if (!$type) { |
| 60 | + throw new \LogicException(sprintf('Could not resolve the "$%s" controller argument: argument should be typed.', $argument->getName())); |
| 61 | + } |
| 62 | + |
| 63 | + $payload = $this->normalizer->denormalize( |
| 64 | + $request->query->all(), |
| 65 | + $type, |
| 66 | + 'json', |
| 67 | + $attribute->context + self::CONTEXT |
| 68 | + ); |
| 69 | + |
| 70 | + if ($this->validator) { |
| 71 | + $violations = $this->validator->validate($payload); |
| 72 | + |
| 73 | + if (\count($violations)) { |
| 74 | + throw new ValidationFailedException($payload, $violations); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return [$payload]; |
| 79 | + } |
| 80 | +} |
0 commit comments