Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4aedcff

Browse files
author
Dominik Liebler
committed
Merge pull requestDesignPatternsPHP#71 from andrewnester/master
Repository pattern
2 parentsabd2b8e +19ff5e6 commit4aedcff

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+
namespaceDesignPatterns\Repository;
4+
5+
/**
6+
* Class MemoryStorage
7+
* @package DesignPatterns\Repository
8+
*/
9+
class MemoryStorageimplements Storage
10+
{
11+
12+
private$data;
13+
private$lastId;
14+
15+
publicfunction__construct()
16+
{
17+
$this->data =array();
18+
$this->lastId =0;
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
publicfunctionpersist($data)
25+
{
26+
$this->data[++$this->lastId] =$data;
27+
return$this->lastId;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
publicfunctionretrieve($id)
34+
{
35+
returnisset($this->data[$id]) ?$this->data[$id] :null;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
publicfunctiondelete($id)
42+
{
43+
if(!isset($this->data[$id])){
44+
returnfalse;
45+
}
46+
47+
$this->data[$id] =null;
48+
unset($this->data[$id]);
49+
50+
returntrue;
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+
namespaceDesignPatterns\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+
publicfunctionsetId($id)
44+
{
45+
$this->id =$id;
46+
}
47+
48+
/**
49+
* @return int
50+
*/
51+
publicfunctiongetId()
52+
{
53+
return$this->id;
54+
}
55+
56+
/**
57+
* @param string $author
58+
*/
59+
publicfunctionsetAuthor($author)
60+
{
61+
$this->author =$author;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
publicfunctiongetAuthor()
68+
{
69+
return$this->author;
70+
}
71+
72+
/**
73+
* @param \DateTime $created
74+
*/
75+
publicfunctionsetCreated($created)
76+
{
77+
$this->created =$created;
78+
}
79+
80+
/**
81+
* @return \DateTime
82+
*/
83+
publicfunctiongetCreated()
84+
{
85+
return$this->created;
86+
}
87+
88+
/**
89+
* @param string $text
90+
*/
91+
publicfunctionsetText($text)
92+
{
93+
$this->text =$text;
94+
}
95+
96+
/**
97+
* @return string
98+
*/
99+
publicfunctiongetText()
100+
{
101+
return$this->text;
102+
}
103+
104+
/**
105+
* @param string $title
106+
*/
107+
publicfunctionsetTitle($title)
108+
{
109+
$this->title =$title;
110+
}
111+
112+
/**
113+
* @return string
114+
*/
115+
publicfunctiongetTitle()
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+
namespaceDesignPatterns\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+
publicfunction__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+
publicfunctiongetById($id)
30+
{
31+
$arrayData =$this->persistence->retrieve($id);
32+
if(is_null($arrayData)){
33+
returnnull;
34+
}
35+
36+
$post =newPost();
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+
publicfunctionsave(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+
publicfunctiondelete(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+
namespaceDesignPatterns\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+
publicfunctionpersist($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+
publicfunctionretrieve($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+
publicfunctiondelete($id);
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp