add initial project files.
This commit is contained in:
69
lib/Mail/Address.php
Normal file
69
lib/Mail/Address.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Orrisroot\Mail;
|
||||
|
||||
class Address
|
||||
{
|
||||
const EMAIL_REGEX = '[a-zA-Z0-9]+(?:[_\\.\\-][a-zA-Z0-9]+)*@(?:[a-zA-Z0-9]+(?:[\\.\\-][a-zA-Z0-9]+)*)+\\.[a-zA-Z]{2,}';
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user