Compare commits

..

13 Commits

Author SHA1 Message Date
Eduardo Gulias Davis 30562b69fc Merge branch 'taz77-correct-readme-php-example-code-php-dependencies' 2016-08-07 23:18:59 +02:00
Eduardo Gulias Davis cbb8b9e152 PHPStorm help and composer suggestion 2016-08-07 23:16:06 +02:00
Eduardo Gulias Davis cf670406e5 Merge branch 'correct-readme-php-example-code-php-dependencies' of https://github.com/taz77/EmailValidator into taz77-correct-readme-php-example-code-php-dependencies 2016-08-07 23:11:36 +02:00
Issei.M 4d446bf971 NoRFCWarningsValidation now has an InvalidEmail instance if invalid email is passed (#126)
* NoRFCWarningsValidation now has an InvalidEmail instance if invalid email is passed

* fix mentioned point
2016-08-03 22:42:12 +02:00
Issei.M 4f0f9023f0 MultipleValidationWithAnd::getError() now returns NULL if no errors (#124)
* MultipleValidationWithAnd::getError() now returns NULL if no errors

* improve adding error logic

* reword something, add tips

* fix CS
remove wrong tips
2016-07-29 22:43:15 +02:00
Brady 9160e26b13 List and links
Turned requirements into an unordered list. Removed the PHPstorm logo as it doesn't exist and could not find one to replace it.
2016-07-19 06:49:04 +02:00
Brady eb036b7d1c Correct PHP example. Add a requirements section.
Corrected PHP example for missing semi-colons. Added a new requirements section that explains the need for composer and the need of the PHP Intl library to use the spoof validation.
2016-07-18 11:55:21 +02:00
Issei.M 19811e0082 MultipleValidatorWithAnd now can break out of loop when error occurs (#122)
* MultipleValidatorWithAnd now can break out of loop when error occurs

* improved logic

* updated doc

* bugfix
2016-07-12 15:22:10 +02:00
Michele Locati b366d54b0a Exclude some dirs and files from repository auto-generated ZIP archives (#113)
People that install this library via composer don't need the test stuff, they just need the library itself.
This PR removes the development directories and files from the repository auto-generated ZIP archives.
Since these development files/directories will still be available via `git clone`, Travis is happy, developers are happy, and so are end-users (they won't have unneeded stuff in production machines).
2016-07-03 23:53:17 +02:00
Issei.M 2bc46e23e5 fixed type missing and removed unused code (#117) 2016-06-25 00:47:43 +02:00
Issei.M 7701238433 fixed some problems in DNSCheckValidation (#116) 2016-06-25 00:43:29 +02:00
Issei.M afde3d3874 fixed DNSCheckValidation bug (#115) 2016-06-21 17:31:36 +02:00
Pedro Maia e9c46eed92 Fix typo (#112) 2016-05-19 00:20:10 +02:00
14 changed files with 246 additions and 61 deletions
+4
View File
@@ -0,0 +1,4 @@
/Tests export-ignore
/documentation export-ignore
/.* export-ignore
/phpunit.xml.dist
+1 -1
View File
@@ -58,7 +58,7 @@ class EmailValidator
}
/**
* @return string
* @return InvalidEmail
*/
public function getError()
{
@@ -3,16 +3,12 @@
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\InvalidEmail;
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
use Egulias\EmailValidator\Exception\NoDNSRecord;
class DNSCheckValidation implements EmailValidation
{
/**
* @var EmailParser
*/
private $parser;
/**
* @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
$host = $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 (preg_match($pattern, $email, $result)) {
$host = $this->extractHost($result);
if (false !== $lastAtPos = strrpos($email, '@')) {
$host = substr($email, $lastAtPos + 1);
}
return $this->checkDNS($host);
@@ -46,16 +42,6 @@ class DNSCheckValidation implements EmailValidation
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)
{
$Aresult = true;
@@ -63,7 +49,7 @@ class DNSCheckValidation implements EmailValidation
if (!$MXresult) {
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
$Aresult = checkdnsrr($host, 'A');
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
if (!$Aresult) {
$this->error = new NoDNSRecord();
}
+16 -2
View File
@@ -3,18 +3,32 @@
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\InvalidEmail;
use Egulias\EmailValidator\Warning\Warning;
interface EmailValidation
{
/**
* Returns true if the given email is valid.
*
* @param string $email The email you want to validate.
* @param EmailLexer $emailLexer The email lexer.
*
* @return bool
*/
public function isValid($email, EmailLexer $emailLexer);
/**
* @return InvalidEmail
* Returns the validation error.
*
* @return InvalidEmail|null
*/
public function getError();
/**
* @return array of Warning
* Returns the validation warnings.
*
* @return Warning[]
*/
public function getWarnings();
}
@@ -0,0 +1,11 @@
<?php
namespace Egulias\EmailValidator\Validation\Error;
use Egulias\EmailValidator\Exception\InvalidEmail;
class RFCWarnings extends InvalidEmail
{
const CODE = 997;
const REASON = 'Warnings were found.';
}
@@ -7,19 +7,55 @@ use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
class MultipleValidationWithAnd implements EmailValidation
{
/**
* If one of validations gets failure skips all succeeding validation.
* This means MultipleErrors will only contain a single error which first found.
*/
const STOP_ON_ERROR = 0;
/**
* All of validations will be invoked even if one of them got failure.
* So MultipleErrors will contain all causes.
*/
const ALLOW_ALL_ERRORS = 1;
/**
* @var EmailValidation[]
*/
private $validations = [];
/**
* @var array
*/
private $warnings = [];
/**
* @var MultipleErrors
*/
private $error;
public function __construct(array $validations)
/**
* @var bool
*/
private $mode;
/**
* @param EmailValidation[] $validations The validations.
* @param int $mode The validation mode (one of the constants).
*/
public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
{
if (count($validations) == 0) {
throw new EmptyValidationList();
}
$this->validations = $validations;
$this->mode = $mode;
}
/**
* {@inheritdoc}
*/
public function isValid($email, EmailLexer $emailLexer)
{
$result = true;
@@ -28,18 +64,45 @@ class MultipleValidationWithAnd implements EmailValidation
$emailLexer->reset();
$result = $result && $validation->isValid($email, $emailLexer);
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
$errors[] = $validation->getError();
$errors = $this->addNewError($validation->getError(), $errors);
if ($this->shouldStop($result)) {
break;
}
}
$this->error = new MultipleErrors($errors);
if (!empty($errors)) {
$this->error = new MultipleErrors($errors);
}
return $result;
}
private function addNewError($possibleError, array $errors)
{
if (null !== $possibleError) {
$errors[] = $possibleError;
}
return $errors;
}
private function shouldStop($result)
{
return !$result && $this->mode === self::STOP_ON_ERROR;
}
/**
* {@inheritdoc}
*/
public function getError()
{
return $this->error;
}
/**
* {@inheritdoc}
*/
public function getWarnings()
{
return $this->warnings;
@@ -3,11 +3,39 @@
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\InvalidEmail;
use Egulias\EmailValidator\Validation\Error\RFCWarnings;
class NoRFCWarningsValidation extends RFCValidation
{
/**
* @var InvalidEmail
*/
private $error;
/**
* {@inheritdoc}
*/
public function isValid($email, EmailLexer $emailLexer)
{
return parent::isValid($email, $emailLexer) && empty($this->getWarnings());
if (!parent::isValid($email, $emailLexer)) {
return false;
}
if (empty($this->getWarnings())) {
return true;
}
$this->error = new RFCWarnings();
return false;
}
/**
* {@inheritdoc}
*/
public function getError()
{
return $this->error ?: parent::getError();
}
}
@@ -3,6 +3,7 @@
namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\InvalidEmail;
use Egulias\EmailValidator\Validation\Error\SpoofEmail;
use \Spoofchecker;
+14 -7
View File
@@ -1,9 +1,13 @@
#EmailValidator
[![Build Status](https://travis-ci.org/egulias/EmailValidator.png?branch=master)](https://travis-ci.org/egulias/EmailValidator) [![Coverage Status](https://coveralls.io/repos/egulias/EmailValidator/badge.png?branch=master)](https://coveralls.io/r/egulias/EmailValidator?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/egulias/EmailValidator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6/small.png)](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6)
=============================
With the help of
With the help of [PHPStorm](https://www.jetbrains.com/phpstorm/)
##Requirements##
* [Composer](https://getcomposer.org) is required for installation
* [Spoofchecking](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php) validation requires that your PHP system have the [PHP Internationalization Libraries](http://php.net/manual/en/book.intl.php) (also known as PHP Intl)
![Powered by PhpStorm](https://www.jetbrains.com/phpstorm/documentation/docs/logo_phpstorm.png)
##Installation##
Run the command below to install via Composer
@@ -32,10 +36,10 @@ $validator->isValid("example@example.com", new RFCValidation()) //true
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)
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)
`MultipleValidationsWithAnd`
`MultipleValidationWithAnd`
It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
@@ -43,13 +47,16 @@ It is a validation that operates over other validations performing a logical and
<?php
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
use Egulias\EmailValidator\Validation\RFCValidation;
$validator = new EmailValidator();
$multipleValidations = new MultipleValidationsWithAnd([
$multipleValidations = new MultipleValidationWithAnd([
new RFCValidation(),
new DNSCheckValidation()
])
$validator->isValid("example@example.com", $multipleValidations) //true
]);
$validator->isValid("example@example.com", $multipleValidations); //true
```
###How to extend###
@@ -9,10 +9,30 @@ use Egulias\EmailValidator\Warning\NoDNSMXRecord;
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();
$this->assertTrue($validation->isValid("example@example.com", new EmailLexer()));
$this->assertTrue($validation->isValid($validEmail, new EmailLexer()));
}
public function testInvalidDNS()
@@ -25,13 +25,26 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Egulias\EmailValidator\Validation\Exception\EmptyValidationList
* @expectedException \Egulias\EmailValidator\Validation\Exception\EmptyValidationList
*/
public function testEmptyListIsNotAllowed()
{
new MultipleValidationWithAnd([]);
}
public function testValidationIsValid()
{
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
$validation = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
$validation->expects($this->any())->method("isValid")->willReturn(true);
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
$multipleValidation = new MultipleValidationWithAnd([$validation]);
$this->assertTrue($multipleValidation->isValid("example@example.com", $lexer));
$this->assertNull($multipleValidation->getError());
}
public function testAccumulatesWarnings()
{
$warnings1 = [
@@ -79,4 +92,27 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
$multipleValidation->isValid("example@example.com", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError());
}
public function testBreakOutOfLoopWhenError()
{
$error = new CommaInDomain();
$expectedResult = new MultipleErrors([$error]);
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
$validation1->expects($this->any())->method("isValid")->willReturn(false);
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
$validation1->expects($this->once())->method("getError")->willReturn($error);
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
$validation2->expects($this->never())->method("isValid");
$validation2->expects($this->never())->method("getWarnings");
$validation2->expects($this->never())->method("getError");
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR);
$multipleValidation->isValid("example@example.com", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError());
}
}
@@ -0,0 +1,35 @@
<?php
namespace Egulias\Tests\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\NoDomainPart;
use Egulias\EmailValidator\Validation\Error\RFCWarnings;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
class NoRFCWarningsValidationTest extends \PHPUnit_Framework_TestCase
{
public function testInvalidEmailIsInvalid()
{
$validation = new NoRFCWarningsValidation();
$this->assertFalse($validation->isValid('non-email-string', new EmailLexer()));
$this->assertInstanceOf(NoDomainPart::class, $validation->getError());
}
public function testEmailWithWarningsIsInvalid()
{
$validation = new NoRFCWarningsValidation();
$this->assertFalse($validation->isValid(str_repeat('x', 254).'@example.com', new EmailLexer())); // too long email
$this->assertInstanceOf(RFCWarnings::class, $validation->getError());
}
public function testEmailWithoutWarningsIsValid()
{
$validation = new NoRFCWarningsValidation();
$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
$this->assertNull($validation->getError());
}
}
@@ -1,23 +0,0 @@
<?php
namespace Egulias\Tests\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
class NoWarningsRFCValidationTest extends \PHPUnit_Framework_TestCase
{
public function testEmailWithWarningsIsInvalid()
{
$validation = new NoRFCWarningsValidation();
$this->assertFalse($validation->isValid('examp"l"e@example.com', new EmailLexer()));
}
public function testEmailWithoutWarningsIsValid()
{
$validation = new NoRFCWarningsValidation();
$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
}
}
+3
View File
@@ -28,6 +28,9 @@
"phpunit/phpunit": "^4.8.0",
"dominicsayers/isemail": "dev-master"
},
"suggest": {
"ext/php-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation"
},
"autoload": {
"psr-4": {
"Egulias\\EmailValidator\\": "EmailValidator"