Merge pull request #59 from egulias/next-min-ver

Improve UTF8 detection, domain part validation, others
This commit is contained in:
Eduardo Gulias Davis
2015-04-26 17:49:00 +02:00
4 changed files with 36 additions and 37 deletions
+2 -21
View File
@@ -75,8 +75,6 @@ class EmailLexer extends AbstractLexer
'\0' => self::C_NUL,
);
protected $invalidASCII = array(226 => 1,);
protected $hasInvalidTokens = false;
protected $previous;
@@ -138,7 +136,8 @@ class EmailLexer extends AbstractLexer
protected function getCatchablePatterns()
{
return array(
'[a-zA-Z_]+[46]?',
'[a-zA-Z_]+[46]?', //ASCII and domain literal
'[^\x00-\x7F]', //UTF-8
'[0-9]+',
'\r\n',
'::',
@@ -179,11 +178,6 @@ class EmailLexer extends AbstractLexer
return self::INVALID;
}
if ($this->isASCIIInvalid($value)) {
$this->hasInvalidTokens = true;
return self::INVALID;
}
return self::GENERIC;
}
@@ -209,19 +203,6 @@ class EmailLexer extends AbstractLexer
return false;
}
/**
* @param $value
* @return bool
*/
protected function isASCIIInvalid($value)
{
if (isset($this->invalidASCII[ord($value)])) {
return true;
}
return false;
}
/**
* @param $value
* @return bool
@@ -104,9 +104,6 @@ class DomainPart extends Parser
{
$domain = '';
do {
if ($this->lexer->token['type'] === EmailLexer::S_SEMICOLON) {
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
}
$prev = $this->lexer->getPrevious();
@@ -221,9 +218,6 @@ class DomainPart extends Parser
return $addressLiteral;
}
/**
* @param string $addressLiteral
*/
protected function checkIPV4Tag($addressLiteral)
{
$matchesIP = array();
@@ -249,6 +243,17 @@ class DomainPart extends Parser
protected function checkDomainPartExceptions($prev)
{
$invalidDomainTokens = array(
EmailLexer::S_DQUOTE => true,
EmailLexer::S_SEMICOLON => true,
EmailLexer::S_GREATERTHAN => true,
EmailLexer::S_LOWERTHAN => true,
);
if (isset($invalidDomainTokens[$this->lexer->token['type']])) {
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
}
if ($this->lexer->token['type'] === EmailLexer::S_COMMA) {
throw new \InvalidArgumentException('ERR_COMMA_IN_DOMAIN');
}
@@ -103,6 +103,17 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
$this->assertEquals(EmailLexer::S_HTAB, $lexer->token['type']);
}
public function testLexerForUTF8()
{
$lexer = new EmailLexer();
$lexer->setInput("áÇ@bar.com");
$lexer->moveNext();
$lexer->moveNext();
$this->assertEquals(EmailLexer::GENERIC, $lexer->token['type']);
$lexer->moveNext();
$this->assertEquals(EmailLexer::GENERIC, $lexer->token['type']);
}
public function testLexerSearchToken()
{
$lexer = new EmailLexer();
@@ -111,15 +122,6 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
$this->assertTrue($lexer->find(EmailLexer::S_HTAB));
}
public function testLexerHasInvalidTokens()
{
$lexer = new EmailLexer();
$lexer->setInput(chr(226));
$lexer->moveNext();
$lexer->moveNext();
$this->assertTrue($lexer->hasInvalidTokens());
}
public function getTokens()
{
return array(
@@ -152,7 +154,7 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
array('}', EmailLexer::S_CLOSEQBRACKET),
array('', EmailLexer::S_EMPTY),
array(chr(31), EmailLexer::INVALID),
array(chr(226), EmailLexer::INVALID),
array(chr(226), EmailLexer::GENERIC),
array(chr(0), EmailLexer::C_NUL)
);
}
@@ -37,6 +37,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
public function getValidEmails()
{
return array(
array('â@iana.org'),
array('fabien@symfony.com'),
array('example@example.co.uk'),
array('fabien_potencier@example.fr'),
@@ -56,6 +57,12 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
array('""@iana.org'),
array('"\""@iana.org'),
array('müller@möller.de'),
array('test@email*'),
array('test@email!'),
array('test@email&'),
array('test@email^'),
array('test@email%'),
array('test@email$'),
);
}
@@ -112,6 +119,10 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
array('test@foo;bar.com'),
array('test;123@foobar.com'),
array('test@example..com'),
array('email.email@email."'),
array('test@email>'),
array('test@email<'),
array('test@email{'),
);
}