mirror of
https://github.com/retailcrm/transparent-email.git
synced 2025-04-15 15:00:56 +00:00
Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
|
635a11fd5b | ||
|
c2f7ea5767 | ||
|
eb7c29e45b |
9 changed files with 56 additions and 21 deletions
|
@ -9,6 +9,9 @@ php:
|
||||||
- 7.1
|
- 7.1
|
||||||
- 7.2
|
- 7.2
|
||||||
- 7.3
|
- 7.3
|
||||||
|
- 8.0
|
||||||
|
- 8.1
|
||||||
|
- 8.2
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- ./vendor/bin/phpunit
|
- ./vendor/bin/phpunit
|
||||||
|
|
|
@ -3,10 +3,11 @@
|
||||||
"description": "Remove aliases from email and get primary email account",
|
"description": "Remove aliases from email and get primary email account",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^7.0.0"
|
"php": "^8.0.0",
|
||||||
|
"ext-mbstring": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "6.5.14",
|
"phpunit/phpunit": "^9.0.0",
|
||||||
"satooshi/php-coveralls": "^1.0.1",
|
"satooshi/php-coveralls": "^1.0.1",
|
||||||
"codacy/coverage": "^1.0.3"
|
"codacy/coverage": "^1.0.3"
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,19 +8,25 @@ class Email implements EmailInterface
|
||||||
{
|
{
|
||||||
use EmailTrait;
|
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);
|
list($this->localPart, $this->domain) = explode('@', $email);
|
||||||
if (!$caseSensitive) {
|
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!");
|
throw new InvalidEmailException("Email '{$email}' is not valid!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,15 +10,15 @@ class ServiceCollector implements ServiceCollectorInterface
|
||||||
{
|
{
|
||||||
private $services = [];
|
private $services = [];
|
||||||
|
|
||||||
public function addService(ServiceInterface $service)
|
public function addService(ServiceInterface $service): void
|
||||||
{
|
{
|
||||||
$this->services[] = $service;
|
$this->services[] = $service;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getIterator()
|
public function getIterator(): \Traversable
|
||||||
{
|
{
|
||||||
foreach ($this->services as $service) {
|
foreach ($this->services as $service) {
|
||||||
yield $service;
|
yield $service;
|
||||||
|
|
|
@ -9,5 +9,5 @@ interface ServiceCollectorInterface extends \IteratorAggregate
|
||||||
/**
|
/**
|
||||||
* @return ServiceInterface[]
|
* @return ServiceInterface[]
|
||||||
*/
|
*/
|
||||||
public function getIterator();
|
public function getIterator(): \Traversable;
|
||||||
}
|
}
|
|
@ -127,7 +127,7 @@ class EditableEmailTest extends TestCase
|
||||||
$editable = new EditableEmail($email);
|
$editable = new EditableEmail($email);
|
||||||
$new = $editable->lowerCaseLocalPartIf($condition);
|
$new = $editable->lowerCaseLocalPartIf($condition);
|
||||||
$this->assertSame(!$condition, $editable === $new);
|
$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());
|
$this->assertSame($email->getDomain(), $new->getDomain());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,18 +18,20 @@ class EmailTest extends TestCase
|
||||||
* @param string $localPart
|
* @param string $localPart
|
||||||
* @param string $domain
|
* @param string $domain
|
||||||
* @param bool $caseSensitive
|
* @param bool $caseSensitive
|
||||||
|
* @param bool $cyrillicAllowed
|
||||||
*/
|
*/
|
||||||
public function testConstructor(
|
public function testConstructor(
|
||||||
string $email,
|
string $email,
|
||||||
bool $expectedException,
|
bool $expectedException,
|
||||||
string $localPart = '',
|
string $localPart = '',
|
||||||
string $domain = '',
|
string $domain = '',
|
||||||
bool $caseSensitive = false
|
bool $caseSensitive = false,
|
||||||
|
bool $cyrillicAllowed = false
|
||||||
) {
|
) {
|
||||||
if ($expectedException) {
|
if ($expectedException) {
|
||||||
$this->expectException(InvalidEmailException::class);
|
$this->expectException(InvalidEmailException::class);
|
||||||
}
|
}
|
||||||
$object = new Email($email, $caseSensitive);
|
$object = new Email($email, $caseSensitive, $cyrillicAllowed);
|
||||||
$this->assertSame($localPart, $object->getLocalPart());
|
$this->assertSame($localPart, $object->getLocalPart());
|
||||||
$this->assertSame($domain, $object->getDomain());
|
$this->assertSame($domain, $object->getDomain());
|
||||||
}
|
}
|
||||||
|
@ -41,6 +43,13 @@ class EmailTest extends TestCase
|
||||||
['.johndoe@example.com', true],
|
['.johndoe@example.com', true],
|
||||||
['Jane.Doe@Example.COM', false, 'Jane.Doe', 'example.com', true],
|
['Jane.Doe@Example.COM', false, 'Jane.Doe', 'example.com', true],
|
||||||
['Jane.Doe@Example.COM', false, 'jane.doe', 'example.com'],
|
['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],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,10 +15,11 @@ class MailRuTest extends TestCase
|
||||||
*
|
*
|
||||||
* @param string $inputEmail
|
* @param string $inputEmail
|
||||||
* @param string $outputEmail
|
* @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
|
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@MaiL.Ru', 'foobar@mail.ru'],
|
['fOObar@MaiL.Ru', 'foobar@mail.ru'],
|
||||||
['foobar+alias@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],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,10 +15,11 @@ class YandexRuTest extends TestCase
|
||||||
*
|
*
|
||||||
* @param string $inputEmail
|
* @param string $inputEmail
|
||||||
* @param string $outputEmail
|
* @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
|
public function providerGetPrimaryEmail() : array
|
||||||
|
@ -33,6 +34,16 @@ class YandexRuTest extends TestCase
|
||||||
['foobar@yandex.by', 'foobar@yandex.ru'],
|
['foobar@yandex.by', 'foobar@yandex.ru'],
|
||||||
['foobar@yandex.kz', 'foobar@yandex.ru'],
|
['foobar@yandex.kz', 'foobar@yandex.ru'],
|
||||||
['foobar@yandex.ua', '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],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue