diff --git a/composer.json b/composer.json index 2b99a3d..206a445 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,9 @@ } ], "require": { + "php": ">=7", "yiisoft/yii2": "*", - "ely/php-tempmailbuster": "~1.0.2" + "daveearley/daves-email-validation-tool": "^0.1.5" }, "require-dev": { "phpunit/phpunit": "~4.8 || ~5.0" diff --git a/src/TempmailValidator.php b/src/TempmailValidator.php index e092f79..b04463b 100644 --- a/src/TempmailValidator.php +++ b/src/TempmailValidator.php @@ -1,31 +1,16 @@ buildValidator(); - if ($validator->validate($value)) { - return null; + $validator = $this->buildValidator($value); + $results = $validator->getValidationResults()->asArray(); + if ($results['disposable_email_provider']) { + return [$this->message, []]; } - return [$this->message, []]; + return null; } - /** - * @return TempmailBuster - */ - protected function buildValidator() + protected function buildValidator(string $email): EmailValidator { - /** @var \Ely\TempMailBuster\LoaderInterface $loader */ - $loader = new $this->loader; - /** @var StorageInterface $primaryStorage */ - $primaryStorage = new $this->storage($loader->load()); - $secondaryStorage = $this->secondaryStorage; - if (is_array($this->secondaryStorage)) { - $secondaryStorage = new $this->storage($this->secondaryStorage); - } + $emailAddress = new EmailAddress($email); + $emailDataProvider = new EmailDataProvider(); + $emailValidationResults = new ValidationResults(); + $emailValidator = new EmailValidator($emailAddress, $emailValidationResults, $emailDataProvider); + $emailValidator->registerValidator(new DisposableEmailValidator()); - $validator = new TempmailBuster($primaryStorage, $secondaryStorage); - $validator->whitelistMode($this->whitelistMode); - - return $validator; + return $emailValidator; } } diff --git a/tests/TempmailValidatorTest.php b/tests/TempmailValidatorTest.php index c76a6e7..d10cee5 100644 --- a/tests/TempmailValidatorTest.php +++ b/tests/TempmailValidatorTest.php @@ -3,7 +3,6 @@ namespace Ely\Yii2; include __DIR__ . '/../vendor/yiisoft/yii2/Yii.php'; -use Ely\TempMailBuster\Storage; use yii\base\Model; use yii\console\Application; @@ -22,70 +21,42 @@ class TempmailValidatorTest extends \PHPUnit_Framework_TestCase public function testValidateValue() { $validator = new TempmailValidator(); - $this->assertNull($this->callValidateValue($validator, 'team@ely.by')); - $this->assertEquals([$validator->message, []], $this->callValidateValue($validator, 'h4l29@spam4.me')); - } - - 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()); + $this->assertTrue($validator->validate('team@ely.by')); + $this->assertFalse($validator->validate('h4l29@spam4.me', $error)); + $this->assertSame('the input value is not allowed email address.', $error); } public function testValidatorOnModel() { - $model = new DummyModel(); + $model = $this->createDummyModel(); $model->email = 'team@ely.by'; $this->assertTrue($model->validate()); - $model = new DummyModel(); + $model = $this->createDummyModel(); $model->email = 'spam@spam4.me'; $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')); } - /** - * @param TempmailValidator $object - * @return \Ely\TempMailBuster\Validator - */ - private function callBuildValidator($object) - { - $class = new \ReflectionClass($object); - $method = $class->getMethod('buildValidator'); - $method->setAccessible(true); + private function createDummyModel(string $customMessage = null) { + return new class(['customMessage' => $customMessage]) extends Model + { + public $email; - return $method->invoke($object); - } + public $customMessage; - private function callValidateValue($object, $value) - { - $class = new \ReflectionClass($object); - $method = $class->getMethod('validateValue'); - $method->setAccessible(true); - - return $method->invokeArgs($object, [$value]); + public function rules() + { + return [ + [['email'], TempmailValidator::class, 'message' => $this->customMessage], + ]; + } + }; } } -class DummyModel extends Model -{ - public $email; - - public function rules() - { - return [ - [['email'], TempmailValidator::className()], - ]; - } -}