mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-03 14:10:40 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b366d54b0a | |||
| 2bc46e23e5 | |||
| 7701238433 | |||
| afde3d3874 | |||
| e9c46eed92 |
@@ -0,0 +1,4 @@
|
|||||||
|
/Tests export-ignore
|
||||||
|
/documentation export-ignore
|
||||||
|
/.* export-ignore
|
||||||
|
/phpunit.xml.dist
|
||||||
@@ -58,7 +58,7 @@ class EmailValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return InvalidEmail
|
||||||
*/
|
*/
|
||||||
public function getError()
|
public function getError()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,16 +3,12 @@
|
|||||||
namespace Egulias\EmailValidator\Validation;
|
namespace Egulias\EmailValidator\Validation;
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
|
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||||
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
|
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
|
||||||
use Egulias\EmailValidator\Exception\NoDNSRecord;
|
use Egulias\EmailValidator\Exception\NoDNSRecord;
|
||||||
|
|
||||||
class DNSCheckValidation implements EmailValidation
|
class DNSCheckValidation implements EmailValidation
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var EmailParser
|
|
||||||
*/
|
|
||||||
private $parser;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
@@ -27,10 +23,10 @@ class DNSCheckValidation implements EmailValidation
|
|||||||
{
|
{
|
||||||
// use the input to check DNS if we cannot extract something similar to a domain
|
// use the input to check DNS if we cannot extract something similar to a domain
|
||||||
$host = $email;
|
$host = $email;
|
||||||
|
|
||||||
// Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
|
// Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
|
||||||
$pattern = "/^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+)+)+$/";
|
if (false !== $lastAtPos = strrpos($email, '@')) {
|
||||||
if (preg_match($pattern, $email, $result)) {
|
$host = substr($email, $lastAtPos + 1);
|
||||||
$host = $this->extractHost($result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->checkDNS($host);
|
return $this->checkDNS($host);
|
||||||
@@ -46,16 +42,6 @@ class DNSCheckValidation implements EmailValidation
|
|||||||
return $this->warnings;
|
return $this->warnings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extractHost(array $result)
|
|
||||||
{
|
|
||||||
foreach ($result as $match) {
|
|
||||||
$onlyDomainPattern = "/^([a-z0-9]+([._-][a-z0-9]+))+$/";
|
|
||||||
if (preg_match($onlyDomainPattern, $match, $domainResult)) {
|
|
||||||
return $domainResult[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function checkDNS($host)
|
protected function checkDNS($host)
|
||||||
{
|
{
|
||||||
$Aresult = true;
|
$Aresult = true;
|
||||||
@@ -63,7 +49,7 @@ class DNSCheckValidation implements EmailValidation
|
|||||||
|
|
||||||
if (!$MXresult) {
|
if (!$MXresult) {
|
||||||
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
|
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
|
||||||
$Aresult = checkdnsrr($host, 'A');
|
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
|
||||||
if (!$Aresult) {
|
if (!$Aresult) {
|
||||||
$this->error = new NoDNSRecord();
|
$this->error = new NoDNSRecord();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace Egulias\EmailValidator\Validation;
|
namespace Egulias\EmailValidator\Validation;
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
|
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||||
|
|
||||||
interface EmailValidation
|
interface EmailValidation
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace Egulias\EmailValidator\Validation;
|
namespace Egulias\EmailValidator\Validation;
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
|
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||||
use Egulias\EmailValidator\Validation\Error\SpoofEmail;
|
use Egulias\EmailValidator\Validation\Error\SpoofEmail;
|
||||||
use \Spoofchecker;
|
use \Spoofchecker;
|
||||||
|
|
||||||
|
|||||||
@@ -32,10 +32,10 @@ $validator->isValid("example@example.com", new RFCValidation()) //true
|
|||||||
2. [NoWarningsRFCValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/NoRFCWarningsValidation.php)
|
2. [NoWarningsRFCValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/NoRFCWarningsValidation.php)
|
||||||
3. [DNSCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/DNSCheckValidation.php)
|
3. [DNSCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/DNSCheckValidation.php)
|
||||||
4. [SpoofCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php)
|
4. [SpoofCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php)
|
||||||
5. [MultipleValidationsWithAnd](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/MultipleValidationWithAnd.php)
|
5. [MultipleValidationWithAnd](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/MultipleValidationWithAnd.php)
|
||||||
6. [Your own validation](#how-to-extend)
|
6. [Your own validation](#how-to-extend)
|
||||||
|
|
||||||
`MultipleValidationsWithAnd`
|
`MultipleValidationWithAnd`
|
||||||
|
|
||||||
It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
|
It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
|
||||||
|
|
||||||
@@ -43,9 +43,12 @@ It is a validation that operates over other validations performing a logical and
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailValidator;
|
use Egulias\EmailValidator\EmailValidator;
|
||||||
|
use Egulias\EmailValidator\Validation\DNSCheckValidation;
|
||||||
|
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
|
||||||
|
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||||
|
|
||||||
$validator = new EmailValidator();
|
$validator = new EmailValidator();
|
||||||
$multipleValidations = new MultipleValidationsWithAnd([
|
$multipleValidations = new MultipleValidationWithAnd([
|
||||||
new RFCValidation(),
|
new RFCValidation(),
|
||||||
new DNSCheckValidation()
|
new DNSCheckValidation()
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -9,10 +9,30 @@ use Egulias\EmailValidator\Warning\NoDNSMXRecord;
|
|||||||
|
|
||||||
class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testValidDNS()
|
public function validEmailsProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// dot-atom
|
||||||
|
['Abc@example.com'],
|
||||||
|
['ABC@EXAMPLE.COM'],
|
||||||
|
['Abc.123@example.com'],
|
||||||
|
['user+mailbox/department=shipping@example.com'],
|
||||||
|
['!#$%&\'*+-/=?^_`.{|}~@example.com'],
|
||||||
|
|
||||||
|
// quoted string
|
||||||
|
['"Abc@def"@example.com'],
|
||||||
|
['"Fred\ Bloggs"@example.com'],
|
||||||
|
['"Joe.\\Blow"@example.com'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider validEmailsProvider
|
||||||
|
*/
|
||||||
|
public function testValidDNS($validEmail)
|
||||||
{
|
{
|
||||||
$validation = new DNSCheckValidation();
|
$validation = new DNSCheckValidation();
|
||||||
$this->assertTrue($validation->isValid("example@example.com", new EmailLexer()));
|
$this->assertTrue($validation->isValid($validEmail, new EmailLexer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvalidDNS()
|
public function testInvalidDNS()
|
||||||
|
|||||||
Reference in New Issue
Block a user