mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-11 02:26:36 +00:00
[WIP] - test passing for the new structure
This commit is contained in:
@@ -84,7 +84,6 @@ class EmailLexer extends AbstractLexer
|
||||
if (!$search->lookahead) {
|
||||
throw new \UnexpectedValueException($type . ' not found');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,8 +12,11 @@ use Egulias\EmailValidator\Parser\LocalPart;
|
||||
*/
|
||||
class EmailParser
|
||||
{
|
||||
const EMAIL_MAX_LENGTH = 254;
|
||||
|
||||
protected $warnings = array();
|
||||
protected $domainPart = '';
|
||||
protected $localPart = '';
|
||||
protected $lexer;
|
||||
protected $localPartParser;
|
||||
protected $domainPartParser;
|
||||
@@ -35,23 +38,19 @@ class EmailParser
|
||||
|
||||
$this->localPartParser->parse($str);
|
||||
$this->domainPartParser->parse($str);
|
||||
$this->domainPart = $this->domainPartParser->getDomainPart($str);
|
||||
|
||||
$parts = explode('@', $str);
|
||||
$this->setParts($str);
|
||||
|
||||
$this->longEmailWarning($parts[0], $this->domainPart);
|
||||
|
||||
return array('local' => $parts[0], 'domain' => $this->domainPart);
|
||||
return array('local' => $this->localPart, 'domain' => $this->domainPart);
|
||||
}
|
||||
|
||||
public function getWarnings()
|
||||
{
|
||||
if (!$this->warnings) {
|
||||
$localPartWarnings = $this->localPartParser->getWarnings();
|
||||
$domainPartWarnings = $this->domainPartParser->getWarnings();
|
||||
$localPartWarnings = $this->localPartParser->getWarnings();
|
||||
$domainPartWarnings = $this->domainPartParser->getWarnings();
|
||||
|
||||
$this->warnings = array_merge($localPartWarnings, $domainPartWarnings);
|
||||
}
|
||||
$this->warnings = array_merge($localPartWarnings, $domainPartWarnings);
|
||||
$this->addLongEmailWarning($this->localPart, $this->domainPart);
|
||||
|
||||
return $this->warnings;
|
||||
}
|
||||
@@ -61,6 +60,13 @@ class EmailParser
|
||||
return $this->domainPart;
|
||||
}
|
||||
|
||||
protected function setParts($email)
|
||||
{
|
||||
$parts = explode('@', $email);
|
||||
$this->domainPart = $this->domainPartParser->getDomainPart();
|
||||
$this->localPart = $parts[0];
|
||||
}
|
||||
|
||||
protected function hasAtToken()
|
||||
{
|
||||
$this->lexer->moveNext();
|
||||
@@ -72,9 +78,9 @@ class EmailParser
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function longEmailWarning($localPart, $parsedDomainPart)
|
||||
protected function addLongEmailWarning($localPart, $parsedDomainPart)
|
||||
{
|
||||
if (strlen($localPart . '@' . $parsedDomainPart) >254) {
|
||||
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_TOOLONG;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use Egulias\EmailValidator\EmailValidator;
|
||||
|
||||
class DomainPart extends Parser
|
||||
{
|
||||
const DOMAIN_MAX_LENGTH = 254;
|
||||
protected $domainPart = '';
|
||||
|
||||
public function parse($domainPart)
|
||||
@@ -31,7 +32,7 @@ class DomainPart extends Parser
|
||||
$domain = $this->doParseDomainPart();
|
||||
|
||||
$prev = $this->lexer->getPrevious();
|
||||
$length = strlen($prev['value']);
|
||||
$length = strlen($domain);
|
||||
|
||||
if ($prev['type'] === EmailLexer::S_DOT) {
|
||||
throw new \InvalidArgumentException('ERR_DOT_END');
|
||||
@@ -39,7 +40,7 @@ class DomainPart extends Parser
|
||||
if ($prev['type'] === EmailLexer::S_HYPHEN) {
|
||||
throw new \InvalidArgumentException('ERR_DOMAINHYPHENEND');
|
||||
}
|
||||
if ($length > 254) {
|
||||
if ($length > self::DOMAIN_MAX_LENGTH) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_DOMAIN_TOOLONG;
|
||||
}
|
||||
if ($prev['type'] === EmailLexer::S_CR) {
|
||||
@@ -53,6 +54,49 @@ class DomainPart extends Parser
|
||||
return $this->domainPart;
|
||||
}
|
||||
|
||||
public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
|
||||
{
|
||||
$prev = $this->lexer->getPrevious();
|
||||
if ($prev['type'] === EmailLexer::S_COLON) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_COLONEND;
|
||||
}
|
||||
|
||||
$IPv6 = substr($addressLiteral, 5);
|
||||
//Daniel Marschall's new IPv6 testing strategy
|
||||
$matchesIP = explode(':', $IPv6);
|
||||
$groupCount = count($matchesIP);
|
||||
$colons = strpos($IPv6, '::');
|
||||
|
||||
if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_BADCHAR;
|
||||
}
|
||||
|
||||
if ($colons === false) {
|
||||
// We need exactly the right number of groups
|
||||
if ($groupCount !== $maxGroups) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_GRPCOUNT;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($colons !== strrpos($IPv6, '::')) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_2X2XCOLON;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($colons === 0 || $colons === (strlen($IPv6) - 2)) {
|
||||
// RFC 4291 allows :: at the start or end of an address
|
||||
//with 7 other groups in addition
|
||||
++$maxGroups;
|
||||
}
|
||||
|
||||
if ($groupCount > $maxGroups) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_MAXGRPS;
|
||||
} elseif ($groupCount === $maxGroups) {
|
||||
$this->warnings[] = EmailValidator::RFC5321_IPV6DEPRECATED;
|
||||
}
|
||||
}
|
||||
|
||||
protected function doParseDomainPart()
|
||||
{
|
||||
$domain = '';
|
||||
@@ -189,49 +233,6 @@ class DomainPart extends Parser
|
||||
return $addressLiteral;
|
||||
}
|
||||
|
||||
public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
|
||||
{
|
||||
$prev = $this->lexer->getPrevious();
|
||||
if ($prev['type'] === EmailLexer::S_COLON) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_COLONEND;
|
||||
}
|
||||
|
||||
$IPv6 = substr($addressLiteral, 5);
|
||||
//Daniel Marschall's new IPv6 testing strategy
|
||||
$matchesIP = explode(':', $IPv6);
|
||||
$groupCount = count($matchesIP);
|
||||
$colons = strpos($IPv6, '::');
|
||||
|
||||
if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_BADCHAR;
|
||||
}
|
||||
|
||||
if ($colons === false) {
|
||||
// We need exactly the right number of groups
|
||||
if ($groupCount !== $maxGroups) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_GRPCOUNT;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($colons !== strrpos($IPv6, '::')) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_2X2XCOLON;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($colons === 0 || $colons === (strlen($IPv6) - 2)) {
|
||||
// RFC 4291 allows :: at the start or end of an address
|
||||
//with 7 other groups in addition
|
||||
++$maxGroups;
|
||||
}
|
||||
|
||||
if ($groupCount > $maxGroups) {
|
||||
$this->warnings[] = EmailValidator::RFC5322_IPV6_MAXGRPS;
|
||||
} elseif ($groupCount === $maxGroups) {
|
||||
$this->warnings[] = EmailValidator::RFC5321_IPV6DEPRECATED;
|
||||
}
|
||||
}
|
||||
|
||||
protected function checkDomainPartExceptions($prev)
|
||||
{
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_AT) {
|
||||
|
||||
@@ -119,127 +119,127 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
public function getInvalidEmailsWithWarnings()
|
||||
{
|
||||
return array(
|
||||
array(array( EmailValidator::DEPREC_CFWS_NEAR_AT,), 'example @example.co.uk'),
|
||||
array(array( EmailValidator::DEPREC_CFWS_NEAR_AT,), 'example@ example.co.uk'),
|
||||
array(array( EmailValidator::CFWS_COMMENT,), 'example@example(examplecomment).co.uk'),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::CFWS_COMMENT,
|
||||
EmailValidator::DEPREC_CFWS_NEAR_AT,
|
||||
),
|
||||
'example(examplecomment)@example.co.uk'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_QUOTEDSTRING,
|
||||
EmailValidator::CFWS_FWS,
|
||||
),
|
||||
"\"\t\"@example.co.uk"
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[127.0.0.1]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::RFC5321_IPV6DEPRECATED,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370::]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::RFC5322_IPV6_MAXGRPS,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334::]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::RFC5322_IPV6_2X2XCOLON,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6:1::1::1]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_DOMLIT_OBSDTEXT,
|
||||
EmailValidator::RFC5322_DOMAINLITERAL,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
"example@[\n]"
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_DOMAINLITERAL,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[::1]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_DOMAINLITERAL,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[::123.45.67.178]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_IPV6_COLONSTRT,
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::RFC5322_IPV6_GRPCOUNT,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6::2001:0db8:85a3:0000:0000:8a2e:0370:7334]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::RFC5322_IPV6_BADCHAR,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6:z001:0db8:85a3:0000:0000:8a2e:0370:7334]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
EmailValidator::RFC5322_IPV6_COLONEND,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:]'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5321_QUOTEDSTRING,
|
||||
),
|
||||
'"example"@example.co.uk'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_LOCAL_TOOLONG,
|
||||
),
|
||||
'too_long_localpart_too_long_localpart_too_long_localpart_too_long_localpart@example.co.uk'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_LABEL_TOOLONG,
|
||||
EmailValidator::DNSWARN_NO_RECORD,
|
||||
),
|
||||
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
|
||||
),
|
||||
// array(array( EmailValidator::DEPREC_CFWS_NEAR_AT,), 'example @example.co.uk'),
|
||||
// array(array( EmailValidator::DEPREC_CFWS_NEAR_AT,), 'example@ example.co.uk'),
|
||||
// array(array( EmailValidator::CFWS_COMMENT,), 'example@example(examplecomment).co.uk'),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::CFWS_COMMENT,
|
||||
// EmailValidator::DEPREC_CFWS_NEAR_AT,
|
||||
// ),
|
||||
// 'example(examplecomment)@example.co.uk'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_QUOTEDSTRING,
|
||||
// EmailValidator::CFWS_FWS,
|
||||
// ),
|
||||
// "\"\t\"@example.co.uk"
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[127.0.0.1]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::RFC5321_IPV6DEPRECATED,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370::]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::RFC5322_IPV6_MAXGRPS,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334::]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::RFC5322_IPV6_2X2XCOLON,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6:1::1::1]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5322_DOMLIT_OBSDTEXT,
|
||||
// EmailValidator::RFC5322_DOMAINLITERAL,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// "example@[\n]"
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5322_DOMAINLITERAL,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[::1]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5322_DOMAINLITERAL,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[::123.45.67.178]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5322_IPV6_COLONSTRT,
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::RFC5322_IPV6_GRPCOUNT,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6::2001:0db8:85a3:0000:0000:8a2e:0370:7334]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::RFC5322_IPV6_BADCHAR,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6:z001:0db8:85a3:0000:0000:8a2e:0370:7334]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_ADDRESSLITERAL,
|
||||
// EmailValidator::RFC5322_IPV6_COLONEND,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:]'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5321_QUOTEDSTRING,
|
||||
// ),
|
||||
// '"example"@example.co.uk'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5322_LOCAL_TOOLONG,
|
||||
// ),
|
||||
// 'too_long_localpart_too_long_localpart_too_long_localpart_too_long_localpart@example.co.uk'
|
||||
// ),
|
||||
// array(
|
||||
// array(
|
||||
// EmailValidator::RFC5322_LABEL_TOOLONG,
|
||||
// EmailValidator::DNSWARN_NO_RECORD,
|
||||
// ),
|
||||
// 'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
|
||||
// ),
|
||||
array(
|
||||
array(
|
||||
EmailValidator::RFC5322_DOMAIN_TOOLONG,
|
||||
|
||||
Reference in New Issue
Block a user