Skip to content

Instantly share code, notes, and snippets.

@mdzwigala
Created December 4, 2020 11:49
Show Gist options
  • Save mdzwigala/83617b9d1a95fa906b84e0b7179e876c to your computer and use it in GitHub Desktop.
Save mdzwigala/83617b9d1a95fa906b84e0b7179e876c to your computer and use it in GitHub Desktop.
ADR-EXAMPLE\Infrastructuer\SymfonyValidator
<?php
declare(strict_types=1);
namespace App\Infrastructure\Validator;
use App\Infrastructure\Exception\DataValidationException;
use App\Infrastructure\Service\SymfonyViolationListConverter;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class SymfonyValidator implements DataValidator
{
private ValidatorInterface $validator;
private SymfonyViolationListConverter $converter;
public function __construct(ValidatorInterface $validator, SymfonyViolationListConverter $converter)
{
$this->validator = $validator;
$this->converter = $converter;
}
public function validate($data): void
{
$errors = $this->validator->validate($data);
if (count($errors) === 0) {
return;
}
throw new DataValidationException($this->converter->convertToArray($errors));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment