mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-08 17:16:28 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 19811e0082 |
@@ -3,23 +3,60 @@
|
|||||||
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\Exception\EmptyValidationList;
|
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
|
||||||
|
|
||||||
class MultipleValidationWithAnd implements EmailValidation
|
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 = [];
|
private $validations = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private $warnings = [];
|
private $warnings = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var MultipleErrors
|
||||||
|
*/
|
||||||
private $error;
|
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) {
|
if (count($validations) == 0) {
|
||||||
throw new EmptyValidationList();
|
throw new EmptyValidationList();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->validations = $validations;
|
$this->validations = $validations;
|
||||||
|
$this->mode = $mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function isValid($email, EmailLexer $emailLexer)
|
public function isValid($email, EmailLexer $emailLexer)
|
||||||
{
|
{
|
||||||
$result = true;
|
$result = true;
|
||||||
@@ -29,17 +66,32 @@ class MultipleValidationWithAnd implements EmailValidation
|
|||||||
$result = $result && $validation->isValid($email, $emailLexer);
|
$result = $result && $validation->isValid($email, $emailLexer);
|
||||||
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
|
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
|
||||||
$errors[] = $validation->getError();
|
$errors[] = $validation->getError();
|
||||||
|
|
||||||
|
if ($this->shouldStop($result)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$this->error = new MultipleErrors($errors);
|
$this->error = new MultipleErrors($errors);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function shouldStop($result)
|
||||||
|
{
|
||||||
|
return !$result && $this->mode === self::STOP_ON_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function getError()
|
public function getError()
|
||||||
{
|
{
|
||||||
return $this->error;
|
return $this->error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function getWarnings()
|
public function getWarnings()
|
||||||
{
|
{
|
||||||
return $this->warnings;
|
return $this->warnings;
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Egulias\EmailValidator\Validation\Exception\EmptyValidationList
|
* @expectedException \Egulias\EmailValidator\Validation\Exception\EmptyValidationList
|
||||||
*/
|
*/
|
||||||
public function testEmptyListIsNotAllowed()
|
public function testEmptyListIsNotAllowed()
|
||||||
{
|
{
|
||||||
@@ -79,4 +79,27 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
$multipleValidation->isValid("example@example.com", $lexer);
|
$multipleValidation->isValid("example@example.com", $lexer);
|
||||||
$this->assertEquals($expectedResult, $multipleValidation->getError());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user