introduced PostId and PostStatus and changed naming from Storage to Persistence

This commit is contained in:
Dominik Liebler
2018-06-14 18:28:40 +02:00
parent 2ab82dbfbf
commit 6c726d66b7
18 changed files with 334 additions and 743 deletions

View File

@@ -5,10 +5,15 @@ namespace DesignPatterns\More\Repository\Domain;
class Post
{
/**
* @var int
* @var PostId
*/
private $id;
/**
* @var PostStatus
*/
private $status;
/**
* @var string
*/
@@ -19,10 +24,11 @@ class Post
*/
private $text;
public static function draft(int $id, string $title, string $text): Post
public static function draft(PostId $id, string $title, string $text): Post
{
return new self(
$id,
PostStatus::fromString(PostStatus::STATE_DRAFT),
$title,
$text
);
@@ -31,29 +37,37 @@ class Post
public static function fromState(array $state): Post
{
return new self(
$state['id'],
PostId::fromInt($state['id']),
PostStatus::fromInt($state['statusId']),
$state['title'],
$state['text']
);
}
/**
* @param int $id
* @param string $text
* @param PostId $id
* @param PostStatus $status
* @param string $title
* @param string $text
*/
private function __construct(int $id, string $title, string $text)
private function __construct(PostId $id, PostStatus $status, string $title, string $text)
{
$this->id = $id;
$this->status = $status;
$this->text = $text;
$this->title = $title;
}
public function getId(): int
public function getId(): PostId
{
return $this->id;
}
public function getStatus(): PostStatus
{
return $this->status;
}
public function getText(): string
{
return $this->text;

View File

@@ -0,0 +1,42 @@
<?php
namespace DesignPatterns\More\Repository\Domain;
/**
* This is a perfect example of a value object that is identifiable by it's value alone and
* is guaranteed to be valid each time an instance is created. Another important property of value objects
* is immutability.
*
* Notice also the use of a named constructor (fromInt) which adds a little context when creating an instance.
*/
class PostId
{
/**
* @var int
*/
private $id;
public static function fromInt(int $id)
{
self::ensureIsValid($id);
return new self($id);
}
private function __construct(int $id)
{
$this->id = $id;
}
public function toInt(): int
{
return $this->id;
}
private static function ensureIsValid(int $id)
{
if ($id <= 0) {
throw new \InvalidArgumentException('Invalid PostId given');
}
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace DesignPatterns\More\Repository\Domain;
/**
* Like PostId, this is a value object which holds the value of the current status of a Post. It can be constructed
* either from a string or int and is able to validate itself. An instance can then be converted back to int or string.
*/
class PostStatus
{
const STATE_DRAFT_ID = 1;
const STATE_PUBLISHED_ID = 2;
const STATE_DRAFT = 'draft';
const STATE_PUBLISHED = 'published';
private static $validStates = [
self::STATE_DRAFT_ID => self::STATE_DRAFT,
self::STATE_PUBLISHED_ID => self::STATE_PUBLISHED,
];
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $name;
public static function fromInt(int $statusId)
{
self::ensureIsValidId($statusId);
return new self($statusId, self::$validStates[$statusId]);
}
public static function fromString(string $status)
{
self::ensureIsValidName($status);
return new self(array_search($status, self::$validStates), $status);
}
private function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
public function toInt(): int
{
return $this->id;
}
/**
* there is a reason that I avoid using __toString() as it operates outside of the stack in PHP
* and is therefor not able to operate well with exceptions
*/
public function toString(): string
{
return $this->name;
}
private static function ensureIsValidId(int $status)
{
if (!in_array($status, array_keys(self::$validStates), true)) {
throw new \InvalidArgumentException('Invalid status id given');
}
}
private static function ensureIsValidName(string $status)
{
if (!in_array($status, self::$validStates, true)) {
throw new \InvalidArgumentException('Invalid status name given');
}
}
}