EmailValidator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 $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. }