Custom exception for dot at start

This commit is contained in:
Eduardo Gulias Davis
2015-06-25 20:11:07 +02:00
parent af864423f5
commit f4b154c169
6 changed files with 35 additions and 4 deletions
@@ -2,6 +2,8 @@
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\InvalidEmail;
/**
* EmailValidator
*
@@ -78,6 +80,9 @@ class EmailValidator
try {
$this->parser->parse((string)$email);
$this->warnings = $this->parser->getWarnings();
} catch (InvalidEmail $invalid) {
$this->error = $invalid->getCode();
return false;
} catch (\Exception $e) {
$rClass = new \ReflectionClass($this);
$this->error = $rClass->getConstant($e->getMessage());
@@ -0,0 +1,15 @@
<?php
namespace Egulias\EmailValidator\Exception;
use Egulias\EmailValidator\InvalidEmail;
class DotAtStart extends InvalidEmail
{
const CODE = 141;
public function __construct($part)
{
parent::__construct("Found DOT at start in " . $part, self::CODE);
}
}
@@ -0,0 +1,8 @@
<?php
namespace Egulias\EmailValidator;
class InvalidEmail extends \InvalidArgumentException
{
}
@@ -4,6 +4,7 @@
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\DotAtStart;
use Egulias\EmailValidator\Parser\Parser;
use Egulias\EmailValidator\EmailValidator;
@@ -17,7 +18,7 @@ class DomainPart extends Parser
$this->lexer->moveNext();
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
throw new \InvalidArgumentException('ERR_DOT_START');
throw new DotAtStart('domain');
}
if ($this->lexer->token['type'] === EmailLexer::S_EMPTY) {
@@ -2,6 +2,7 @@
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\Exception\DotAtStart;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\EmailValidator;
use \InvalidArgumentException;
@@ -16,7 +17,7 @@ class LocalPart extends Parser
while ($this->lexer->token['type'] !== EmailLexer::S_AT && $this->lexer->token) {
if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) {
throw new \InvalidArgumentException('ERR_DOT_START');
throw new DotAtStart('local');
}
$closingQuote = $this->checkDQUOTE($closingQuote);
@@ -3,6 +3,7 @@
namespace Egulias\Tests\EmailValidator;
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Exception\DotAtStart;
class EmailValidatorTest extends \PHPUnit_Framework_TestCase
{
@@ -147,8 +148,8 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
array(EmailValidator::ERR_CONSECUTIVEDOTS, 'example..example@example.co.uk'),
array(EmailValidator::ERR_CONSECUTIVEDOTS, 'example@example..co.uk'),
array(EmailValidator::ERR_EXPECTING_ATEXT, '<fabien_potencier>@example.fr'),
array(EmailValidator::ERR_DOT_START, '.example@localhost'),
array(EmailValidator::ERR_DOT_START, 'example@.localhost'),
array(DotAtStart::CODE, '.example@localhost'),
array(DotAtStart::CODE, 'example@.localhost'),
array(EmailValidator::ERR_DOT_END, 'example@localhost.'),
array(EmailValidator::ERR_DOT_END, 'example.@example.co.uk'),
array(EmailValidator::ERR_UNCLOSEDCOMMENT, '(example@localhost'),