Created
March 7, 2019 18:39
-
-
Save nikolaposa/3fb86305c964c0a9725503c69e2c9835 to your computer and use it in GitHub Desktop.
Email Address VO
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 My\Model; | |
final class EmailAddress implements ValueObject | |
{ | |
/** @var string */ | |
private $email; | |
private function __construct(string $email) | |
{ | |
$this->email = $email; | |
} | |
public static function fromString(string $email): EmailAddress | |
{ | |
if (! \filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
throw new EmailAddressNotValid(); | |
} | |
return new self($email); | |
} | |
public function toString(): string | |
{ | |
return $this->email; | |
} | |
public function sameValueAs(ValueObject $other): bool | |
{ | |
/** @var $other EmailAddress */ | |
return \get_class($this) === \get_class($other) && $this->email === $other->email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment