-
-
Save fesor/fb1d53e8e4e427c59b930559da83d9a3 to your computer and use it in GitHub Desktop.
junk
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 | |
class RequestObjectResolver implements ArgumentValueResolverInterface | |
{ | |
private $denormalizer; | |
private $validator; | |
public function __construct( | |
DenormalizerInterface $denormalizer, | |
ValidatorInterface $validator | |
) | |
{ | |
$this->denormalizer = $denormalizer; | |
$this->validator = $validator; | |
} | |
public function supports(Request $request, ArgumentMetadata $argument) | |
{ | |
return is_subclass_of($argument->getType(), RequestObject::class); | |
} | |
public function resolve(Request $request, ArgumentMetadata $argument) | |
{ | |
$data = $request->request->all(); | |
if (Request::METHOD_GET === $request->getMethod()) { | |
$data = $request->query->all(); | |
} | |
$dto = $this->denormalizer->denormalize($data, $argument->getType()); | |
$this->validateDTO($dto); | |
yield $dto; | |
} | |
private function validateDTO($dto) | |
{ | |
$errors = $this->validator->validate($dto); | |
if (0 !== count($errors)) { | |
throw new InvalidRequestData($errors); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment