|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Examples\Blog\Data; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This is just a simple in-memory data holder for the sake of example. |
|
6
|
|
|
* Data layer for real app may use Doctrine or query the database directly (e.g. in CQRS style) |
|
7
|
|
|
*/ |
|
8
|
|
|
class DataSource |
|
9
|
|
|
{ |
|
10
|
|
|
private static $users = []; |
|
11
|
|
|
private static $stories = []; |
|
12
|
|
|
private static $storyLikes = []; |
|
13
|
|
|
private static $comments = []; |
|
14
|
|
|
private static $storyComments = []; |
|
15
|
|
|
private static $commentReplies = []; |
|
16
|
|
|
private static $storyMentions = []; |
|
17
|
|
|
|
|
18
|
|
|
public static function init() |
|
19
|
|
|
{ |
|
20
|
|
|
self::$users = [ |
|
21
|
|
|
'1' => new User([ |
|
22
|
|
|
'id' => '1', |
|
23
|
|
|
'email' => '[email protected]', |
|
24
|
|
|
'firstName' => 'John', |
|
25
|
|
|
'lastName' => 'Doe' |
|
26
|
|
|
]), |
|
27
|
|
|
'2' => new User([ |
|
28
|
|
|
'id' => '2', |
|
29
|
|
|
'email' => '[email protected]', |
|
30
|
|
|
'firstName' => 'Jane', |
|
31
|
|
|
'lastName' => 'Doe' |
|
32
|
|
|
]), |
|
33
|
|
|
'3' => new User([ |
|
34
|
|
|
'id' => '3', |
|
35
|
|
|
'email' => '[email protected]', |
|
36
|
|
|
'firstName' => 'John', |
|
37
|
|
|
'lastName' => 'Doe' |
|
38
|
|
|
]), |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
self::$stories = [ |
|
42
|
|
|
'1' => new Story(['id' => '1', 'authorId' => '1', 'body' => '<h1>GraphQL is awesome!</h1>']), |
|
43
|
|
|
'2' => new Story(['id' => '2', 'authorId' => '1', 'body' => '<a>Test this</a>']), |
|
44
|
|
|
'3' => new Story(['id' => '3', 'authorId' => '3', 'body' => "This\n<br>story\n<br>spans\n<br>newlines"]), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
self::$storyLikes = [ |
|
48
|
|
|
'1' => ['1', '2', '3'], |
|
49
|
|
|
'2' => [], |
|
50
|
|
|
'3' => ['1'] |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
self::$comments = [ |
|
54
|
|
|
// thread #1: |
|
55
|
|
|
'100' => new Comment(['id' => '100', 'authorId' => '3', 'storyId' => '1', 'body' => 'Likes']), |
|
56
|
|
|
'110' => new Comment(['id' =>'110', 'authorId' =>'2', 'storyId' => '1', 'body' => 'Reply <b>#1</b>', 'parentId' => '100']), |
|
57
|
|
|
'111' => new Comment(['id' => '111', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-1', 'parentId' => '110']), |
|
58
|
|
|
'112' => new Comment(['id' => '112', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-2', 'parentId' => '110']), |
|
59
|
|
|
'113' => new Comment(['id' => '113', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-3', 'parentId' => '110']), |
|
60
|
|
|
'114' => new Comment(['id' => '114', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-4', 'parentId' => '110']), |
|
61
|
|
|
'115' => new Comment(['id' => '115', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-5', 'parentId' => '110']), |
|
62
|
|
|
'116' => new Comment(['id' => '116', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-6', 'parentId' => '110']), |
|
63
|
|
|
'117' => new Comment(['id' => '117', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-7', 'parentId' => '110']), |
|
64
|
|
|
'120' => new Comment(['id' => '120', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #2', 'parentId' => '100']), |
|
65
|
|
|
'130' => new Comment(['id' => '130', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #3', 'parentId' => '100']), |
|
66
|
|
|
'200' => new Comment(['id' => '200', 'authorId' => '2', 'storyId' => '1', 'body' => 'Me2']), |
|
67
|
|
|
'300' => new Comment(['id' => '300', 'authorId' => '3', 'storyId' => '1', 'body' => 'U2']), |
|
68
|
|
|
|
|
69
|
|
|
# thread #2: |
|
70
|
|
|
'400' => new Comment(['id' => '400', 'authorId' => '2', 'storyId' => '2', 'body' => 'Me too']), |
|
71
|
|
|
'500' => new Comment(['id' => '500', 'authorId' => '2', 'storyId' => '2', 'body' => 'Nice!']), |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
self::$storyComments = [ |
|
75
|
|
|
'1' => ['100', '200', '300'], |
|
76
|
|
|
'2' => ['400', '500'] |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
self::$commentReplies = [ |
|
80
|
|
|
'100' => ['110', '120', '130'], |
|
81
|
|
|
'110' => ['111', '112', '113', '114', '115', '116', '117'], |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
self::$storyMentions = [ |
|
85
|
|
|
'1' => [ |
|
86
|
|
|
self::$users['2'] |
|
87
|
|
|
], |
|
88
|
|
|
'2' => [ |
|
89
|
|
|
self::$stories['1'], |
|
90
|
|
|
self::$users['3'] |
|
91
|
|
|
] |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public static function findUser($id) |
|
96
|
|
|
{ |
|
97
|
|
|
return isset(self::$users[$id]) ? self::$users[$id] : null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public static function findStory($id) |
|
101
|
|
|
{ |
|
102
|
|
|
return isset(self::$stories[$id]) ? self::$stories[$id] : null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function findComment($id) |
|
106
|
|
|
{ |
|
107
|
|
|
return isset(self::$comments[$id]) ? self::$comments[$id] : null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static function findLastStoryFor($authorId) |
|
111
|
|
|
{ |
|
112
|
|
|
$storiesFound = array_filter(self::$stories, function(Story $story) use ($authorId) { |
|
113
|
|
|
return $story->authorId == $authorId; |
|
114
|
|
|
}); |
|
115
|
|
|
return !empty($storiesFound) ? $storiesFound[count($storiesFound) - 1] : null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public static function findLikes($storyId, $limit) |
|
119
|
|
|
{ |
|
120
|
|
|
$likes = isset(self::$storyLikes[$storyId]) ? self::$storyLikes[$storyId] : []; |
|
121
|
|
|
$result = array_map( |
|
122
|
|
|
function($userId) { |
|
123
|
|
|
return self::$users[$userId]; |
|
124
|
|
|
}, |
|
125
|
|
|
$likes |
|
126
|
|
|
); |
|
127
|
|
|
return array_slice($result, 0, $limit); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public static function isLikedBy($storyId, $userId) |
|
131
|
|
|
{ |
|
132
|
|
|
$subscribers = isset(self::$storyLikes[$storyId]) ? self::$storyLikes[$storyId] : []; |
|
133
|
|
|
return in_array($userId, $subscribers); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public static function getUserPhoto($userId, $size) |
|
137
|
|
|
{ |
|
138
|
|
|
return new Image([ |
|
139
|
|
|
'id' => $userId, |
|
140
|
|
|
'type' => Image::TYPE_USERPIC, |
|
141
|
|
|
'size' => $size, |
|
142
|
|
|
'width' => rand(100, 200), |
|
143
|
|
|
'height' => rand(100, 200) |
|
144
|
|
|
]); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public static function findLatestStory() |
|
148
|
|
|
{ |
|
149
|
|
|
return array_pop(self::$stories); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public static function findStories($limit, $afterId = null) |
|
153
|
|
|
{ |
|
154
|
|
|
$start = $afterId ? (int) array_search($afterId, array_keys(self::$stories)) + 1 : 0; |
|
155
|
|
|
return array_slice(array_values(self::$stories), $start, $limit); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public static function findComments($storyId, $limit = 5, $afterId = null) |
|
159
|
|
|
{ |
|
160
|
|
|
$storyComments = isset(self::$storyComments[$storyId]) ? self::$storyComments[$storyId] : []; |
|
161
|
|
|
|
|
162
|
|
|
$start = isset($after) ? (int) array_search($afterId, $storyComments) + 1 : 0; |
|
|
|
|
|
|
163
|
|
|
$storyComments = array_slice($storyComments, $start, $limit); |
|
164
|
|
|
|
|
165
|
|
|
return array_map( |
|
166
|
|
|
function($commentId) { |
|
167
|
|
|
return self::$comments[$commentId]; |
|
168
|
|
|
}, |
|
169
|
|
|
$storyComments |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public static function findReplies($commentId, $limit = 5, $afterId = null) |
|
174
|
|
|
{ |
|
175
|
|
|
$commentReplies = isset(self::$commentReplies[$commentId]) ? self::$commentReplies[$commentId] : []; |
|
176
|
|
|
|
|
177
|
|
|
$start = isset($after) ? (int) array_search($afterId, $commentReplies) + 1: 0; |
|
|
|
|
|
|
178
|
|
|
$commentReplies = array_slice($commentReplies, $start, $limit); |
|
179
|
|
|
|
|
180
|
|
|
return array_map( |
|
181
|
|
|
function($replyId) { |
|
182
|
|
|
return self::$comments[$replyId]; |
|
183
|
|
|
}, |
|
184
|
|
|
$commentReplies |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public static function countComments($storyId) |
|
189
|
|
|
{ |
|
190
|
|
|
return isset(self::$storyComments[$storyId]) ? count(self::$storyComments[$storyId]) : 0; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public static function countReplies($commentId) |
|
194
|
|
|
{ |
|
195
|
|
|
return isset(self::$commentReplies[$commentId]) ? count(self::$commentReplies[$commentId]) : 0; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public static function findStoryMentions($storyId) |
|
199
|
|
|
{ |
|
200
|
|
|
return isset(self::$storyMentions[$storyId]) ? self::$storyMentions[$storyId] :[]; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|