mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-04 22:17:25 +02:00
introduced PostId and PostStatus and changed naming from Storage to Persistence
This commit is contained in:
@@ -3,9 +3,10 @@
|
||||
namespace DesignPatterns\More\Repository;
|
||||
|
||||
use DesignPatterns\More\Repository\Domain\Post;
|
||||
use DesignPatterns\More\Repository\Domain\PostId;
|
||||
|
||||
/**
|
||||
* This class is situated between Entity layer (class Post) and access object layer (MemoryStorage).
|
||||
* This class is situated between Entity layer (class Post) and access object layer (Persistence).
|
||||
*
|
||||
* Repository encapsulates the set of objects persisted in a data store and the operations performed over them
|
||||
* providing a more object-oriented view of the persistence layer
|
||||
@@ -16,26 +17,26 @@ use DesignPatterns\More\Repository\Domain\Post;
|
||||
class PostRepository
|
||||
{
|
||||
/**
|
||||
* @var Storage
|
||||
* @var Persistence
|
||||
*/
|
||||
private $persistence;
|
||||
|
||||
public function __construct(Storage $persistence)
|
||||
public function __construct(Persistence $persistence)
|
||||
{
|
||||
$this->persistence = $persistence;
|
||||
}
|
||||
|
||||
public function generateId(): int
|
||||
public function generateId(): PostId
|
||||
{
|
||||
return $this->persistence->generateId();
|
||||
return PostId::fromInt($this->persistence->generateId());
|
||||
}
|
||||
|
||||
public function findById(int $id): Post
|
||||
public function findById(PostId $id): Post
|
||||
{
|
||||
try {
|
||||
$arrayData = $this->persistence->retrieve($id);
|
||||
$arrayData = $this->persistence->retrieve($id->toInt());
|
||||
} catch (\OutOfBoundsException $e) {
|
||||
throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id), 0, $e);
|
||||
throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id->toInt()), 0, $e);
|
||||
}
|
||||
|
||||
return Post::fromState($arrayData);
|
||||
@@ -44,7 +45,8 @@ class PostRepository
|
||||
public function save(Post $post)
|
||||
{
|
||||
$this->persistence->persist([
|
||||
'id' => $post->getId(),
|
||||
'id' => $post->getId()->toInt(),
|
||||
'statusId' => $post->getStatus()->toInt(),
|
||||
'text' => $post->getText(),
|
||||
'title' => $post->getTitle(),
|
||||
]);
|
||||
|
Reference in New Issue
Block a user