Refactoring so regex has it's own variables

This commit is contained in:
Aaron Mueller
2021-04-06 07:49:57 -07:00
parent f48fc452af
commit 63cb61f591

View File

@ -32,6 +32,17 @@ class EmailValidator extends Validator
* @see allowName * @see allowName
*/ */
public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/'; public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/';
/**
* @var string the regular expression used to validate the part before the @ symbol, used if ASCII conversion fails to validate the address.
* @see http://www.regular-expressions.info/email.html
*/
public $patternASCII = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*$/';
/**
* @var string the regular expression used to validate email addresses with the name part before the @ symbol, used if ASCII conversion fails to validate the address.
* This property is used only when [[allowName]] is true.
* @see allowName
*/
public $fullPatternASCII = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*$/';
/** /**
* @var bool whether to allow name in the email address (e.g. "John Smith <john.smith@example.com>"). Defaults to false. * @var bool whether to allow name in the email address (e.g. "John Smith <john.smith@example.com>"). Defaults to false.
* @see fullPattern * @see fullPattern
@ -186,11 +197,7 @@ class EmailValidator extends Validator
{ {
$ascii = $this->idnToAscii($value); $ascii = $this->idnToAscii($value);
if ($ascii === false) { if ($ascii === false) {
$parts = explode('@', $this->pattern, 2); if (preg_match($this->patternASCII, $value) || ($this->allowName && preg_match($this->fullPatternASCII, $value))) {
$pattern = $parts[0] . "$/";
$fullPatternParts = explode('@', $this->fullPattern);
$fullPattern = $fullPatternParts[0] . '@' . $fullPatternParts[1] ."$/";
if (preg_match($pattern, $value) || ($this->allowName && preg_match($fullPattern, $value))) {
return $value; return $value;
} }
} }