Created
October 28, 2014 23:24
-
-
Save keyvanakbary/69c56a9e6ac9648cf9f1 to your computer and use it in GitHub Desktop.
Voter example
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 | |
| //Just a simple relationship | |
| class Voter { | |
| private $id; | |
| private $votedTo; | |
| private $votedAt; | |
| public function votesTo(Party $party) { | |
| if ($this->votedTo) { | |
| throw new DomainException('Already voted'); | |
| } | |
| $this->votedTo = $party; | |
| $this->votedAt = new Datetime(); | |
| } | |
| } | |
| //With a value object | |
| class Voter { | |
| private $id; | |
| private $vote; | |
| public function votesTo(Party $party) { | |
| if ($this->vote) { | |
| throw new DomainException('Already voted'); | |
| } | |
| $this->vote = new Vote($party); | |
| } | |
| } | |
| class Vote { | |
| private $createdAt; | |
| private $toParty; | |
| public function __construct(Party $party) { | |
| $this->toParty = $party; | |
| $this->createdAt = new DateTime(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment