Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. RequestPayloadValueResolver.php

function RequestPayloadValueResolver::onKernelControllerArguments

File

vendor/symfony/http-kernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php, line 102

Class

RequestPayloadValueResolver
@author Konstantin Myakshin <molodchick@gmail.com>

Namespace

Symfony\Component\HttpKernel\Controller\ArgumentResolver

Code

public function onKernelControllerArguments(ControllerArgumentsEvent $event) : void {
    $arguments = $event->getArguments();
    foreach ($arguments as $i => $argument) {
        if ($argument instanceof MapQueryString) {
            $payloadMapper = $this->mapQueryString(...);
            $validationFailedCode = $argument->validationFailedStatusCode;
        }
        elseif ($argument instanceof MapRequestPayload) {
            $payloadMapper = $this->mapRequestPayload(...);
            $validationFailedCode = $argument->validationFailedStatusCode;
        }
        elseif ($argument instanceof MapUploadedFile) {
            $payloadMapper = $this->mapUploadedFile(...);
            $validationFailedCode = $argument->validationFailedStatusCode;
        }
        else {
            continue;
        }
        $request = $event->getRequest();
        if (!$argument->metadata
            ->getType()) {
            throw new \LogicException(\sprintf('Could not resolve the "$%s" controller argument: argument should be typed.', $argument->metadata
                ->getName()));
        }
        if ($this->validator) {
            $violations = new ConstraintViolationList();
            try {
                $payload = $payloadMapper($request, $argument->metadata, $argument);
            } catch (PartialDenormalizationException $e) {
                $trans = $this->translator ? $this->translator
                    ->trans(...) : fn($m, $p) => strtr($m, $p);
                foreach ($e->getErrors() as $error) {
                    $parameters = [];
                    $template = 'This value was of an unexpected type.';
                    if ($expectedTypes = $error->getExpectedTypes()) {
                        $template = 'This value should be of type {{ type }}.';
                        $parameters['{{ type }}'] = implode('|', $expectedTypes);
                    }
                    if ($error->canUseMessageForUser()) {
                        $parameters['hint'] = $error->getMessage();
                    }
                    $message = $trans($template, $parameters, $this->translationDomain);
                    $violations->add(new ConstraintViolation($message, $template, $parameters, null, $error->getPath(), null));
                }
                $payload = $e->getData();
            }
            if (null !== $payload && !\count($violations)) {
                $constraints = $argument->constraints ?? null;
                if (\is_array($payload) && !empty($constraints) && !$constraints instanceof Assert\All) {
                    $constraints = new Assert\All($constraints);
                }
                $violations->addAll($this->validator
                    ->validate($payload, $constraints, $argument->validationGroups ?? null));
            }
            if (\count($violations)) {
                throw HttpException::fromStatusCode($validationFailedCode, implode("\n", array_map(static fn($e) => $e->getMessage(), iterator_to_array($violations))), new ValidationFailedException($payload, $violations));
            }
        }
        else {
            try {
                $payload = $payloadMapper($request, $argument->metadata, $argument);
            } catch (PartialDenormalizationException $e) {
                throw HttpException::fromStatusCode($validationFailedCode, implode("\n", array_map(static fn($e) => $e->getMessage(), $e->getErrors())), $e);
            }
        }
        if (null === $payload) {
            $payload = match (true) {    $argument->metadata
                    ->hasDefaultValue() => $argument->metadata
                    ->getDefaultValue(),
                $argument->metadata
                    ->isNullable() => null,
                default => throw HttpException::fromStatusCode($validationFailedCode),
            
            };
        }
        $arguments[$i] = $payload;
    }
    $event->setArguments($arguments);
}
RSS feed
Powered by Drupal