Created
December 20, 2014 22:11
-
-
Save nexik/f72f9964733fb7428a4f to your computer and use it in GitHub Desktop.
User and EmailAddress as ValueObject
This file contains hidden or 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 | |
| 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; | |
| } | |
| } |
This file contains hidden or 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 | |
| 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