Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4aedcff

Browse files
author
Dominik Liebler
committed
Merge pull request DesignPatternsPHP#71 from andrewnester/master
Repository pattern
2 parents abd2b8e + 19ff5e6 commit 4aedcff

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-0
lines changed

Repository/MemoryStorage.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace DesignPatterns\Repository;
4+
5+
/**
6+
* Class MemoryStorage
7+
* @package DesignPatterns\Repository
8+
*/
9+
class MemoryStorage implements Storage
10+
{
11+
12+
private $data;
13+
private $lastId;
14+
15+
public function __construct()
16+
{
17+
$this->data = array();
18+
$this->lastId = 0;
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function persist($data)
25+
{
26+
$this->data[++$this->lastId] = $data;
27+
return $this->lastId;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function retrieve($id)
34+
{
35+
return isset($this->data[$id]) ? $this->data[$id] : null;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function delete($id)
42+
{
43+
if(!isset($this->data[$id])){
44+
return false;
45+
}
46+
47+
$this->data[$id] = null;
48+
unset($this->data[$id]);
49+
50+
return true;
51+
}
52+
53+
}
54+

Repository/Post.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace DesignPatterns\Repository;
4+
/**
5+
* Post represents entity for some post that user left on the site
6+
*
7+
* Class Post
8+
* @package DesignPatterns\Repository
9+
*/
10+
class Post
11+
{
12+
/**
13+
* @var int
14+
*/
15+
private $id;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $title;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $text;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $author;
31+
32+
/**
33+
* @var \DateTime
34+
*/
35+
private $created;
36+
37+
38+
39+
40+
/**
41+
* @param int $id
42+
*/
43+
public function setId($id)
44+
{
45+
$this->id = $id;
46+
}
47+
48+
/**
49+
* @return int
50+
*/
51+
public function getId()
52+
{
53+
return $this->id;
54+
}
55+
56+
/**
57+
* @param string $author
58+
*/
59+
public function setAuthor($author)
60+
{
61+
$this->author = $author;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getAuthor()
68+
{
69+
return $this->author;
70+
}
71+
72+
/**
73+
* @param \DateTime $created
74+
*/
75+
public function setCreated($created)
76+
{
77+
$this->created = $created;
78+
}
79+
80+
/**
81+
* @return \DateTime
82+
*/
83+
public function getCreated()
84+
{
85+
return $this->created;
86+
}
87+
88+
/**
89+
* @param string $text
90+
*/
91+
public function setText($text)
92+
{
93+
$this->text = $text;
94+
}
95+
96+
/**
97+
* @return string
98+
*/
99+
public function getText()
100+
{
101+
return $this->text;
102+
}
103+
104+
/**
105+
* @param string $title
106+
*/
107+
public function setTitle($title)
108+
{
109+
$this->title = $title;
110+
}
111+
112+
/**
113+
* @return string
114+
*/
115+
public function getTitle()
116+
{
117+
return $this->title;
118+
}
119+
120+
121+
122+
}

Repository/PostRepository.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace DesignPatterns\Repository;
4+
5+
/**
6+
* Repository for class Post
7+
* This class is between Entity layer(class Post) and access object layer(interface Storage)
8+
* 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
9+
* Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers
10+
*
11+
* Class PostRepository
12+
* @package DesignPatterns\Repository
13+
*/
14+
class PostRepository
15+
{
16+
private $persistence;
17+
18+
public function __construct(Storage $persistence)
19+
{
20+
$this->persistence = $persistence;
21+
}
22+
23+
/**
24+
* Returns Post object by specified id
25+
*
26+
* @param int $id
27+
* @return Post|null
28+
*/
29+
public function getById($id)
30+
{
31+
$arrayData = $this->persistence->retrieve($id);
32+
if(is_null($arrayData)){
33+
return null;
34+
}
35+
36+
$post = new Post();
37+
$post->setId($arrayData['id']);
38+
$post->setAuthor($arrayData['author']);
39+
$post->setCreated($arrayData['created']);
40+
$post->setText($arrayData['text']);
41+
$post->setTitle($arrayData['title']);
42+
43+
return $post;
44+
}
45+
46+
/**
47+
* Save post object and populate it with id
48+
*
49+
* @param Post $post
50+
* @return Post
51+
*/
52+
public function save(Post $post)
53+
{
54+
$id = $this->persistence->persist(array(
55+
'author' => $post->getAuthor(),
56+
'created' => $post->getCreated(),
57+
'text' => $post->getText(),
58+
'title' => $post->getTitle()
59+
));
60+
61+
$post->setId($id);
62+
return $post;
63+
}
64+
65+
/**
66+
* Deletes specified Post object
67+
*
68+
* @param Post $post
69+
* @return bool
70+
*/
71+
public function delete(Post $post)
72+
{
73+
return $this->persistence->delete($post->getId());
74+
}
75+
}

Repository/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Repository
2+
3+
## Purpose
4+
5+
Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
6+
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.
7+
Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.
8+
9+
## Examples
10+
11+
* Doctrine 2 ORM: there is Repository that mediates between Entity and DBAL and contains methods to retrieve objects
12+
* Laravel Framework

Repository/Storage.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace DesignPatterns\Repository;
4+
5+
/**
6+
* Interface Storage
7+
*
8+
* This interface describes methods for accessing storage.
9+
* Concrete realization could be whatever we want - in memory, relational database, NoSQL database and etc
10+
*
11+
* @package DesignPatterns\Repository
12+
*/
13+
interface Storage
14+
{
15+
/**
16+
* Method to persist data
17+
* Returns new id for just persisted data
18+
*
19+
* @param array() $data
20+
* @return int
21+
*/
22+
public function persist($data);
23+
24+
/**
25+
* Returns data by specified id.
26+
* If there is no such data null is returned
27+
*
28+
* @param int $id
29+
* @return array|null
30+
*/
31+
public function retrieve($id);
32+
33+
/**
34+
* Delete data specified by id
35+
* If there is no such data - false returns, if data has been successfully deleted - true returns
36+
*
37+
* @param int $id
38+
* @return bool
39+
*/
40+
public function delete($id);
41+
}

0 commit comments

Comments
 (0)