Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Spoof validation
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Validation\Error; | ||
|
||
use Egulias\EmailValidator\Exception\InvalidEmail; | ||
|
||
class SpoofEmail extends InvalidEmail | ||
{ | ||
const CODE = 998; | ||
const REASON = "The email contains mixed UTF8 chars that makes it suspicious"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Validation; | ||
|
||
use Egulias\EmailValidator\EmailLexer; | ||
use Egulias\EmailValidator\Validation\Error\SpoofEmail; | ||
use \Spoofchecker; | ||
|
||
class SpoofCheckValidation implements EmailValidation | ||
{ | ||
/** | ||
* @var InvalidEmail | ||
*/ | ||
private $error; | ||
|
||
public function isValid($email, EmailLexer $emailLexer) | ||
{ | ||
$checker = new Spoofchecker(); | ||
$checker->setChecks(Spoofchecker::SINGLE_SCRIPT); | ||
|
||
if ($checker->isSuspicious($email)) { | ||
$this->error = new SpoofEmail(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
|
||
public function getWarnings() | ||
{ | ||
return []; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Tests/EmailValidator/Validation/SpoofCheckValidationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Egulias\Tests\EmailValidator\Validation; | ||
|
||
use Egulias\EmailValidator\EmailLexer; | ||
use Egulias\EmailValidator\Validation\SpoofCheckValidation; | ||
|
||
class SpoofCheckValidationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider validUTF8EmailsProvider | ||
*/ | ||
public function testUTF8EmailAreValid($email) | ||
{ | ||
$validation = new SpoofCheckValidation(); | ||
|
||
$this->assertTrue($validation->isValid($email, new EmailLexer())); | ||
} | ||
|
||
public function testEmailWithSpoofsIsInvalid() | ||
{ | ||
$validation = new SpoofCheckValidation(); | ||
|
||
$this->assertFalse($validation->isValid("Кириллица"."latin漢字"."ひらがな"."カタカナ", new EmailLexer())); | ||
} | ||
|
||
public function validUTF8EmailsProvider() | ||
{ | ||
return [ | ||
// Cyrillic | ||
['Кириллица@Кириллица'], | ||
// Latin + Han + Hiragana + Katakana | ||
["latin漢字"."ひらがな"."カタカナ"."@example.com"], | ||
// Latin + Han + Hangul | ||
["latin"."漢字"."조선말"."@example.com"], | ||
// Latin + Han + Bopomofo | ||
["latin"."漢字"."ㄅㄆㄇㄈ"."@example.com"] | ||
]; | ||
} | ||
} |