Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Created October 28, 2014 23:24
Show Gist options
  • Select an option

  • Save keyvanakbary/69c56a9e6ac9648cf9f1 to your computer and use it in GitHub Desktop.

Select an option

Save keyvanakbary/69c56a9e6ac9648cf9f1 to your computer and use it in GitHub Desktop.
Voter example
<?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