Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Validator\Model;
  6.  
  7. use App\Exception\InvalidDTOException;
  8. use Symfony\Component\Validator\ConstraintViolationList;
  9. use Symfony\Component\Validator\Validator\ValidatorInterface;
  10.  
  11. final class DTOValidator implements DTOValidatorInterface
  12. {
  13.     /** @var ValidatorInterface */
  14.     private $validator;
  15.  
  16.     public function __construct(ValidatorInterface $validator)
  17.     {
  18.         $this->validator = $validator;
  19.     }
  20.  
  21.     public function validate(...$DTOs): void
  22.     {
  23.         $validationResults = new ConstraintViolationList();
  24.  
  25.         foreach ($DTOs as $DTO) {
  26.             $validationResults->addAll($this->validator->validate($DTO));
  27.         }
  28.  
  29.         if (0 !== count($validationResults)) {
  30.             throw new InvalidDTOException();
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement