Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactor to individual rules #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Rules/DotsInUsername.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ImLiam\UniqueGmailAddress\Rules;

class DotsInUsername implements EmailRule
{
public function normalize(string $email): string
{
[$username, $domain] = explode('@', $email, 2);
imliam marked this conversation as resolved.
Show resolved Hide resolved
$username = str_replace('.', '', $username);

return $username . '@' . $domain;
imliam marked this conversation as resolved.
Show resolved Hide resolved
}

public function regex(string $email): string
{
[$username, $domain] = explode('@', $email, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 This is redundant: the other method also contains this line

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how it's redundant - it's a different function and other than this one line, the functions do different things. I see this one line to split up the localpart and domain as simple enough to be duplicated when it's needed.


$characters = array_map(function ($character) {
return preg_quote($character);
}, str_split($username));

// Each character in the username can have optional dots between them
return join('(\.?)+', $characters) . '@' . $domain;
}
}
10 changes: 10 additions & 0 deletions src/Rules/EmailRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ImLiam\UniqueGmailAddress\Rules;

interface EmailRule
{
public function normalize(string $email): string;

public function regex(string $email): string;
}
26 changes: 26 additions & 0 deletions src/Rules/GmailDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ImLiam\UniqueGmailAddress\Rules;

use ImLiam\UniqueGmailAddress\Util;

class GmailDomain implements EmailRule
{
public function normalize(string $email): string
{
if (! Util::isGmailAddress($email)) {
return $email;
}

[$username, $domain] = explode('@', $email, 2);

return $username . '@gmail.com';
}

public function regex(string $email): string
{
[$username, $domain] = explode('@', $email, 2);

return $username . '@(gmail|googlemail).com';
}
}
18 changes: 18 additions & 0 deletions src/Rules/MixedCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ImLiam\UniqueGmailAddress\Rules;

class MixedCase implements EmailRule
{
public function normalize(string $email): string
{
return strtolower($email);
}

public function regex(string $email): string
{
[$username, $domain] = explode('@', $email, 2);

return '(?i)' . $username . '(?-i)' . '@' . $domain;
}
}
28 changes: 28 additions & 0 deletions src/Rules/TagInUsername.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ImLiam\UniqueGmailAddress\Rules;

class TagInUsername implements EmailRule
{
protected string $separator;

public function __construct(string $separator = '+')
{
$this->separator = $separator;
}

public function normalize(string $email): string
{
[$username, $domain] = explode('@', $email, 2);
[$username, $tag] = explode($this->separator, $username, 2);

return $username . '@' . $domain;
}

public function regex(string $email): string
{
[$username, $domain] = explode('@', $email, 2);

return $username . '(' . preg_quote($this->separator) . '.*)?' . '@' . $domain;
}
}
47 changes: 47 additions & 0 deletions src/UniqueEmailAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace ImLiam\UniqueGmailAddress;

use ImLiam\UniqueGmailAddress\Rules\EmailRule;

class UniqueEmailAddress
{
protected string $email;

/** @var EmailRule[] */
protected array $rules = [];

public function __construct(string $email)
{
$this->email = $email;
}

public function normalize(): string
{
return array_reduce($this->rules, function (string $email, EmailRule $rule) {
return $rule->normalize($email);
}, $this->email);
}

public function getRegexWithDelimiters()
{
return '/' . $this->getRegex() . '/';
}

public function getRegex()
{
$email = str_replace('@', '\\@', $this->normalize());

return array_reduce($this->rules, function (string $email, EmailRule $rule) {
return $rule->regex($email);
}, $email);
}

public function matches($email)
{
/** @var array $matches */
preg_match($this->getRegexWithDelimiters(), $email, $matches);

return ! empty($matches);
}
}
81 changes: 12 additions & 69 deletions src/UniqueGmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,22 @@

namespace ImLiam\UniqueGmailAddress;

class UniqueGmailAddress
{
protected string $originalEmail;
protected string $email;
use ImLiam\UniqueGmailAddress\Rules\DotsInUsername;
use ImLiam\UniqueGmailAddress\Rules\GmailDomain;
use ImLiam\UniqueGmailAddress\Rules\MixedCase;
use ImLiam\UniqueGmailAddress\Rules\TagInUsername;

class UniqueGmailAddress extends UniqueEmailAddress
{
public function __construct(string $email)
{
$this->originalEmail = $email;
$this->email = $this->normalizeAddress();
}

public function isGmailAddress()
{
return $this->stringEndsWith($this->originalEmail, '@googlemail.com')
|| $this->stringEndsWith($this->originalEmail, '@gmail.com');
}

public function normalizeAddress()
{
if (! $this->isGmailAddress()) {
return $this->originalEmail;
}

$username = trim(strstr($this->originalEmail, '@', true));
$username = explode('+', $username)[0];
$username = str_replace('.', '', $username);

return "{$username}@gmail.com";
}

private function stringEndsWith(string $haystack, string $needle): bool
{
$length = strlen($needle);

if ($length === 0) {
return true;
}

return substr($haystack, -$length) === $needle;
}

private function getEscapedEmailUsername()
{
$emailUsername = strstr($this->email, '@', true);

$usernameCharacters = array_map(function ($character) {
return preg_quote($character);
}, str_split($emailUsername));

return join('(\.?)+', $usernameCharacters); // Each character in the username can have optional dots between them
}

public function getRegexWithDelimiters()
{
return '/' . $this->getRegex() . '/';
}
parent::__construct($email);

public function getRegex()
{
if (! $this->isGmailAddress()) {
return '^' . preg_quote($this->email) . '$'; // Must be an exact match
if (Util::isGmailAddress($this->email) || Util::isGsuiteAddress($this->email)) {
$this->rules[GmailDomain::class] = new GmailDomain();
// $this->rules[MixedCase::class] = new MixedCase();
Copy link
Contributor

@szepeviktor szepeviktor Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From: =?utf-8?b?U1rDiVBF?= Viktor <[email protected]>
To: [email protected]
Subject: case

Gmail is case-insensitive. Also domain names are case-insensitive.

$this->rules[DotsInUsername::class] = new DotsInUsername();
$this->rules[TagInUsername::class] = new TagInUsername('+');
}

return '^' . $this->getEscapedEmailUsername() // Must start with the email username
. '(\+.*)?' // Can optionally include anything after a literal plus
. '\@(gmail|googlemail).com$'; // Must end with @gmail.com or @googlemail.com
}

public function matches($emailToCompare)
{
/** @var array $matches */
preg_match($this->getRegexWithDelimiters(), $emailToCompare, $matches);

return ! empty($matches);
}
}
6 changes: 3 additions & 3 deletions src/UniqueGmailAddressRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public function __construct($table = 'users', $column = 'email')

public function passes($attribute, $value)
{
$validator = new UniqueGmailAddress($value);

if (! $validator->isGmailAddress()) {
if (! Util::isGmailAddress($value) && ! Util::isGsuiteAddress($value)) {
return true;
}

$validator = new UniqueGmailAddress($value);

return ! DB::table($this->table)->where($this->column, 'REGEXP', $validator->getRegexWithDelimiters())->exists();
}

Expand Down
31 changes: 31 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ImLiam\UniqueGmailAddress;

class Util
{
public static function isGmailAddress(string $email): bool
{
[$username, $domain] = explode('@', $email, 2);

return in_array($domain, ['gmail.com', 'googlemail.com']);
}

public static function isGsuiteAddress(string $email): bool
{
if (static::isGmailAddress($email)) {
return false;
}

[$username, $domain] = explode('@', $email, 2);

/** @var string[] $mxRecords */
$successful = getmxrr($domain, $mxRecords);

if (! $successful) {
return false;
}

return in_array('aspmx.l.google.com', $mxRecords);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return in_array('aspmx.l.google.com', $mxRecords);
return in_array('aspmx.l.google.com', $mxRecords) || in_array('ASPMX.L.GOOGLE.COM', $mxRecords);

Copy link
Contributor

@szepeviktor szepeviktor Sep 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually all mail exchangers should be converted to lowercase then compared.
Try return array_udiff(['aspmx.l.google.com'], $mxRecords, 'strcasecmp') === [];
I could be wrong.

}
}
3 changes: 2 additions & 1 deletion tests/UniqueGmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function a_denormalized_gmail_address_will_also_match()
{
$validator = new UniqueGmailAddress('[email protected]');

dd($validator->getRegex());
$this->assertTrue($validator->matches('[email protected]'));
$this->assertTrue($validator->matches('[email protected]'));
$this->assertTrue($validator->matches('[email protected]'));
Expand All @@ -39,6 +40,6 @@ public function a_denormalized_gmail_address_can_be_normalized()
{
$validator = new UniqueGmailAddress('[email protected]');

$this->assertEquals('[email protected]', $validator->normalizeAddress());
$this->assertEquals('[email protected]', $validator->normalize());
}
}