From a0c2a7d6387c834e42ed73d0260e8d1264e5ed70 Mon Sep 17 00:00:00 2001 From: andrewnester Date: Wed, 12 Mar 2014 13:11:49 +0300 Subject: [PATCH 1/2] Repository pattern --- Repository/IStorage.php | 41 ++++++++++++ Repository/MemoryStorage.php | 54 +++++++++++++++ Repository/Post.php | 122 ++++++++++++++++++++++++++++++++++ Repository/PostRepository.php | 75 +++++++++++++++++++++ Repository/README.md | 12 ++++ 5 files changed, 304 insertions(+) create mode 100644 Repository/IStorage.php create mode 100644 Repository/MemoryStorage.php create mode 100644 Repository/Post.php create mode 100644 Repository/PostRepository.php create mode 100644 Repository/README.md diff --git a/Repository/IStorage.php b/Repository/IStorage.php new file mode 100644 index 0000000..2da3773 --- /dev/null +++ b/Repository/IStorage.php @@ -0,0 +1,41 @@ +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; + } + +} + diff --git a/Repository/Post.php b/Repository/Post.php new file mode 100644 index 0000000..5308bb0 --- /dev/null +++ b/Repository/Post.php @@ -0,0 +1,122 @@ +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; + } + + + +} \ No newline at end of file diff --git a/Repository/PostRepository.php b/Repository/PostRepository.php new file mode 100644 index 0000000..d63288c --- /dev/null +++ b/Repository/PostRepository.php @@ -0,0 +1,75 @@ +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()); + } +} \ No newline at end of file diff --git a/Repository/README.md b/Repository/README.md new file mode 100644 index 0000000..1c2216f --- /dev/null +++ b/Repository/README.md @@ -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 From 19ff5e6241f13b2abe9c305a7538ff44b484fc90 Mon Sep 17 00:00:00 2001 From: andrewnester Date: Tue, 22 Apr 2014 15:15:52 +0300 Subject: [PATCH 2/2] Repository pattern - some code style changes --- Repository/MemoryStorage.php | 2 +- Repository/PostRepository.php | 4 ++-- Repository/{IStorage.php => Storage.php} | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename Repository/{IStorage.php => Storage.php} (95%) diff --git a/Repository/MemoryStorage.php b/Repository/MemoryStorage.php index 16324a1..fdc6f97 100644 --- a/Repository/MemoryStorage.php +++ b/Repository/MemoryStorage.php @@ -6,7 +6,7 @@ namespace DesignPatterns\Repository; * Class MemoryStorage * @package DesignPatterns\Repository */ -class MemoryStorage implements IStorage +class MemoryStorage implements Storage { private $data; diff --git a/Repository/PostRepository.php b/Repository/PostRepository.php index d63288c..e6c880b 100644 --- a/Repository/PostRepository.php +++ b/Repository/PostRepository.php @@ -4,7 +4,7 @@ namespace DesignPatterns\Repository; /** * Repository for class Post - * This class is between Entity layer(class Post) and access object layer(interface IStorage) + * 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 * @@ -15,7 +15,7 @@ class PostRepository { private $persistence; - public function __construct(IStorage $persistence) + public function __construct(Storage $persistence) { $this->persistence = $persistence; } diff --git a/Repository/IStorage.php b/Repository/Storage.php similarity index 95% rename from Repository/IStorage.php rename to Repository/Storage.php index 2da3773..7ed8589 100644 --- a/Repository/IStorage.php +++ b/Repository/Storage.php @@ -3,14 +3,14 @@ namespace DesignPatterns\Repository; /** - * Interface IStorage + * 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 IStorage +interface Storage { /** * Method to persist data