diff --git a/src/Emails/EditableEmail.php b/src/Emails/EditableEmail.php index cb223bd..e013cdd 100644 --- a/src/Emails/EditableEmail.php +++ b/src/Emails/EditableEmail.php @@ -25,6 +25,14 @@ class EditableEmail implements EmailInterface return $copy; } + public function replaceInLocalPart(string $toReplace, string $replacement) : EditableEmail + { + $copy = clone $this; + $copy->localPart = str_replace($toReplace, $replacement, $this->localPart); + + return $copy; + } + public function removeSuffixAlias(string $delimiter) : EditableEmail { $copy = clone $this; diff --git a/src/Services/YandexRu.php b/src/Services/YandexRu.php new file mode 100644 index 0000000..7d36cd7 --- /dev/null +++ b/src/Services/YandexRu.php @@ -0,0 +1,41 @@ +removeSuffixAlias('+') + ->replaceInLocalPart('.', '-') + ->lowerCaseLocalPartIf(true) + ->setDomain($this->mapDomain($email->getDomain())); + } + + public function isSupported(EmailInterface $email) : bool + { + return in_array($email->getDomain(), ['ya.ru', 'yandex.com', 'yandex.ru', 'yandex.by', 'yandex.kz', 'yandex.ua']); + } + + protected function getDomainMapping() : array + { + return [ + 'ya.ru' => 'yandex.ru', + 'yandex.com' => 'yandex.ru', + 'yandex.by' => 'yandex.ru', + 'yandex.kz' => 'yandex.ru', + 'yandex.ua' => 'yandex.ru' + ]; + } + + private function mapDomain(string $domain) : string + { + return $this->getDomainMapping()[$domain] ?? $domain; + } +} \ No newline at end of file diff --git a/src/TransparentEmailFactory.php b/src/TransparentEmailFactory.php index 8e6232c..34e68f5 100644 --- a/src/TransparentEmailFactory.php +++ b/src/TransparentEmailFactory.php @@ -11,6 +11,7 @@ use bkrukowski\TransparentEmail\Services\OutlookCom; use bkrukowski\TransparentEmail\Services\TlenPl; use bkrukowski\TransparentEmail\Services\Www33MailCom; use bkrukowski\TransparentEmail\Services\YahooCom; +use bkrukowski\TransparentEmail\Services\YandexRu; class TransparentEmailFactory { @@ -39,7 +40,8 @@ class TransparentEmailFactory Www33MailCom::class, TlenPl::class, AppsGoogleCom::class, - MailRu::class + MailRu::class, + YandexRu::class ]; } -} \ No newline at end of file +} diff --git a/tests/Services/YandexRuTest.php b/tests/Services/YandexRuTest.php new file mode 100644 index 0000000..797f7fd --- /dev/null +++ b/tests/Services/YandexRuTest.php @@ -0,0 +1,63 @@ +assertEquals($outputEmail, (new YandexRu())->getPrimaryEmail(new Email($inputEmail))); + } + + public function providerGetPrimaryEmail() : array + { + return [ + ['foobar@YANDEX.RU', 'foobar@yandex.ru'], + ['fOObar@YAndEX.ru', 'foobar@yandex.ru'], + ['foobar+alias@yandex.ru', 'foobar@yandex.ru'], + ['JaneDoe@ya.ru', 'janedoe@yandex.ru'], + ['Jane.Doe@ya.ru', 'jane-doe@yandex.ru'], + ['foobar@yandex.com', 'foobar@yandex.ru'], + ['foobar@yandex.by', 'foobar@yandex.ru'], + ['foobar@yandex.kz', 'foobar@yandex.ru'], + ['foobar@yandex.ua', 'foobar@yandex.ru'], + ]; + } + + /** + * @dataProvider providerIsSupported + * + * @param string $domain + * @param bool $result + */ + public function testIsSupported(string $domain, bool $result) + { + $this->assertSame($result, (new YandexRu())->isSupported(new Email('Jane.Doe@' . $domain))); + } + + public function providerIsSupported() : array + { + return [ + ['yandex.ru', true], + ['yandex.com', true], + ['yandex.by', true], + ['yandex.kz', true], + ['yandex.ua', true], + ['yandex.RU', true], + ['yan.dex.ru', false], + ['YANDEX.RU', true], + ]; + } +} \ No newline at end of file