mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-26 18:28:44 +00:00
All tests green
This commit is contained in:
@@ -26,6 +26,7 @@ class DomainLiteral extends Parser
|
||||
|
||||
$IPv6TAG = false;
|
||||
$addressLiteral = '';
|
||||
|
||||
do {
|
||||
if ($this->lexer->token['type'] === EmailLexer::C_NUL) {
|
||||
return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->token['value']);
|
||||
@@ -69,27 +70,24 @@ class DomainLiteral extends Parser
|
||||
|
||||
//Encapsulate
|
||||
$addressLiteral = str_replace('[', '', $addressLiteral);
|
||||
$addressLiteralIPv4 = $this->checkIPV4Tag($addressLiteral);
|
||||
$isAddressLiteralIPv4 = $this->checkIPV4Tag($addressLiteral);
|
||||
|
||||
if ($addressLiteralIPv4 === $addressLiteral) {
|
||||
//return $addressLiteral;
|
||||
if (!$isAddressLiteralIPv4) {
|
||||
return new ValidEmail();
|
||||
} else {
|
||||
$addressLiteral = $this->convertIPv4ToIPv6($addressLiteral);
|
||||
}
|
||||
|
||||
if (!$IPv6TAG) {
|
||||
$this->warnings[WarningDomainLiteral::CODE] = new WarningDomainLiteral();
|
||||
return new ValidEmail();
|
||||
//return $addressLiteral;
|
||||
}
|
||||
|
||||
$this->warnings[AddressLiteral::CODE] = new AddressLiteral();
|
||||
|
||||
$this->checkIPV6Tag($addressLiteralIPv4);
|
||||
|
||||
//return $addressLiteralIPv4;
|
||||
$this->checkIPV6Tag($addressLiteral);
|
||||
|
||||
return new ValidEmail();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,13 +136,34 @@ class DomainLiteral extends Parser
|
||||
$this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated();
|
||||
}
|
||||
}
|
||||
|
||||
public function convertIPv4ToIPv6($addressLiteralIPv4) : string
|
||||
{
|
||||
$matchesIP = array();
|
||||
$IPv4Match = preg_match(
|
||||
'/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/',
|
||||
$addressLiteralIPv4,
|
||||
$matchesIP);
|
||||
|
||||
// Extract IPv4 part from the end of the address-literal (if there is one)
|
||||
if ($IPv4Match > 0) {
|
||||
$index = strrpos($addressLiteralIPv4, $matchesIP[0]);
|
||||
//There's a match but it is at the start
|
||||
if ($index > 0) {
|
||||
// Convert IPv4 part to IPv6 format for further testing
|
||||
return substr($addressLiteralIPv4, 0, (int) $index) . '0:0';
|
||||
}
|
||||
}
|
||||
|
||||
return $addressLiteralIPv4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $addressLiteral
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function checkIPV4Tag($addressLiteral) : string
|
||||
protected function checkIPV4Tag($addressLiteral) : bool
|
||||
{
|
||||
$matchesIP = array();
|
||||
$IPv4Match = preg_match(
|
||||
@@ -159,13 +178,11 @@ class DomainLiteral extends Parser
|
||||
//There's a match but it is at the start
|
||||
if ($index === 0) {
|
||||
$this->warnings[AddressLiteral::CODE] = new AddressLiteral();
|
||||
return $addressLiteral;
|
||||
return false;
|
||||
}
|
||||
// Convert IPv4 part to IPv6 format for further testing
|
||||
$addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0';
|
||||
}
|
||||
|
||||
return $addressLiteral;
|
||||
return true;
|
||||
}
|
||||
|
||||
private function addObsoleteWarnings()
|
||||
|
||||
@@ -38,6 +38,7 @@ use Egulias\EmailValidator\Result\Reason\NoDomainPart as ReasonNoDomainPart;
|
||||
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot as ReasonConsecutiveDot;
|
||||
use Egulias\EmailValidator\Result\Reason\DomainHyphened as ReasonDomainHyphened;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingDTEXT as ReasonExpectingDTEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\UnclosedComment as ReasonUnclosedComment;
|
||||
|
||||
class RFCValidationTest extends TestCase
|
||||
@@ -215,10 +216,10 @@ class RFCValidationTest extends TestCase
|
||||
//This was the original. But atext is not allowed after \n
|
||||
//array(EmailValidator::ERR_EXPECTING_ATEXT, "exampl\ne@example.co.uk"),
|
||||
[new InvalidEmail(new AtextAfterCFWS(), "\n"), "exampl\ne@example.co.uk"],
|
||||
[new ExpectingDTEXT(), "example@[[]"],
|
||||
[new InvalidEmail(new ReasonExpectingDTEXT(), '['), "example@[[]"],
|
||||
[new InvalidEmail(new AtextAfterCFWS(), "\t"), "exampl\te@example.co.uk"],
|
||||
[new CRNoLF(), "example@exa\rmple.co.uk"],
|
||||
[new CRNoLF(), "example@[\r]"],
|
||||
[new InvalidEmail(new ReasonCRNoLF(), "\r"), "example@exa\rmple.co.uk"],
|
||||
[new InvalidEmail(new ReasonCRNoLF(), "["), "example@[\r]"],
|
||||
[new InvalidEmail(new ReasonCRNoLF(), "\r"), "exam\rple@example.co.uk"],
|
||||
];
|
||||
}
|
||||
@@ -231,8 +232,8 @@ class RFCValidationTest extends TestCase
|
||||
$this->assertTrue($this->validator->isValid($email, $this->lexer));
|
||||
$warnings = $this->validator->getWarnings();
|
||||
$this->assertCount(
|
||||
count($warnings), $expectedWarnings,
|
||||
"Expected: " . implode(",", $expectedWarnings) . " and got " . implode(",", $warnings)
|
||||
count($expectedWarnings), $warnings,
|
||||
"Expected: " . implode(",", $expectedWarnings) . " and got: " . PHP_EOL . implode(PHP_EOL, $warnings)
|
||||
);
|
||||
|
||||
foreach ($warnings as $warning) {
|
||||
|
||||
Reference in New Issue
Block a user