diff --git a/src/EmailChecker/Adapter/ArrayAdapter.php b/src/EmailChecker/Adapter/ArrayAdapter.php index adb341d..98adbe5 100644 --- a/src/EmailChecker/Adapter/ArrayAdapter.php +++ b/src/EmailChecker/Adapter/ArrayAdapter.php @@ -16,17 +16,23 @@ * * @author Matthieu Moquet */ -final class ArrayAdapter implements AdapterInterface +class ArrayAdapter implements AdapterInterface { + /** + * @var array + */ + private array $domainSet; + /** * @param string[] $domains List of throwaway domains */ - public function __construct(private array $domains) + public function __construct(array $domains) { + $this->domainSet = array_flip($domains); } public function isThrowawayDomain(string $domain): bool { - return in_array($domain, $this->domains, true); + return isset($this->domainSet[$domain]); } } diff --git a/src/EmailChecker/Adapter/BuiltInAdapter.php b/src/EmailChecker/Adapter/BuiltInAdapter.php index 16c10f4..ad8a9c8 100644 --- a/src/EmailChecker/Adapter/BuiltInAdapter.php +++ b/src/EmailChecker/Adapter/BuiltInAdapter.php @@ -23,27 +23,10 @@ * * @author Matthieu Moquet */ -final class BuiltInAdapter implements AdapterInterface +final class BuiltInAdapter extends ArrayAdapter { - /** - * @var string[]|null - */ - protected array|null $domains = null; - - public function isThrowawayDomain(string $domain): bool + public function __construct() { - return in_array($domain, $this->getDomains(), true); - } - - /** - * @return string[] - */ - private function getDomains(): array - { - if (null === $this->domains) { - $this->domains = (new ThrowawayDomains())->toArray(); - } - - return $this->domains; + parent::__construct((new ThrowawayDomains())->toArray()); } } diff --git a/src/EmailChecker/Adapter/FileAdapter.php b/src/EmailChecker/Adapter/FileAdapter.php index 1313e6a..3b4ade0 100644 --- a/src/EmailChecker/Adapter/FileAdapter.php +++ b/src/EmailChecker/Adapter/FileAdapter.php @@ -18,13 +18,8 @@ * * @author Matthieu Moquet */ -final class FileAdapter implements AdapterInterface +final class FileAdapter extends ArrayAdapter { - /** - * @var string[] - */ - protected array $domains; - /** * @param string $filename Filename containing all domains */ @@ -35,11 +30,6 @@ public function __construct(string $filename) throw new \InvalidArgumentException(sprintf('File "%s" not found', $filename)); } - $this->domains = Utilities::parseLines($content); - } - - public function isThrowawayDomain(string $domain): bool - { - return in_array($domain, $this->domains, true); + parent::__construct(Utilities::parseLines($content)); } } diff --git a/src/EmailChecker/Adapter/GaufretteAdapter.php b/src/EmailChecker/Adapter/GaufretteAdapter.php index d2715f6..df96145 100644 --- a/src/EmailChecker/Adapter/GaufretteAdapter.php +++ b/src/EmailChecker/Adapter/GaufretteAdapter.php @@ -19,20 +19,10 @@ * * @author Matthieu Moquet */ -final class GaufretteAdapter implements AdapterInterface +final class GaufretteAdapter extends ArrayAdapter { - /** - * @var string[] - */ - private array $domains; - public function __construct(File $file) { - $this->domains = Utilities::parseLines($file->getContent()); - } - - public function isThrowawayDomain(string $domain): bool - { - return in_array($domain, $this->domains, true); + parent::__construct(Utilities::parseLines($file->getContent())); } }