0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】フリーメールかを判定する関数を自作してみた

Last updated at Posted at 2022-06-14

Laravelになかったので「Freemail」というフリーメールかどうかを判定するRailsのライブラリを参考に自作してみた。

環境

Laravel v9.5.1 (PHP v8.1.3)

実装

  1. フリーメールのドメインを配列で返すようFreeEmailモデルで定義

オブジェクト指向の観点からするとクラスにするまでもないが、まあよしとする。

app/Models/FreeEmail.php
class FreeEmail
{
  public static function list(): array
  {
    return [
      "0-mail.com",
      "027168.com",
      ...
    ]
  }
}
  1. Emailクラスに、emailからドメインを取るfqdn関数とフリーメールかどうか判定するisFreeEmail関数を定義
app/Models/Email.php
class Email
{
  private $email;

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

  public function fqdn(): string
  {
    return substr($this->email, strrpos($this->email, '@') + 1);
  }

  public function isFreeEmail(): bool
  {
    return in_array($this->fqdn(), FreeEmail::list());
  }
}

使い方

$email = 'aaa@gmail.com';
$email->fqdn();
// => "gmail.com"
$email->isFreeEmail();
// => true

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?