EmailValidator.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Egulias\EmailValidator;
  3. use Egulias\EmailValidator\Exception\InvalidEmail;
  4. use Egulias\EmailValidator\Validation\EmailValidation;
  5. class EmailValidator
  6. {
  7. /**
  8. * @var EmailLexer
  9. */
  10. private $lexer;
  11. /**
  12. * @var array
  13. */
  14. protected $warnings;
  15. /**
  16. * @var InvalidEmail
  17. */
  18. protected $error;
  19. public function __construct()
  20. {
  21. $this->lexer = new EmailLexer();
  22. }
  23. /**
  24. * @param string $email
  25. * @param EmailValidation $emailValidation
  26. * @return bool
  27. */
  28. public function isValid($email, EmailValidation $emailValidation)
  29. {
  30. $isValid = $emailValidation->isValid($email, $this->lexer);
  31. $this->warnings = $emailValidation->getWarnings();
  32. $this->error = $emailValidation->getError();
  33. return $isValid;
  34. }
  35. /**
  36. * @return boolean
  37. */
  38. public function hasWarnings()
  39. {
  40. return !empty($this->warnings);
  41. }
  42. /**
  43. * @return array
  44. */
  45. public function getWarnings()
  46. {
  47. return $this->warnings;
  48. }
  49. /**
  50. * @return InvalidEmail
  51. */
  52. public function getError()
  53. {
  54. return $this->error;
  55. }
  56. }