PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Validator/Constraints/EmailValidator.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 80 lines | 37 code | 13 blank | 30 comment | 11 complexity | 434465387e0e3e2a33c6e95fe83c73e5 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. /**
  15. * @api
  16. */
  17. class EmailValidator extends ConstraintValidator
  18. {
  19. /**
  20. * Checks if the passed value is valid.
  21. *
  22. * @param mixed $value The value that should be validated
  23. * @param Constraint $constraint The constraint for the validation
  24. *
  25. * @return Boolean Whether or not the value is valid
  26. *
  27. * @api
  28. */
  29. public function isValid($value, Constraint $constraint)
  30. {
  31. if (null === $value || '' === $value) {
  32. return true;
  33. }
  34. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  35. throw new UnexpectedTypeException($value, 'string');
  36. }
  37. $value = (string) $value;
  38. $valid = filter_var($value, FILTER_VALIDATE_EMAIL);
  39. if ($valid) {
  40. $host = substr($value, strpos($value, '@') + 1);
  41. if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
  42. // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
  43. $valid = false;
  44. }
  45. // Check MX records
  46. if ($valid && $constraint->checkMX) {
  47. $valid = $this->checkMX($host);
  48. }
  49. }
  50. if (!$valid) {
  51. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Check DNS Records for MX type.
  58. *
  59. * @param string $host Host name
  60. *
  61. * @return Boolean
  62. */
  63. private function checkMX($host)
  64. {
  65. return checkdnsrr($host, 'MX');
  66. }
  67. }