MessageIDValidation.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Egulias\EmailValidator\Validation;
  3. use Egulias\EmailValidator\EmailLexer;
  4. use Egulias\EmailValidator\MessageIDParser;
  5. use Egulias\EmailValidator\Result\InvalidEmail;
  6. use Egulias\EmailValidator\Result\Reason\ExceptionFound;
  7. class MessageIDValidation implements EmailValidation
  8. {
  9. /**
  10. * @var array
  11. */
  12. private $warnings = [];
  13. /**
  14. * @var ?InvalidEmail
  15. */
  16. private $error;
  17. public function isValid(string $email, EmailLexer $emailLexer): bool
  18. {
  19. $parser = new MessageIDParser($emailLexer);
  20. try {
  21. $result = $parser->parse($email);
  22. $this->warnings = $parser->getWarnings();
  23. if ($result->isInvalid()) {
  24. /** @psalm-suppress PropertyTypeCoercion */
  25. $this->error = $result;
  26. return false;
  27. }
  28. } catch (\Exception $invalid) {
  29. $this->error = new InvalidEmail(new ExceptionFound($invalid), '');
  30. return false;
  31. }
  32. return true;
  33. }
  34. public function getWarnings(): array
  35. {
  36. return $this->warnings;
  37. }
  38. public function getError(): ?InvalidEmail
  39. {
  40. return $this->error;
  41. }
  42. }