mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-07 00:32:19 +00:00
Port @vajexal #278
This commit is contained in:
+42
-18
@@ -27,12 +27,16 @@ class DomainPart extends Parser
|
||||
const DOMAIN_MAX_LENGTH = 253;
|
||||
const LABEL_MAX_LENGTH = 63;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $domainPart = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $label = '';
|
||||
|
||||
public function parse() : Result
|
||||
{
|
||||
$this->lexer->moveNext();
|
||||
@@ -174,18 +178,21 @@ class DomainPart extends Parser
|
||||
$literalResult = $this->parseDomainLiteral();
|
||||
|
||||
$this->addTLDWarnings($tldMissing);
|
||||
//Invalid literal parsing
|
||||
//if($literalResult->isInvalid()) {
|
||||
// return $literalResult;
|
||||
//}
|
||||
return $literalResult;
|
||||
}
|
||||
|
||||
$labelCheck = $this->checkLabelLength($prev);
|
||||
if ($labelCheck->isInvalid()) {
|
||||
return $labelCheck;
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
|
||||
$labelCheck = $this->checkLabelLength($this->label);
|
||||
$this->label = '';
|
||||
if ($labelCheck->isInvalid()) {
|
||||
return $labelCheck;
|
||||
}
|
||||
} else {
|
||||
$this->label .= $this->lexer->token['value'];
|
||||
}
|
||||
|
||||
//$labelCheck = $this->checkLabelLength($prev);
|
||||
|
||||
$FwsResult = $this->parseFWS();
|
||||
if($FwsResult->isInvalid()) {
|
||||
return $FwsResult;
|
||||
@@ -202,12 +209,14 @@ class DomainPart extends Parser
|
||||
return $exceptionsResult;
|
||||
}
|
||||
$this->lexer->moveNext();
|
||||
//if ($this->lexer->token['type'] === EmailLexer::S_SP) {
|
||||
// return new InvalidEmail(new CharNotAllowed(), $this->lexer->token['value']);
|
||||
//}
|
||||
|
||||
} while (null !== $this->lexer->token['type']);
|
||||
|
||||
$labelCheck = $this->checkLabelLength($this->label);
|
||||
$this->label = '';
|
||||
if ($labelCheck->isInvalid()) {
|
||||
return $labelCheck;
|
||||
}
|
||||
$this->addTLDWarnings($tldMissing);
|
||||
|
||||
$this->domainPart = $domain;
|
||||
@@ -277,18 +286,33 @@ class DomainPart extends Parser
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
protected function checkLabelLength(array $prev) : Result
|
||||
|
||||
private function checkLabelLength(string $label) : Result
|
||||
{
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
|
||||
$prev['type'] === EmailLexer::GENERIC &&
|
||||
strlen($prev['value']) > self::LABEL_MAX_LENGTH
|
||||
) {
|
||||
//$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
|
||||
return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
|
||||
if ($this->isLabelTooLong($label)) {
|
||||
return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
|
||||
}
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
|
||||
private function isLabelTooLong(string $label) : bool
|
||||
{
|
||||
if (preg_match('/[^\x00-\x7F]/', $label)) {
|
||||
idn_to_ascii(utf8_decode($label), IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
|
||||
return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
|
||||
}
|
||||
return strlen($label) > self::LABEL_MAX_LENGTH;
|
||||
}
|
||||
//if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
|
||||
// $prev['type'] === EmailLexer::GENERIC &&
|
||||
// strlen($prev['value']) > self::LABEL_MAX_LENGTH
|
||||
//) {
|
||||
// return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
|
||||
//}
|
||||
//return new ValidEmail();
|
||||
//}
|
||||
|
||||
private function addTLDWarnings(bool $isTLDMissing) : void
|
||||
{
|
||||
if ($isTLDMissing) {
|
||||
|
||||
@@ -26,11 +26,23 @@ class NoRFCWarningsValidationTest extends TestCase
|
||||
$this->assertInstanceOf(RFCWarnings::class, $validation->getError()->reason());
|
||||
}
|
||||
|
||||
public function testEmailWithoutWarningsIsValid()
|
||||
/**
|
||||
* @dataProvider getValidEmailsWithoutWarnings
|
||||
*/
|
||||
public function testEmailWithoutWarningsIsValid($email)
|
||||
{
|
||||
$validation = new NoRFCWarningsValidation();
|
||||
|
||||
$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
|
||||
$this->assertTrue($validation->isValid($email, new EmailLexer()));
|
||||
$this->assertNull($validation->getError());
|
||||
}
|
||||
|
||||
public function getValidEmailsWithoutWarnings()
|
||||
{
|
||||
return [
|
||||
['example@example.com',],
|
||||
[sprintf('example@%s.com', str_repeat('ъ', 40)),],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,23 +3,25 @@
|
||||
namespace Egulias\EmailValidator\Tests\EmailValidator\Validation;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Warning\TLD;
|
||||
use Egulias\EmailValidator\Warning\Comment;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Warning\IPV6BadChar;
|
||||
use Egulias\EmailValidator\Result\Reason\CRNoLF;
|
||||
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
|
||||
use Egulias\EmailValidator\Warning\DomainLiteral;
|
||||
use Egulias\EmailValidator\Warning\IPV6MaxGroups;
|
||||
use Egulias\EmailValidator\Warning\ObsoleteDTEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
|
||||
use Egulias\EmailValidator\Warning\AddressLiteral;
|
||||
use Egulias\EmailValidator\Warning\IPV6ColonStart;
|
||||
use Egulias\EmailValidator\Warning\IPV6Deprecated;
|
||||
use Egulias\EmailValidator\Warning\IPV6GroupCount;
|
||||
use Egulias\EmailValidator\Warning\IPV6DoubleColon;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtStart;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
|
||||
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||
use Egulias\EmailValidator\Result\Reason\LabelTooLong;
|
||||
use Egulias\EmailValidator\Result\Reason\NoDomainPart;
|
||||
use Egulias\EmailValidator\Result\Reason\ConsecutiveAt;
|
||||
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
|
||||
@@ -27,7 +29,6 @@ use Egulias\EmailValidator\Result\Reason\DomainHyphened;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingDTEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
|
||||
use Egulias\EmailValidator\Result\Reason\CRNoLF;
|
||||
|
||||
|
||||
class RFCValidationDomainPartTest extends TestCase
|
||||
@@ -178,6 +179,9 @@ class RFCValidationDomainPartTest extends TestCase
|
||||
[new InvalidEmail(new CRNoLF(), "["), "example@[\r]"],
|
||||
[new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ,'), ','), 'example@exam,ple.com'],
|
||||
[new InvalidEmail(new ExpectingATEXT("Invalid token in domain: '"), "'"), "test@example.com'"],
|
||||
[new InvalidEmail(new LabelTooLong(), "."), sprintf('example@%s.com', str_repeat('ъ', 64))],
|
||||
[new InvalidEmail(new LabelTooLong(), "."), sprintf('example@%s.com', str_repeat('a4t', 22))],
|
||||
[new InvalidEmail(new LabelTooLong(), ""), sprintf('example@%s', str_repeat('a4t', 22))],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ class RFCValidationTest extends TestCase
|
||||
['"\""@iana.org'],
|
||||
['müller@möller.de'],
|
||||
["1500111@профи-инвест.рф"],
|
||||
[sprintf('example@%s.com', str_repeat('ъ', 40))],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user