Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active March 4, 2021 08:33
Show Gist options
  • Save fesor/fb1d53e8e4e427c59b930559da83d9a3 to your computer and use it in GitHub Desktop.
Save fesor/fb1d53e8e4e427c59b930559da83d9a3 to your computer and use it in GitHub Desktop.
junk
<?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