Skip to content

Commit cc5234a

Browse files
optimize addressValidation and emailValidation in Address class
1 parent 2e73381 commit cc5234a

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Address.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,25 @@ public function getEmail(): string
7676
/**
7777
* Validate if an address is valid.
7878
*
79-
* @param string $email The email address to validate.
79+
* Supports formats:
80+
* - john@example.com
81+
* - John Doe <john@example.com>
82+
*
83+
* @param string $address
8084
* @return bool
8185
*/
8286
public static function addressValidation(string $address): bool
8387
{
84-
if (preg_match('/^(?:[^<]+<[^>]+>|', $address, $matches)) {
85-
if (static::emailValidation($matches[2])) {
86-
return true;
87-
}
88+
if (preg_match('/<([^>]+)>/', $address, $matches)) {
89+
$email = $matches[1];
90+
} else {
91+
$email = $address;
8892
}
89-
return false;
93+
94+
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
9095
}
9196

97+
9298
/**
9399
* Validate if an email address is valid.
94100
*
@@ -97,10 +103,8 @@ public static function addressValidation(string $address): bool
97103
*/
98104
public static function emailValidation(string $email): bool
99105
{
100-
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
101-
return false;
102-
}
103-
return true;
106+
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
107+
104108
}
105109

106110
/**

0 commit comments

Comments
 (0)