Custom data provider class
nunorbatista opened this issue · comments
Hi,
Need some guidance because I can't seem to understand the method that takes care of the Custom Email Provider.
I created a new Email Provider that extends EmailValidation\EmailDataProviderInterface, but how do I apply it to the \EmailValidation class?
My code:
$provider = new CustomEmailDataProvider; // my Custom Provider
$validator = \EmailValidation\EmailValidatorFactory::create('alice@example.com);
$validator->registerValidator(new CustomValidator()); // my Custom Validator, that works with no issues
$result = $validator->getValidationResults()->asArray();
How do I implement the custom $provider into the \EmailValidation\EmailValidatorFactory ?
Thanks in advance.
Hi Nuno,
You'd need to create your own factory to achieve that. Something like:
<?php
namespace YourNamespace;
use EmailValidation\Validations\Validator;
use CustomEmailDataProvider
class CustomEmailValidatorFactory extends EmailValidatorFactory
{
public static function create(string $emailAddress): EmailValidator
{
$emailAddress = new EmailAddress($emailAddress);
$emailDataProvider = new CustomEmailDataProvider();
$emailValidationResults = new ValidationResults();
$emailValidator = new EmailValidator($emailAddress, $emailValidationResults, $emailDataProvider);
foreach (self::$defaultValidators as $validator) {
$emailValidator->registerValidator(new $validator);
}
return $emailValidator;
}
}
I've just pushed up a change that changes EmailValidatorFactory::$defaultValidators
from private to protected so you'll need to do a composer update
to bring in the update.
Hi Dave,
Thanks for the answer, it was helpful.
After creating a new EmailValidatorFactory I was getting this error:
Typed property EmailValidation\Validations\Validator::$emailDataProvider must be an instance of EmailValidation\EmailDataProvider or null, app\components\CustomEmailDataProvider used
The issue is with the file src/Validations/Validator.php as it expects to receive a EmailDataProvider when in fact is receiving a EmailDataProviderInterface.
I changed it to :
abstract class Validator
{
private ?EmailAddress $emailAddress;
private ?EmailDataProvideInterface $emailDataProvider;
and now it works.
I don't know if this can impact other functionalities of the code. Up to you if you want to accept this change.
Also, I suggest to update the documentation in order to reflect this implementation of the custom email data provider.
Thanks again!
Nuno
Cheers for the heads up. I've pushed a fix