Compare commits

...

3 Commits

Author SHA1 Message Date
Michele Locati 758a77525b [1.x] Fix result of isValid when DNS lookup fails and strict is false (#172)
* Fix result of isValid when DNS lookup fails and strict is false

* Fix TravisCI for PHP 5.3

* Fix tests (isValid now returns false in case of errors in MX DNS lookups)

* Cache composer files

* Run TravisCI tests for PHP 7.1 and 7.2

* Use the PHPUnit installed by composer, not the system one

* Cache only composer packages, not their metadata
2018-09-25 22:59:41 +02:00
Sebastiaan Provost 5642614492 Backporting the changes made in #139 (#140)
* Backporting the changes made in https://github.com/egulias/EmailValidator/pull/139 so this functionality is also accessible in the 1.2.x releases.
This fix allows for more compliance with the DNS RFC. Explanation here: https://github.com/egulias/EmailValidator/pull/139/commits/08bac068c26edd210daa259a2819d7dfd7438091
2017-02-03 23:48:59 +01:00
Chris McCafferty b8bb147f46 Added EmailValidatorInterface (#120) 2016-07-03 23:52:18 +02:00
5 changed files with 88 additions and 33 deletions
+10 -2
View File
@@ -2,12 +2,17 @@ sudo: false
language: php
cache:
directories:
- $HOME/.composer/cache/files
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm
env:
@@ -18,6 +23,9 @@ matrix:
fast_finish: true
include:
- php: 5.3
dist: precise
- php: 5.3
dist: precise
env: deps=low
- php: 5.4
env: deps=no
@@ -31,5 +39,5 @@ install:
script:
- mkdir -p build/logs
- phpunit --coverage-clover build/logs/clover.xml
- composer test -- --coverage-clover build/logs/clover.xml
+3
View File
@@ -24,5 +24,8 @@
"psr-0": {
"Egulias\\": "src/"
}
},
"scripts": {
"test": "phpunit"
}
}
+12 -30
View File
@@ -7,7 +7,7 @@ namespace Egulias\EmailValidator;
*
* @author Eduardo Gulias Davis <me@egulias.com>
*/
class EmailValidator
class EmailValidator implements EmailValidatorInterface
{
/**
* Critical validation errors used to indicate that
@@ -113,26 +113,7 @@ class EmailValidator
}
/**
* Validates an email address against the following standards:
*
* RFC-5321: Simple Mail Transfer Protocol
* RFC-5322: Internet Message Format
* RFC-6530: Overview and Framework for Internationalized Email
* RFC-6531: SMTP Extension for Internationalized Email
* RFC-6532: Internationalized Email Headers
* RFC-1123 section 2.1: Requirements for Internet Hosts -- Application and Support
* RFC-4291 section 2.2: IP Version 6 Addressing Architecture
*
* @param string $email The email address to validate.
* @param bool $checkDNS Whether or not the email address's hostname should
* be confirmed with a DNS lookup. This only comes
* into play if strict mode is also enabled.
* @param bool $strict If this is true, and any informational warnings
* were raised during validation, the email address
* will be considered invalid. Additionally, if
* $checkDNS is true and the DNS lookup failed,
* the email address will be considered invalid.
* @return bool
* {@inheritdoc}
*/
public function isValid($email, $checkDNS = false, $strict = false)
{
@@ -152,11 +133,11 @@ class EmailValidator
return false;
}
return ($strict ? (!$this->hasWarnings() && !$dnsProblemExists) : true);
return !($dnsProblemExists || $strict && $this->hasWarnings());
}
/**
* @return boolean
* {@inheritdoc}
*/
public function hasWarnings()
{
@@ -164,7 +145,7 @@ class EmailValidator
}
/**
* @return array
* {@inheritdoc}
*/
public function getWarnings()
{
@@ -172,7 +153,7 @@ class EmailValidator
}
/**
* @return string
* {@inheritdoc}
*/
public function getError()
{
@@ -180,9 +161,7 @@ class EmailValidator
}
/**
* @param int $threshold
*
* @return EmailValidator
* {@inheritdoc}
*/
public function setThreshold($threshold)
{
@@ -192,7 +171,7 @@ class EmailValidator
}
/**
* @return int
* {@inheritdoc}
*/
public function getThreshold()
{
@@ -205,7 +184,10 @@ class EmailValidator
*/
protected function checkDNS()
{
$mxRecordExists = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'MX');
$host = $this->parser->getParsedDomainPart();
$host = rtrim($host, '.') . '.';
$mxRecordExists = checkdnsrr($host, 'MX');
if (!$mxRecordExists) {
$this->warnings[] = self::DNSWARN_NO_RECORD;
@@ -0,0 +1,62 @@
<?php
namespace Egulias\EmailValidator;
/**
* EmailValidatorInterface
*
* @author Chris McCafferty <cilefen@gmail.com>
*/
interface EmailValidatorInterface
{
/**
* Validates an email address against the following standards:
*
* RFC-5321: Simple Mail Transfer Protocol
* RFC-5322: Internet Message Format
* RFC-6530: Overview and Framework for Internationalized Email
* RFC-6531: SMTP Extension for Internationalized Email
* RFC-6532: Internationalized Email Headers
* RFC-1123 section 2.1: Requirements for Internet Hosts -- Application and Support
* RFC-4291 section 2.2: IP Version 6 Addressing Architecture
*
* @param string $email The email address to validate.
* @param bool $checkDNS Whether or not the email address's hostname should
* be confirmed with a DNS lookup. This only comes
* into play if strict mode is also enabled.
* @param bool $strict If this is true, and any informational warnings
* were raised during validation, the email address
* will be considered invalid. Additionally, if
* $checkDNS is true and the DNS lookup failed,
* the email address will be considered invalid.
* @return bool
*/
public function isValid($email, $checkDNS = false, $strict = false);
/**
* @return bool
*/
public function hasWarnings();
/**
* @return array
*/
public function getWarnings();
/**
* @return string
*/
public function getError();
/**
* @param int $threshold The acceptable number of deprecation warnings.
*
* @return EmailValidator
*/
public function setThreshold($threshold);
/**
* @return int
*/
public function getThreshold();
}
@@ -180,7 +180,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
*/
public function testValidEmailsWithWarningsCheck($warnings, $email)
{
$this->assertTrue($this->validator->isValid($email, true));
$this->assertFalse($this->validator->isValid($email, true));
$this->assertEquals($warnings, $this->validator->getWarnings());
}