mirror of
https://github.com/elyby/yii2-tempmail-validator.git
synced 2025-05-31 14:11:55 +05:30
Initial commit
This commit is contained in:
66
src/TempmailValidator.php
Normal file
66
src/TempmailValidator.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Ely\Yii2;
|
||||
|
||||
use Ely\TempMailBuster\StorageInterface;
|
||||
use Ely\TempMailBuster\Validator as TempmailBuster;
|
||||
use Yii;
|
||||
use yii\validators\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()
|
||||
{
|
||||
parent::init();
|
||||
if ($this->message === null) {
|
||||
$this->message = Yii::t('yii', '{attribute} is not allowed email address.');
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateValue($value)
|
||||
{
|
||||
$validator = $this->buildValidator();
|
||||
if ($validator->validate($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [$this->message, []];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TempmailBuster
|
||||
*/
|
||||
protected function buildValidator()
|
||||
{
|
||||
/** @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);
|
||||
}
|
||||
|
||||
$validator = new TempmailBuster($primaryStorage, $secondaryStorage);
|
||||
$validator->whitelistMode($this->whitelistMode);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user