-
-
Save AndreTeros/c4e3c1f2d6b056d6e74b24fdc6b35aee to your computer and use it in GitHub Desktop.
This file contains hidden or 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 declare(strict_types=1); | |
| namespace Common\ArgumentResolver; | |
| use Common\Exception\ValidationErrorException; | |
| use Common\Request\RequestData; | |
| use Symfony\Component\HttpFoundation\Request; | |
| use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | |
| use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | |
| use Symfony\Component\Validator\Validator\ValidatorInterface; | |
| class RequestResolver implements ArgumentValueResolverInterface | |
| { | |
| private $validator; | |
| public function __construct(ValidatorInterface $validator) | |
| { | |
| $this->validator = $validator; | |
| } | |
| public function supports(Request $request, ArgumentMetadata $argumentMetadata): bool | |
| { | |
| return is_subclass_of($argumentMetadata->getType(), RequestData::class); | |
| } | |
| public function resolve(Request $request, ArgumentMetadata $argumentMetadata): \Generator | |
| { | |
| /** @var RequestData $requestData */ | |
| $requestData = $argumentMetadata->getType(); | |
| /** @var RequestData $mapped */ | |
| $mapped = $requestData::create($request->request); | |
| $errors = $this->validator->validate($mapped); | |
| if (count($errors)) { | |
| throw new ValidationErrorException($errors); | |
| } | |
| yield $mapped; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment