public function __construct(array $options = [], DbConnection $db = null)
{
parent::__construct($options);
if (!$db) {
// try to get db instance from default Dependency Injection
$di = Di::getDefault();
if ($di instanceof DiInterface && $di->has('db')) {
$db = $di->get('db');
}
}
if (!$db instanceof DbConnection) {
throw new ValidationException('Validator Uniqueness require connection to database');
}
if (!$this->hasOption('table')) {
throw new ValidationException('Validator require table option to be set');
}
if (!$this->hasOption('column')) {
throw new ValidationException('Validator require column option to be set');
}
if ($this->hasOption('exclude')) {
$exclude = $this->getOption('exclude');
if (!isset($exclude['column']) || empty($exclude['column'])) {
throw new ValidationException('Validator with "exclude" option require column option to be set');
}
if (!isset($exclude['value']) || empty($exclude['value'])) {
throw new ValidationException('Validator with "exclude" option require value option to be set');
}
}
$this->db = $db;
}