Compare commits

...

3 commits

Author SHA1 Message Date
kruglov
635a11fd5b Php version update 2024-01-23 12:36:03 +03:00
Alexey
c2f7ea5767
Merge pull request #1 from theunclehonnor/add-cyrillic-support
Added support for cyrillic characters in email
2022-05-06 11:25:39 +03:00
theunclehonnor
eb7c29e45b Added support for cyrillic characters in email 2022-05-05 18:06:15 +03:00
9 changed files with 56 additions and 21 deletions

View file

@ -9,6 +9,9 @@ php:
- 7.1
- 7.2
- 7.3
- 8.0
- 8.1
- 8.2
script:
- ./vendor/bin/phpunit

View file

@ -3,10 +3,11 @@
"description": "Remove aliases from email and get primary email account",
"type": "library",
"require": {
"php": "^7.0.0"
"php": "^8.0.0",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "6.5.14",
"phpunit/phpunit": "^9.0.0",
"satooshi/php-coveralls": "^1.0.1",
"codacy/coverage": "^1.0.3"
},

View file

@ -8,19 +8,25 @@ class Email implements EmailInterface
{
use EmailTrait;
public function __construct(string $email, bool $caseSensitive = false)
const PATTERN_WITH_SUPPORT_CYRILLIC = '/^[a-zA-Zа-яА-Я0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Zа-яА-Я0-9](?:[a-zA-Zа-яА-Я0-9-]{0,61}[a-zA-Zа-яА-Я0-9])?(?:\.[a-zA-Zа-яА-Я0-9](?:[a-zA-Zа-яА-Я0-9-]{0,61}[a-zA-Zа-яА-Я0-9])?)+$/u';
public function __construct(string $email, bool $caseSensitive = false, bool $cyrillicAllowed = false)
{
$this->validateEmail($email);
$this->validateEmail($email, $cyrillicAllowed);
list($this->localPart, $this->domain) = explode('@', $email);
if (!$caseSensitive) {
$this->localPart = strtolower($this->localPart);
$this->localPart = mb_strtolower($this->localPart);
}
$this->domain = strtolower($this->domain);
$this->domain = mb_strtolower($this->domain);
}
private function validateEmail(string $email)
private function validateEmail(string $email, bool $cyrillicAllowed)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
if ($cyrillicAllowed && !preg_match(self::PATTERN_WITH_SUPPORT_CYRILLIC, $email)) {
throw new InvalidEmailException("Email '{$email}' is not valid!");
}
if (!$cyrillicAllowed && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidEmailException("Email '{$email}' is not valid!");
}
}

View file

@ -10,15 +10,15 @@ class ServiceCollector implements ServiceCollectorInterface
{
private $services = [];
public function addService(ServiceInterface $service)
public function addService(ServiceInterface $service): void
{
$this->services[] = $service;
}
/**
* @inheritdoc
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): \Traversable
{
foreach ($this->services as $service) {
yield $service;

View file

@ -9,5 +9,5 @@ interface ServiceCollectorInterface extends \IteratorAggregate
/**
* @return ServiceInterface[]
*/
public function getIterator();
public function getIterator(): \Traversable;
}

View file

@ -127,7 +127,7 @@ class EditableEmailTest extends TestCase
$editable = new EditableEmail($email);
$new = $editable->lowerCaseLocalPartIf($condition);
$this->assertSame(!$condition, $editable === $new);
$this->assertSame($isLowerCase, strtolower($editable->getLocalPart()) === $new->getLocalPart());
$this->assertSame($isLowerCase, mb_strtolower($editable->getLocalPart()) === $new->getLocalPart());
$this->assertSame($email->getDomain(), $new->getDomain());
}

View file

@ -18,18 +18,20 @@ class EmailTest extends TestCase
* @param string $localPart
* @param string $domain
* @param bool $caseSensitive
* @param bool $cyrillicAllowed
*/
public function testConstructor(
string $email,
bool $expectedException,
string $localPart = '',
string $domain = '',
bool $caseSensitive = false
bool $caseSensitive = false,
bool $cyrillicAllowed = false
) {
if ($expectedException) {
$this->expectException(InvalidEmailException::class);
}
$object = new Email($email, $caseSensitive);
$object = new Email($email, $caseSensitive, $cyrillicAllowed);
$this->assertSame($localPart, $object->getLocalPart());
$this->assertSame($domain, $object->getDomain());
}
@ -41,6 +43,13 @@ class EmailTest extends TestCase
['.johndoe@example.com', true],
['Jane.Doe@Example.COM', false, 'Jane.Doe', 'example.com', true],
['Jane.Doe@Example.COM', false, 'jane.doe', 'example.com'],
// Cyrillic use
['тест тест@example.com', true],
['.тест@example.com', true],
['Тест.Тест@Example.COM', false, 'Тест.Тест', 'example.com', true, true],
['Тест.Тест@Example.COM', false, 'тест.тест', 'example.com', false, true],
['Тест.Тест@Тест.РУ', false, 'Тест.Тест', 'тест.ру', true, true],
['Тест.Тест@Тест.РУ', false, 'тест.тест', 'тест.ру', false, true],
];
}
}

View file

@ -15,10 +15,11 @@ class MailRuTest extends TestCase
*
* @param string $inputEmail
* @param string $outputEmail
* @param bool $cyrillicAllowed
*/
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail)
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail, bool $cyrillicAllowed = false)
{
$this->assertEquals($outputEmail, (new MailRu())->getPrimaryEmail(new Email($inputEmail)));
$this->assertEquals($outputEmail, (new MailRu())->getPrimaryEmail(new Email($inputEmail, false, $cyrillicAllowed)));
}
public function providerGetPrimaryEmail() : array
@ -27,6 +28,10 @@ class MailRuTest extends TestCase
['foobar@MAIL.RU', 'foobar@mail.ru'],
['fOObar@MaiL.Ru', 'foobar@mail.ru'],
['foobar+alias@mail.ru', 'foobar@mail.ru'],
// Cyrillic use
['иванов@MAIL.RU', 'иванов@mail.ru', true],
['иванОВ@MaiL.Ru', 'иванов@mail.ru', true],
['иванов+alias@mail.ru', 'иванов@mail.ru', true],
];
}

View file

@ -15,10 +15,11 @@ class YandexRuTest extends TestCase
*
* @param string $inputEmail
* @param string $outputEmail
* @param bool $cyrillicAllowed
*/
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail)
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail, bool $cyrillicAllowed = false)
{
$this->assertEquals($outputEmail, (new YandexRu())->getPrimaryEmail(new Email($inputEmail)));
$this->assertEquals($outputEmail, (new YandexRu())->getPrimaryEmail(new Email($inputEmail, false, $cyrillicAllowed)));
}
public function providerGetPrimaryEmail() : array
@ -33,6 +34,16 @@ class YandexRuTest extends TestCase
['foobar@yandex.by', 'foobar@yandex.ru'],
['foobar@yandex.kz', 'foobar@yandex.ru'],
['foobar@yandex.ua', 'foobar@yandex.ru'],
// Cyrillic use
['иванов@YANDEX.RU', 'иванов@yandex.ru', true],
['иванОВ@YAndEX.ru', 'иванов@yandex.ru', true],
['иванов+alias@yandex.ru', 'иванов@yandex.ru', true],
['ИвановИван@ya.ru', 'ивановиван@yandex.ru', true],
['Иванов.Иван@ya.ru', 'иванов-иван@yandex.ru', true],
['иванов@yandex.com', 'иванов@yandex.ru', true],
['иванов@yandex.by', 'иванов@yandex.ru', true],
['иванов@yandex.kz', 'иванов@yandex.ru', true],
['иванов@yandex.ua', 'иванов@yandex.ru', true],
];
}