-
-
Save mdzwigala/83617b9d1a95fa906b84e0b7179e876c to your computer and use it in GitHub Desktop.
ADR-EXAMPLE\Infrastructuer\SymfonyValidator
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 | |
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