mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-30 12:07:59 +00:00
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
This commit is contained in:
committed by
Eduardo Gulias Davis
parent
19811e0082
commit
4f0f9023f0
@@ -4,18 +4,31 @@ 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();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Egulias\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
|
||||
|
||||
class MultipleValidationWithAnd implements EmailValidation
|
||||
@@ -65,17 +64,29 @@ 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;
|
||||
|
||||
@@ -32,6 +32,19 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
||||
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 = [
|
||||
|
||||
Reference in New Issue
Block a user