Skip to content

Instantly share code, notes, and snippets.

@JanMikes
Last active November 28, 2016 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanMikes/d3f9f15166722f9bf166c6722a35c2e6 to your computer and use it in GitHub Desktop.
Save JanMikes/d3f9f15166722f9bf166c6722a35c2e6 to your computer and use it in GitHub Desktop.
Draft of possible immutable append-only entities
<?php
class Article
{
/** @var \DateTime */
private $createdAt;
/** @var ArticleHistory[] */
private $historyChanges;
public function __construct($text)
{
$this->createdAt = new \DateTime;
$this->updateText($text);
}
public function updateText($text)
{
$this->historyChanges[] = new ArticleHistory($this, $text);
}
public function text()
{
// případně jiná logika, na vytáhnutí poslední změny
return $this->historyChanges->last()->text();
}
}
class ArticleHistory
{
/** @var Article */
private $article;
/** @var \DateTime */
private $createdAt;
/** @var string */
private $text;
public function __construct(Article $article, $text)
{
$this->createdAt = new \DateTime;
$this->article = $article;
$this->text = $text;
}
public function text()
{
return $this->text;
}
}
<?php
class Article
{
/** @var UuidInterface */
private $id; // primary key
/** @var UuidInterface */
private $articleId; // non-unique
/** @var string */
private $text;
public function __construct($text, $articleId = NULL)
{
$this->id = Uuid::uuid4();
$this->articleId = $articleId ? $articleId : Uuid::uuid4();
$this->text = $text;
}
public function updateText($text)
{
return new Article($text, $this->articleId);
}
public function text()
{
return $this->text;
}
}
Possible solution - dispatch domain event, something like "ArticleEdited" every time any editation happens -> this domain event could be persisted and saved into other persistence storage type (nosql etc.).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment