Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto multi validation #335

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/EmailValidatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Egulias\EmailValidator;

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\RFCValidation;

class EmailValidatorFactory
{
/** @var [] */
protected static array $defaultValidators = [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you expecting or encouraging extension? This prevents it. There is also no entry point to add additional/other validators than this.

You can do it via constructor arguments with sensible defaults (is like your idea here, but with simple extension point)

RFCValidation::class,
NoRFCWarningsValidation::class,
DNSCheckValidation::class
];

/**
* @param string $emailAddress
* @return array
*/
public static function create(string $emailAddress): array
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which are the reasons behing making it static? It prevents being injected as dependency.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you are returning the lists of results, not a validator ready for use. I'd suggest to change the name to something like validationsResults or similar.

{
$validator = new EmailValidator();
$result = [];

foreach (self::$defaultValidators as $key => $val) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just use ´MultipleValidationWithAnd´ at construction time. And remove line 15.

$result[get_class(new $val)] = $validator->isValid($emailAddress, new $val);
}

return $result;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the validator's Result type not useful? Maybe adding which validation is giving which result would be useful?

}
}