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

Conversation

mariuszmalek
Copy link

I created this branch to have every validation in one place by class name.

Example use:

$array = EmailValidatorFactory::create("my@email.com");

@mariuszmalek
Copy link
Author

Thanks to this, I can now easily handle all the information and save it in the database.

Copy link
Owner

@egulias egulias left a comment

Choose a reason for hiding this comment

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

Hey. Thanks for your patience. Leaving you here some comments.

* @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.

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)

$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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants