FilterEmailValidation.php
TLDR
This file defines a class called FilterEmailValidation
which implements the EmailValidation
interface. It provides methods to validate an email address and retrieve validation errors and warnings.
Methods
__construct
This method is the constructor of the class. It allows you to create a new instance of FilterEmailValidation
and set the flags to pass to the filter_var
function.
unicode
This method is a static method that creates a new instance of FilterEmailValidation
with the FILTER_FLAG_EMAIL_UNICODE
flag set. This allows the validation of email addresses with any unicode characters in the local part.
isValid
This method takes an email address and an EmailLexer
instance as parameters and returns a boolean value indicating if the given email address is valid. It uses the filter_var
function to perform the validation with the provided flags.
getError
This method returns the validation error as an instance of InvalidEmail
. In this implementation, it always returns null
as there are no validation errors.
getWarnings
This method returns an array of validation warnings as instances of Warning
. In this implementation, it always returns an empty array as there are no validation warnings.
<?php
namespace Illuminate\Validation\Concerns;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\EmailValidation;
class FilterEmailValidation implements EmailValidation
{
/**
* The flags to pass to the filter_var function.
*
* @var int|null
*/
protected $flags;
/**
* Create a new validation instance.
*
* @param int $flags
* @return void
*/
public function __construct($flags = null)
{
$this->flags = $flags;
}
/**
* Create a new instance which allows any unicode characters in local-part.
*
* @return static
*/
public static function unicode()
{
return new static(FILTER_FLAG_EMAIL_UNICODE);
}
/**
* Returns true if the given email is valid.
*
* @param string $email
* @param \Egulias\EmailValidator\EmailLexer $emailLexer
* @return bool
*/
public function isValid(string $email, EmailLexer $emailLexer): bool
{
return is_null($this->flags)
? filter_var($email, FILTER_VALIDATE_EMAIL) !== false
: filter_var($email, FILTER_VALIDATE_EMAIL, $this->flags) !== false;
}
/**
* Returns the validation error.
*
* @return \Egulias\EmailValidator\Result\InvalidEmail|null
*/
public function getError(): ?InvalidEmail
{
return null;
}
/**
* Returns the validation warnings.
*
* @return \Egulias\EmailValidator\Warning\Warning[]
*/
public function getWarnings(): array
{
return [];
}
}