Created
May 27, 2021 09:50
-
-
Save BerezhniyDmitro/4c7e623bd94ea3011722825db9b7f308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\VO; | |
use InvalidArgumentException; | |
/** | |
* Class Email обьект значение которое инкапсулирует в себе email значение | |
*/ | |
final class Email | |
{ | |
/** | |
* @var string email | |
*/ | |
private $email; | |
/** | |
* Email constructor. | |
* | |
* @param string $email | |
*/ | |
private function __construct(string $email) | |
{ | |
$this->email = $email; | |
} | |
/** | |
* Метод создает Обьект-значение Email из строки | |
* | |
* @param string $email | |
* | |
* @return self | |
*/ | |
public static function createFromString(string $email): Email | |
{ | |
$email = mb_strtolower($email); | |
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
throw new InvalidArgumentException('Переданное значение должно быть корректным email адресом'); | |
} | |
return new self($email); | |
} | |
/** | |
* Метод возвращает значение | |
* | |
* @return string | |
*/ | |
public function __toString(): string | |
{ | |
return $this->email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment