Skip to content

Commit 8ab64f5

Browse files
committed
Refactor Email class to implement canonical email normalization, replacing deprecated methods and introducing a new Canonicals namespace. Update README and tests to reflect changes in method names and functionality.
1 parent 2e7b660 commit 8ab64f5

23 files changed

+105
-122
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ $email = new Email('[email protected]');
3030
$email->get(); // [email protected]
3131
$email->getLocal(); // user
3232
$email->getDomain(); // example.com
33-
$email->getLocalOnly(); // user
34-
$email->getDomainOnly(); // example.com
33+
$email->getLocal(); // user
34+
$email->getDomain(); // example.com
3535
$email->isValid(); // true
3636
$email->hasValidLocal(); // true
3737
$email->hasValidDomain(); // true
@@ -89,7 +89,6 @@ The Email class provides the following constants for email formatting:
8989
* **get()** - Return full email address.
9090
* **getLocal()** - Return local part (before @).
9191
* **getDomain()** - Return domain part (after @).
92-
* **getLocalOnly()** - Return email without domain part (local only).
9392
* **getDomainOnly()** - Return email without local part (domain only).
9493
* **isValid()** - Check if email is valid format.
9594
* **hasValidLocal()** - Check if email has valid local part.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer;
3+
namespace Utopia\Emails\Canonicals;
44

55
/**
66
* Abstract Email Provider
@@ -15,13 +15,13 @@ abstract class Provider
1515
abstract public function supports(string $domain): bool;
1616

1717
/**
18-
* Normalize the email address according to provider rules
18+
* Get the canonical form of the email address according to provider rules
1919
*
2020
* @param string $local The local part of the email (before @)
2121
* @param string $domain The domain part of the email (after @)
22-
* @return array Array with 'local' and 'domain' keys containing normalized values
22+
* @return array Array with 'local' and 'domain' keys containing canonical values
2323
*/
24-
abstract public function normalize(string $local, string $domain): array;
24+
abstract public function getCanonical(string $local, string $domain): array;
2525

2626
/**
2727
* Get the canonical domain for this provider

src/Emails/Normalizer/Providers/Fastmail.php renamed to src/Emails/Canonicals/Providers/Fastmail.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* Fastmail
@@ -23,7 +23,7 @@ public function supports(string $domain): bool
2323
return in_array($domain, self::SUPPORTED_DOMAINS, true);
2424
}
2525

26-
public function normalize(string $local, string $domain): array
26+
public function getCanonical(string $local, string $domain): array
2727
{
2828
// Convert to lowercase
2929
$normalizedLocal = $this->toLowerCase($local);

src/Emails/Normalizer/Providers/Generic.php renamed to src/Emails/Canonicals/Providers/Generic.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* Generic
@@ -19,7 +19,7 @@ public function supports(string $domain): bool
1919
return true;
2020
}
2121

22-
public function normalize(string $local, string $domain): array
22+
public function getCanonical(string $local, string $domain): array
2323
{
2424
// Convert to lowercase
2525
$normalizedLocal = $this->toLowerCase($local);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* Gmail
@@ -23,7 +23,7 @@ public function supports(string $domain): bool
2323
return in_array($domain, self::SUPPORTED_DOMAINS, true);
2424
}
2525

26-
public function normalize(string $local, string $domain): array
26+
public function getCanonical(string $local, string $domain): array
2727
{
2828
// Convert to lowercase
2929
$normalizedLocal = $this->toLowerCase($local);

src/Emails/Normalizer/Providers/Icloud.php renamed to src/Emails/Canonicals/Providers/Icloud.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* iCloud
@@ -23,7 +23,7 @@ public function supports(string $domain): bool
2323
return in_array($domain, self::SUPPORTED_DOMAINS, true);
2424
}
2525

26-
public function normalize(string $local, string $domain): array
26+
public function getCanonical(string $local, string $domain): array
2727
{
2828
// Convert to lowercase
2929
$normalizedLocal = $this->toLowerCase($local);

src/Emails/Normalizer/Providers/Outlook.php renamed to src/Emails/Canonicals/Providers/Outlook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* Outlook
@@ -26,7 +26,7 @@ public function supports(string $domain): bool
2626
return in_array($domain, self::SUPPORTED_DOMAINS, true);
2727
}
2828

29-
public function normalize(string $local, string $domain): array
29+
public function getCanonical(string $local, string $domain): array
3030
{
3131
// Convert to lowercase
3232
$normalizedLocal = $this->toLowerCase($local);

src/Emails/Normalizer/Providers/Protonmail.php renamed to src/Emails/Canonicals/Providers/Protonmail.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* ProtonMail
@@ -23,7 +23,7 @@ public function supports(string $domain): bool
2323
return in_array($domain, self::SUPPORTED_DOMAINS, true);
2424
}
2525

26-
public function normalize(string $local, string $domain): array
26+
public function getCanonical(string $local, string $domain): array
2727
{
2828
// Convert to lowercase
2929
$normalizedLocal = $this->toLowerCase($local);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Utopia\Emails\Normalizer\Providers;
3+
namespace Utopia\Emails\Canonicals\Providers;
44

5-
use Utopia\Emails\Normalizer\Provider;
5+
use Utopia\Emails\Canonicals\Provider;
66

77
/**
88
* Yahoo
@@ -26,7 +26,7 @@ public function supports(string $domain): bool
2626
return in_array($domain, self::SUPPORTED_DOMAINS, true);
2727
}
2828

29-
public function normalize(string $local, string $domain): array
29+
public function getCanonical(string $local, string $domain): array
3030
{
3131
// Convert to lowercase
3232
$normalizedLocal = $this->toLowerCase($local);

src/Emails/Email.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Utopia\Emails;
44

55
use Exception;
6-
use Utopia\Emails\Normalizer\Provider;
7-
use Utopia\Emails\Normalizer\Providers\Fastmail;
8-
use Utopia\Emails\Normalizer\Providers\Generic;
9-
use Utopia\Emails\Normalizer\Providers\Gmail;
10-
use Utopia\Emails\Normalizer\Providers\Icloud;
11-
use Utopia\Emails\Normalizer\Providers\Outlook;
12-
use Utopia\Emails\Normalizer\Providers\Protonmail;
13-
use Utopia\Emails\Normalizer\Providers\Yahoo;
6+
use Utopia\Emails\Canonicals\Provider;
7+
use Utopia\Emails\Canonicals\Providers\Fastmail;
8+
use Utopia\Emails\Canonicals\Providers\Generic;
9+
use Utopia\Emails\Canonicals\Providers\Gmail;
10+
use Utopia\Emails\Canonicals\Providers\Icloud;
11+
use Utopia\Emails\Canonicals\Providers\Outlook;
12+
use Utopia\Emails\Canonicals\Providers\Protonmail;
13+
use Utopia\Emails\Canonicals\Providers\Yahoo;
1414

1515
class Email
1616
{
@@ -135,22 +135,6 @@ public function getDomain(): string
135135
return $this->domain;
136136
}
137137

138-
/**
139-
* Return email without local part (domain only)
140-
*/
141-
public function getDomainOnly(): string
142-
{
143-
return $this->domain;
144-
}
145-
146-
/**
147-
* Return email without domain part (local only)
148-
*/
149-
public function getLocalOnly(): string
150-
{
151-
return $this->local;
152-
}
153-
154138
/**
155139
* Check if email is valid format
156140
*/
@@ -289,30 +273,30 @@ public function hasSubdomain(): bool
289273
}
290274

291275
/**
292-
* Normalize email address (remove extra spaces, convert to lowercase)
276+
* Get the email address (as provided, just lowercased and trimmed)
293277
*/
294-
public function normalize(): string
278+
public function getAddress(): string
295279
{
296280
return $this->email;
297281
}
298282

299283
/**
300-
* Get unique email address by removing aliases and provider-specific variations
284+
* Get the canonical email address by removing aliases and provider-specific variations
301285
* This method removes plus addressing, dot notation (for Gmail), and other aliasing techniques
302286
* to return the canonical form of the email address
303287
*/
304-
public function getUnique(): string
288+
public function getCanonical(): string
305289
{
306290
$provider = $this->getProviderForDomain($this->domain);
307-
$normalized = $provider->normalize($this->local, $this->domain);
291+
$canonical = $provider->getCanonical($this->local, $this->domain);
308292

309-
return $normalized['local'].'@'.$normalized['domain'];
293+
return $canonical['local'].'@'.$canonical['domain'];
310294
}
311295

312296
/**
313-
* Check if the email domain is supported for normalization
297+
* Check if the email domain is supported for canonical form generation
314298
*/
315-
public function isNormalizationSupported(): bool
299+
public function isCanonicalSupported(): bool
316300
{
317301
return $this->isDomainSupported($this->domain);
318302
}

0 commit comments

Comments
 (0)