This repository was archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.php
More file actions
70 lines (63 loc) · 1.46 KB
/
Repository.php
File metadata and controls
70 lines (63 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Kuriv\PHPDesignPatterns\More\Repository;
use OutOfBoundsException;
class Repository
{
/**
* Store the action instance.
*
* @var Action
*/
private Action $action;
/**
* Store the action instance to the current instance.
*
* @param Action $action
* @return void
*/
public function __construct(Action $action)
{
$this->action = $action;
}
/**
* Generate an ID instance.
*
* @param void
* @return PostID
*/
public function generateID(): PostID
{
return PostID::getInstance($this->action->generateID());
}
/**
* Get the specified instance.
*
* @param PostID $id
* @return Post
*/
public function find(PostID $id): Post
{
try {
$id = $id->getID();
$result = $this->action->find($id);
} catch (OutOfBoundsException $e) {
throw new OutOfBoundsException(sprintf('Post with ID %d does not exist', $id), 0, $e);
}
return Post::getInstance($result);
}
/**
* Save the data.
*
* @param Post $post
* @return void
*/
public function save(Post $post)
{
$this->action->save([
'id' => $post->getID()->getID(),
'title' => $post->getTitle(),
'content' => $post->getContent(),
'status_id' => $post->getStatus()->getStatusID()
]);
}
}