2020-06-09 16:02:25 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Orrisroot\Mail;
|
|
|
|
|
|
|
|
class Address
|
|
|
|
{
|
2021-06-02 10:26:49 +09:00
|
|
|
public const EMAIL_REGEX = '[a-zA-Z0-9]+(?:[_\\.\\-][a-zA-Z0-9]+)*@(?:[a-zA-Z0-9]+(?:[\\.\\-][a-zA-Z0-9]+)*)+\\.[a-zA-Z]{2,}';
|
2020-06-09 16:02:25 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string name
|
|
|
|
*/
|
|
|
|
private string $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string email address
|
|
|
|
*/
|
|
|
|
private string $email;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor.
|
|
|
|
*
|
|
|
|
* @param string $name name
|
|
|
|
* @param string $email email address
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function __construct(string $name, string $email)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
if (!$this->validateEmail($email)) {
|
|
|
|
throw new \Exception('Invalid email address found: '.$email);
|
|
|
|
}
|
|
|
|
$this->email = $email;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get name.
|
|
|
|
*
|
|
|
|
* @return string name
|
|
|
|
*/
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get email.
|
|
|
|
*
|
|
|
|
* @return string email
|
|
|
|
*/
|
|
|
|
public function getEmail(): string
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check wheter string is email.
|
|
|
|
*
|
|
|
|
* @param string $text email
|
|
|
|
*
|
|
|
|
* @return bool false if not email string
|
|
|
|
*/
|
|
|
|
public function validateEmail(string $email): bool
|
|
|
|
{
|
|
|
|
return false !== preg_match('/^'.self::EMAIL_REGEX.'$/', $email);
|
|
|
|
}
|
|
|
|
}
|