1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EmailValidator\Validator; |
6
|
|
|
|
7
|
|
|
use EmailValidator\EmailAddress; |
8
|
|
|
use EmailValidator\Policy; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Validates and transforms Gmail email addresses |
12
|
|
|
* |
13
|
|
|
* This validator handles Gmail-specific functionality including: |
14
|
|
|
* - Detection of Gmail addresses using the "plus trick" |
15
|
|
|
* - Transformation of Gmail addresses to remove the plus trick |
16
|
|
|
* - Sanitization of Gmail addresses by removing dots |
17
|
|
|
* |
18
|
|
|
* @since 2.0.0 |
19
|
|
|
*/ |
20
|
|
|
class GmailValidator extends AValidator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Validates if an email address is a Gmail address |
24
|
|
|
* |
25
|
|
|
* @param EmailAddress $email The email address to validate |
26
|
|
|
* @return bool True if the email is a valid Gmail address, false otherwise |
27
|
|
|
* @since 2.0.0 |
28
|
|
|
*/ |
29
|
|
|
public function validate(EmailAddress $email): bool |
30
|
|
|
{ |
31
|
|
|
$domain = $email->getDomain(); |
32
|
|
|
return $domain !== null && in_array($domain, ['gmail.com', 'googlemail.com'], true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Determines if a Gmail account is using the "plus trick" |
37
|
|
|
* |
38
|
|
|
* @param EmailAddress $email The email address to check |
39
|
|
|
* @return bool True if the Gmail address uses the plus trick, false otherwise |
40
|
|
|
* @since 2.0.0 |
41
|
|
|
*/ |
42
|
|
|
public function isGmailWithPlusChar(EmailAddress $email): bool |
43
|
|
|
{ |
44
|
|
|
return $this->validate($email) && $email->isGmailWithPlusChar(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a Gmail address with the "plus trick" portion removed |
49
|
|
|
* |
50
|
|
|
* @param EmailAddress $email The email address to transform |
51
|
|
|
* @return string The Gmail address without the plus trick portion |
52
|
|
|
* @since 2.0.0 |
53
|
|
|
*/ |
54
|
|
|
public function getGmailAddressWithoutPlus(EmailAddress $email): string |
55
|
|
|
{ |
56
|
|
|
return $this->validate($email) ? $email->getGmailAddressWithoutPlus() : $email->getEmailAddress(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a sanitized Gmail address (plus trick removed and dots removed) |
61
|
|
|
* |
62
|
|
|
* @param EmailAddress $email The email address to sanitize |
63
|
|
|
* @return string The sanitized Gmail address |
64
|
|
|
* @since 2.0.0 |
65
|
|
|
*/ |
66
|
|
|
public function getSanitizedGmailAddress(EmailAddress $email): string |
67
|
|
|
{ |
68
|
|
|
return $this->validate($email) ? $email->getSanitizedGmailAddress() : $email->getEmailAddress(); |
69
|
|
|
} |
70
|
|
|
} |