Files
DesignPatternsPHP/More/Repository/PostRepository.php
diwipl fadf0f15cf Update PostRepository.php
sprintf function in findById method was missing second argument.
2016-10-11 21:27:46 +02:00

47 lines
1.2 KiB
PHP

<?php
namespace DesignPatterns\More\Repository;
/**
* This class is situated between Entity layer (class Post) and access object layer (MemoryStorage).
*
* 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
*
* Repository also supports the objective of achieving a clean separation and one-way dependency
* between the domain and data mapping layers
*/
class PostRepository
{
/**
* @var MemoryStorage
*/
private $persistence;
public function __construct(MemoryStorage $persistence)
{
$this->persistence = $persistence;
}
public function findById(int $id): Post
{
$arrayData = $this->persistence->retrieve($id);
if (is_null($arrayData)) {
throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist', $id));
}
return Post::fromState($arrayData);
}
public function save(Post $post)
{
$id = $this->persistence->persist([
'text' => $post->getText(),
'title' => $post->getTitle(),
]);
$post->setId($id);
}
}