src/Service/Validator/ValidatorService.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Service\Validator;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Validator\Validator\ValidatorInterface;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. class ValidatorService
  7. {
  8.     private $validator;
  9.     private $requestStack;
  10.     public function __construct(
  11.         ValidatorInterface            $validator,
  12.         RequestStack                  $requestStack,
  13.         protected TranslatorInterface $translator,
  14.     )
  15.     {
  16.         $this->validator $validator;
  17.         $this->requestStack $requestStack;
  18.     }
  19.     public function validate($constraints)
  20.     {
  21.         if (!empty($this->requestStack->getCurrentRequest()->getContent())) {
  22.             $input json_decode($this->requestStack->getCurrentRequest()->getContent(), true);
  23.         } else {
  24.             $input $this->requestStack->getCurrentRequest()->request->all();
  25.         }
  26.         if (empty($input)) {
  27.             $input = [];
  28.         }
  29.         $validationResult $this->validator->validate($input$constraints);
  30.         foreach ($validationResult as $violation) {
  31.             throw  new MessageException($violation->getPropertyPath(), $violation->getMessage());
  32.         }
  33.         foreach ($input as $key => $value) {
  34.             if (is_string($value))
  35.                 $input[$key] = trim($value);
  36.         }
  37.         return $input;
  38.     }
  39.     public function validateWithoutError($constraints)
  40.     {
  41.         if (!empty($this->requestStack->getCurrentRequest()->getContent())) {
  42.             $input json_decode($this->requestStack->getCurrentRequest()->getContent(), true);
  43.         } else {
  44.             $input $this->requestStack->getCurrentRequest()->request->all();
  45.         }
  46.         if (empty($input)) {
  47.             $input = [];
  48.         }
  49.         if (isset($input['email'])) {
  50.             $email $input['email'];
  51.             $accents '/&([A-Za-z]{1,2})(grave|acute|circ|cedil|uml|lig);/';
  52.             $string_encoded htmlentities($emailENT_NOQUOTES'UTF-8');
  53.             $email preg_replace($accents'$1'$string_encoded);
  54.             $input['email'] = $email;
  55.         }
  56.         $validationResult $this->validator->validate($input$constraints);
  57.         foreach ($validationResult as $violation) {
  58.             $message $violation->getMessage();
  59.             if ($violation->getMessageTemplate() == 'This field is missing.') {
  60.                 $message $this->translator->trans('api.fillRequiredField');
  61.             }
  62.             throw  new MessageException(''$message);
  63.         }
  64.         foreach ($input as $key => $value) {
  65.             if (is_string($value))
  66.                 $input[$key] = trim($value);
  67.         }
  68.         return $input;
  69.     }
  70. }