We're trying to design validation classes with a common interface. This would apply to all validators. We would define a validator interface with a validate($input):bool method. Each validator would represent a specific data type (email address, login, etc.); the validate method would implement the appropriate logic.
Option 1: Instantiate a validator object (either directly or through a factory) to perform validation
class EmailValidator implements ValidatorInterface
{
public function validate($input) { ... }
}
// Usage:
$var = new EmailValidator();
if ($var->validate($val)) { ... }
Option 2: Validator interface defines a static validate() method implemented by each validator
class EmailValidator implements ValidatorInterface
{
public static function validate($input) { ... }
}
// Usage:
if (EmailValidator::validate($val)) { ... }
Which would be the best option to use? Are there better ways to go about this than those laid out above?