Skip to content

Instantly share code, notes, and snippets.

@nikolaposa
Created March 7, 2019 18:39
Show Gist options
  • Save nikolaposa/3fb86305c964c0a9725503c69e2c9835 to your computer and use it in GitHub Desktop.
Save nikolaposa/3fb86305c964c0a9725503c69e2c9835 to your computer and use it in GitHub Desktop.
Email Address VO
<?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