DesignPatternsPHP/More/Repository/PostRepository.php

80 lines
1.9 KiB
PHP
Raw Normal View History

2014-03-12 13:11:49 +03:00
<?php
namespace DesignPatterns\Repository;
/**
* Repository for class Post
* This class is between Entity layer(class Post) and access object layer(interface Storage)
2014-11-05 10:24:19 +01:00
*
* 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
2014-03-12 13:11:49 +03:00
*
* Class PostRepository
* @package DesignPatterns\Repository
*/
class PostRepository
{
private $persistence;
public function __construct(Storage $persistence)
2014-03-12 13:11:49 +03:00
{
$this->persistence = $persistence;
}
/**
* Returns Post object by specified id
*
* @param int $id
* @return Post|null
*/
public function getById($id)
{
$arrayData = $this->persistence->retrieve($id);
2014-11-05 10:24:19 +01:00
if (is_null($arrayData)) {
2014-03-12 13:11:49 +03:00
return null;
}
$post = new Post();
$post->setId($arrayData['id']);
$post->setAuthor($arrayData['author']);
$post->setCreated($arrayData['created']);
$post->setText($arrayData['text']);
$post->setTitle($arrayData['title']);
return $post;
}
/**
* Save post object and populate it with id
*
* @param Post $post
* @return Post
*/
public function save(Post $post)
{
$id = $this->persistence->persist(array(
'author' => $post->getAuthor(),
'created' => $post->getCreated(),
'text' => $post->getText(),
'title' => $post->getTitle()
));
$post->setId($id);
return $post;
}
/**
* Deletes specified Post object
*
* @param Post $post
* @return bool
*/
public function delete(Post $post)
{
return $this->persistence->delete($post->getId());
}
2014-11-05 10:24:19 +01:00
}