Skip to content

Instantly share code, notes, and snippets.

@nexik
Created December 20, 2014 22:11
Show Gist options
  • Save nexik/f72f9964733fb7428a4f to your computer and use it in GitHub Desktop.
Save nexik/f72f9964733fb7428a4f to your computer and use it in GitHub Desktop.
User and EmailAddress as ValueObject
<?php
namespace AppBundle\Entity\User
class EmailAddress
{
private $address;
public function __construct($address)
{
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid email', $address));
}
$this->address = $address;
}
public function __toString()
{
return $this->address;
}
}
<?php
namespace AppBundle\Entity;
class User
{
private $id;
private $emailAddress;
public function __construct(EmailAddress $emailAddress)
{
$this->emailAddress = $emailAddress;
}
public function getEmailAddress()
{
return $this->emailAddress;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment