mirror of
https://github.com/elyby/yii2-tempmail-validator.git
synced 2024-11-08 13:42:29 +05:30
Switching to daveearley/daves-email-validation-tool
This commit is contained in:
parent
0487fa3ee0
commit
e5dc01ee1b
@ -16,8 +16,9 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
|
"php": ">=7",
|
||||||
"yiisoft/yii2": "*",
|
"yiisoft/yii2": "*",
|
||||||
"ely/php-tempmailbuster": "~1.0.2"
|
"daveearley/daves-email-validation-tool": "^0.1.5"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "~4.8 || ~5.0"
|
"phpunit/phpunit": "~4.8 || ~5.0"
|
||||||
|
@ -1,31 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ely\Yii2;
|
namespace Ely\Yii2;
|
||||||
|
|
||||||
use Ely\TempMailBuster\StorageInterface;
|
use EmailValidation\EmailAddress;
|
||||||
use Ely\TempMailBuster\Validator as TempmailBuster;
|
use EmailValidation\EmailDataProvider;
|
||||||
|
use EmailValidation\EmailValidator;
|
||||||
|
use EmailValidation\ValidationResults;
|
||||||
|
use EmailValidation\Validations\DisposableEmailValidator;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\validators\Validator;
|
use yii\validators\Validator;
|
||||||
|
|
||||||
class TempmailValidator extends Validator
|
class TempmailValidator extends Validator
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var string class name for used tempmail loader
|
|
||||||
*/
|
|
||||||
public $loader = '\Ely\TempMailBuster\Loader\AntiTempmailRepo';
|
|
||||||
/**
|
|
||||||
* @var string class name for used storage object
|
|
||||||
*/
|
|
||||||
public $storage = '\Ely\TempMailBuster\Storage';
|
|
||||||
/**
|
|
||||||
* @var bool switcher for white/blacklist validation
|
|
||||||
*/
|
|
||||||
public $whitelistMode = false;
|
|
||||||
/**
|
|
||||||
* @var null|array|StorageInterface additional list to invert current mode validation
|
|
||||||
* @see \Ely\TempMailBuster\Validator::validate() implementation for additional info
|
|
||||||
*/
|
|
||||||
public $secondaryStorage;
|
|
||||||
|
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
@ -36,31 +21,23 @@ class TempmailValidator extends Validator
|
|||||||
|
|
||||||
protected function validateValue($value)
|
protected function validateValue($value)
|
||||||
{
|
{
|
||||||
$validator = $this->buildValidator();
|
$validator = $this->buildValidator($value);
|
||||||
if ($validator->validate($value)) {
|
$results = $validator->getValidationResults()->asArray();
|
||||||
return null;
|
if ($results['disposable_email_provider']) {
|
||||||
|
return [$this->message, []];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [$this->message, []];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function buildValidator(string $email): EmailValidator
|
||||||
* @return TempmailBuster
|
|
||||||
*/
|
|
||||||
protected function buildValidator()
|
|
||||||
{
|
{
|
||||||
/** @var \Ely\TempMailBuster\LoaderInterface $loader */
|
$emailAddress = new EmailAddress($email);
|
||||||
$loader = new $this->loader;
|
$emailDataProvider = new EmailDataProvider();
|
||||||
/** @var StorageInterface $primaryStorage */
|
$emailValidationResults = new ValidationResults();
|
||||||
$primaryStorage = new $this->storage($loader->load());
|
$emailValidator = new EmailValidator($emailAddress, $emailValidationResults, $emailDataProvider);
|
||||||
$secondaryStorage = $this->secondaryStorage;
|
$emailValidator->registerValidator(new DisposableEmailValidator());
|
||||||
if (is_array($this->secondaryStorage)) {
|
|
||||||
$secondaryStorage = new $this->storage($this->secondaryStorage);
|
|
||||||
}
|
|
||||||
|
|
||||||
$validator = new TempmailBuster($primaryStorage, $secondaryStorage);
|
return $emailValidator;
|
||||||
$validator->whitelistMode($this->whitelistMode);
|
|
||||||
|
|
||||||
return $validator;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ namespace Ely\Yii2;
|
|||||||
|
|
||||||
include __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
|
include __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
|
||||||
|
|
||||||
use Ely\TempMailBuster\Storage;
|
|
||||||
use yii\base\Model;
|
use yii\base\Model;
|
||||||
use yii\console\Application;
|
use yii\console\Application;
|
||||||
|
|
||||||
@ -22,70 +21,42 @@ class TempmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testValidateValue()
|
public function testValidateValue()
|
||||||
{
|
{
|
||||||
$validator = new TempmailValidator();
|
$validator = new TempmailValidator();
|
||||||
$this->assertNull($this->callValidateValue($validator, 'team@ely.by'));
|
$this->assertTrue($validator->validate('team@ely.by'));
|
||||||
$this->assertEquals([$validator->message, []], $this->callValidateValue($validator, 'h4l29@spam4.me'));
|
$this->assertFalse($validator->validate('h4l29@spam4.me', $error));
|
||||||
}
|
$this->assertSame('the input value is not allowed email address.', $error);
|
||||||
|
|
||||||
public function testBuildValidator()
|
|
||||||
{
|
|
||||||
$validator = new TempmailValidator();
|
|
||||||
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $this->callBuildValidator($validator));
|
|
||||||
|
|
||||||
$domains = ['mojang\.com'];
|
|
||||||
$validator = new TempmailValidator(['secondaryStorage' => $domains]);
|
|
||||||
$buster = $this->callBuildValidator($validator);
|
|
||||||
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $buster);
|
|
||||||
$this->assertEquals($domains, $buster->getSecondaryStorage()->getItems());
|
|
||||||
|
|
||||||
$validator = new TempmailValidator(['secondaryStorage' => new Storage($domains)]);
|
|
||||||
$buster = $this->callBuildValidator($validator);
|
|
||||||
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $buster);
|
|
||||||
$this->assertEquals($domains, $buster->getSecondaryStorage()->getItems());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValidatorOnModel()
|
public function testValidatorOnModel()
|
||||||
{
|
{
|
||||||
$model = new DummyModel();
|
$model = $this->createDummyModel();
|
||||||
$model->email = 'team@ely.by';
|
$model->email = 'team@ely.by';
|
||||||
$this->assertTrue($model->validate());
|
$this->assertTrue($model->validate());
|
||||||
|
|
||||||
$model = new DummyModel();
|
$model = $this->createDummyModel();
|
||||||
$model->email = 'spam@spam4.me';
|
$model->email = 'spam@spam4.me';
|
||||||
$this->assertFalse($model->validate());
|
$this->assertFalse($model->validate());
|
||||||
$this->assertNotEmpty($model->getErrors('email'));
|
$this->assertSame('Email is not allowed email address.', $model->getFirstError('email'));
|
||||||
|
|
||||||
|
$model = $this->createDummyModel('{attribute} with custom message.');
|
||||||
|
$model->email = 'spam@spam4.me';
|
||||||
|
$this->assertFalse($model->validate());
|
||||||
|
$this->assertSame('Email with custom message.', $model->getFirstError('email'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function createDummyModel(string $customMessage = null) {
|
||||||
* @param TempmailValidator $object
|
return new class(['customMessage' => $customMessage]) extends Model
|
||||||
* @return \Ely\TempMailBuster\Validator
|
{
|
||||||
*/
|
public $email;
|
||||||
private function callBuildValidator($object)
|
|
||||||
{
|
|
||||||
$class = new \ReflectionClass($object);
|
|
||||||
$method = $class->getMethod('buildValidator');
|
|
||||||
$method->setAccessible(true);
|
|
||||||
|
|
||||||
return $method->invoke($object);
|
public $customMessage;
|
||||||
}
|
|
||||||
|
|
||||||
private function callValidateValue($object, $value)
|
public function rules()
|
||||||
{
|
{
|
||||||
$class = new \ReflectionClass($object);
|
return [
|
||||||
$method = $class->getMethod('validateValue');
|
[['email'], TempmailValidator::class, 'message' => $this->customMessage],
|
||||||
$method->setAccessible(true);
|
];
|
||||||
|
}
|
||||||
return $method->invokeArgs($object, [$value]);
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DummyModel extends Model
|
|
||||||
{
|
|
||||||
public $email;
|
|
||||||
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
[['email'], TempmailValidator::className()],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user