removed/moved newly added patterns

This commit is contained in:
Dominik Liebler
2014-06-06 09:22:01 +02:00
parent c98bc8326f
commit c00be42790
19 changed files with 0 additions and 332 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace DesignPatterns\Repository;
/**
* Class MemoryStorage
* @package DesignPatterns\Repository
*/
class MemoryStorage implements Storage
{
private $data;
private $lastId;
public function __construct()
{
$this->data = array();
$this->lastId = 0;
}
/**
* {@inheritdoc}
*/
public function persist($data)
{
$this->data[++$this->lastId] = $data;
return $this->lastId;
}
/**
* {@inheritdoc}
*/
public function retrieve($id)
{
return isset($this->data[$id]) ? $this->data[$id] : null;
}
/**
* {@inheritdoc}
*/
public function delete($id)
{
if(!isset($this->data[$id])){
return false;
}
$this->data[$id] = null;
unset($this->data[$id]);
return true;
}
}

122
More/Repository/Post.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
namespace DesignPatterns\Repository;
/**
* Post represents entity for some post that user left on the site
*
* Class Post
* @package DesignPatterns\Repository
*/
class Post
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $text;
/**
* @var string
*/
private $author;
/**
* @var \DateTime
*/
private $created;
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param string $author
*/
public function setAuthor($author)
{
$this->author = $author;
}
/**
* @return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* @param \DateTime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* @param string $text
*/
public function setText($text)
{
$this->text = $text;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace DesignPatterns\Repository;
/**
* Repository for class Post
* This class is between Entity layer(class Post) and access object layer(interface Storage)
* 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
* @package DesignPatterns\Repository
*/
class PostRepository
{
private $persistence;
public function __construct(Storage $persistence)
{
$this->persistence = $persistence;
}
/**
* Returns Post object by specified id
*
* @param int $id
* @return Post|null
*/
public function getById($id)
{
$arrayData = $this->persistence->retrieve($id);
if(is_null($arrayData)){
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());
}
}

12
More/Repository/README.md Normal file
View File

@@ -0,0 +1,12 @@
# Repository
## Purpose
Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
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.
## Examples
* Doctrine 2 ORM: there is Repository that mediates between Entity and DBAL and contains methods to retrieve objects
* Laravel Framework

View File

@@ -0,0 +1,41 @@
<?php
namespace DesignPatterns\Repository;
/**
* Interface Storage
*
* This interface describes methods for accessing storage.
* Concrete realization could be whatever we want - in memory, relational database, NoSQL database and etc
*
* @package DesignPatterns\Repository
*/
interface Storage
{
/**
* Method to persist data
* Returns new id for just persisted data
*
* @param array() $data
* @return int
*/
public function persist($data);
/**
* Returns data by specified id.
* If there is no such data null is returned
*
* @param int $id
* @return array|null
*/
public function retrieve($id);
/**
* Delete data specified by id
* If there is no such data - false returns, if data has been successfully deleted - true returns
*
* @param int $id
* @return bool
*/
public function delete($id);
}