-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBookRepository.php
165 lines (144 loc) · 4.07 KB
/
BookRepository.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace WoohooLabs\Yin\Examples\Book\Repository;
use WoohooLabs\Yin\Examples\Utils\AbstractRepository;
use WoohooLabs\Yin\Examples\Utils\Collection;
use function array_slice;
use function count;
class BookRepository extends AbstractRepository
{
/**
* @var array
*/
private static $authors = [
[
"id" => 100,
"name" => "Jez Humble",
],
[
"id" => 101,
"name" => "David Farley",
],
[
"id" => 102,
"name" => "Sam Newman",
],
[
"id" => 103,
"name" => "Jay Fields",
],
];
/**
* @var array
*/
private static $publishers = [
[
"id" => 12346,
"name" => "Addison-Wesley Professional",
"representative" => 10,
],
[
"id" => 12347,
"name" => "O'Reilly Media",
"representative" => 11,
],
[
"id" => 12348,
"name" => "CreateSpace Independent Publishing Platform",
"representative" => null,
],
];
/**
* @var array
*/
private static $representatives = [
[
"id" => 10,
"name" => "Melbourne Wesley Cummings",
"email" => "[email protected]",
],
[
"id" => 11,
"name" => "Tim O'Reilly",
"email" => "[email protected]",
],
];
/**
* @var array
*/
private static $books = [
[
"id" => 1,
"title" => "Building Microservices",
"isbn13" => "978-1491950357",
"release_date" => "2015-02-20",
"hard_cover" => false,
"pages" => 282,
"authors" => [102],
"publisher" => 12347,
],
[
"id" => 2,
"title" => "Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation",
"isbn13" => "978-0321601919",
"release_date" => "2010-08-06",
"hard_cover" => true,
"pages" => 512,
"authors" => [100, 101],
"publisher" => 12346,
],
[
"id" => 3,
"title" => "Working Effectively with Unit Tests",
"isbn13" => "978-1503242708",
"release_date" => "2014-12-09",
"hard_cover" => true,
"pages" => 354,
"authors" => [103],
"publisher" => 12348,
],
];
public static function getBooks(?int $page = null, ?int $size = null): Collection
{
if ($page === null) {
$page = 1;
}
if ($size === null) {
$size = 10;
}
$books = array_slice(self::$books, ($page - 1) * $size, $size);
foreach ($books as $key => &$book) {
$book = self::getBook($book["id"]);
}
return new Collection($books, count(self::$books), $page, $size);
}
public static function getBook(int $id): ?array
{
$book = self::getItemById($id, self::$books);
if ($book !== null) {
$book["authors"] = self::getItemsByIds($book["authors"], self::$authors);
$book["publisher"] = self::getItemById($book["publisher"], self::$publishers);
$book["publisher"]["representative"] = self::getItemById(
$book["publisher"]["representative"],
self::$representatives
);
}
return $book;
}
public static function getAuthorsOfBook(int $bookId): array
{
$book = self::getItemById($bookId, self::$books);
if ($book === null) {
return [];
}
return self::getItemsByIds($book["authors"], self::$authors);
}
public static function getAuthors(array $ids): array
{
return self::getItemsByIds($ids, self::$authors);
}
public static function getPublisher(int $id): array
{
return self::getItemById($id, self::$publishers);
}
}