2016-10-21 14:43:11 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Examples\Blog\Data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DataSource
|
|
|
|
*
|
|
|
|
* This is just a simple in-memory data holder for the sake of example.
|
|
|
|
* Data layer for real app may use Doctrine or query the database directly (e.g. in CQRS style)
|
|
|
|
*
|
|
|
|
* @package GraphQL\Examples\Blog
|
|
|
|
*/
|
|
|
|
class DataSource
|
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
private static $users = [];
|
|
|
|
private static $stories = [];
|
|
|
|
private static $storyLikes = [];
|
|
|
|
private static $comments = [];
|
|
|
|
private static $storyComments = [];
|
|
|
|
private static $commentReplies = [];
|
|
|
|
private static $storyMentions = [];
|
|
|
|
|
|
|
|
public static function init()
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$users = [
|
2016-10-23 01:13:55 +03:00
|
|
|
'1' => new User([
|
|
|
|
'id' => '1',
|
2016-10-21 14:43:11 +03:00
|
|
|
'email' => 'john@example.com',
|
|
|
|
'firstName' => 'John',
|
|
|
|
'lastName' => 'Doe'
|
|
|
|
]),
|
2016-10-23 01:13:55 +03:00
|
|
|
'2' => new User([
|
|
|
|
'id' => '2',
|
2016-10-21 14:43:11 +03:00
|
|
|
'email' => 'jane@example.com',
|
|
|
|
'firstName' => 'Jane',
|
|
|
|
'lastName' => 'Doe'
|
|
|
|
]),
|
2016-10-23 01:13:55 +03:00
|
|
|
'3' => new User([
|
|
|
|
'id' => '3',
|
2016-10-21 14:43:11 +03:00
|
|
|
'email' => 'john@example.com',
|
|
|
|
'firstName' => 'John',
|
|
|
|
'lastName' => 'Doe'
|
|
|
|
]),
|
|
|
|
];
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$stories = [
|
2016-10-23 01:13:55 +03:00
|
|
|
'1' => new Story(['id' => '1', 'authorId' => '1', 'body' => '<h1>GraphQL is awesome!</h1>']),
|
|
|
|
'2' => new Story(['id' => '2', 'authorId' => '1', 'body' => '<a>Test this</a>']),
|
|
|
|
'3' => new Story(['id' => '3', 'authorId' => '3', 'body' => "This\n<br>story\n<br>spans\n<br>newlines"]),
|
2016-10-21 14:43:11 +03:00
|
|
|
];
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$storyLikes = [
|
2016-10-23 01:13:55 +03:00
|
|
|
'1' => ['1', '2', '3'],
|
|
|
|
'2' => [],
|
|
|
|
'3' => ['1']
|
|
|
|
];
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$comments = [
|
2016-10-23 01:13:55 +03:00
|
|
|
// thread #1:
|
|
|
|
'100' => new Comment(['id' => '100', 'authorId' => '3', 'storyId' => '1', 'body' => 'Likes']),
|
|
|
|
'110' => new Comment(['id' =>'110', 'authorId' =>'2', 'storyId' => '1', 'body' => 'Reply <b>#1</b>', 'parentId' => '100']),
|
|
|
|
'111' => new Comment(['id' => '111', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-1', 'parentId' => '110']),
|
|
|
|
'112' => new Comment(['id' => '112', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-2', 'parentId' => '110']),
|
|
|
|
'113' => new Comment(['id' => '113', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-3', 'parentId' => '110']),
|
|
|
|
'114' => new Comment(['id' => '114', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-4', 'parentId' => '110']),
|
|
|
|
'115' => new Comment(['id' => '115', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-5', 'parentId' => '110']),
|
|
|
|
'116' => new Comment(['id' => '116', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-6', 'parentId' => '110']),
|
|
|
|
'117' => new Comment(['id' => '117', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-7', 'parentId' => '110']),
|
|
|
|
'120' => new Comment(['id' => '120', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #2', 'parentId' => '100']),
|
|
|
|
'130' => new Comment(['id' => '130', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #3', 'parentId' => '100']),
|
|
|
|
'200' => new Comment(['id' => '200', 'authorId' => '2', 'storyId' => '1', 'body' => 'Me2']),
|
|
|
|
'300' => new Comment(['id' => '300', 'authorId' => '3', 'storyId' => '1', 'body' => 'U2']),
|
|
|
|
|
|
|
|
# thread #2:
|
|
|
|
'400' => new Comment(['id' => '400', 'authorId' => '2', 'storyId' => '2', 'body' => 'Me too']),
|
|
|
|
'500' => new Comment(['id' => '500', 'authorId' => '2', 'storyId' => '2', 'body' => 'Nice!']),
|
|
|
|
];
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$storyComments = [
|
2016-10-23 01:13:55 +03:00
|
|
|
'1' => ['100', '200', '300'],
|
|
|
|
'2' => ['400', '500']
|
|
|
|
];
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$commentReplies = [
|
2016-10-23 01:13:55 +03:00
|
|
|
'100' => ['110', '120', '130'],
|
|
|
|
'110' => ['111', '112', '113', '114', '115', '116', '117'],
|
|
|
|
];
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$storyMentions = [
|
2016-10-23 01:13:55 +03:00
|
|
|
'1' => [
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$users['2']
|
2016-10-23 01:13:55 +03:00
|
|
|
],
|
|
|
|
'2' => [
|
2016-11-06 17:33:13 +03:00
|
|
|
self::$stories['1'],
|
|
|
|
self::$users['3']
|
2016-10-23 01:13:55 +03:00
|
|
|
]
|
2016-10-21 14:43:11 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findUser($id)
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return isset(self::$users[$id]) ? self::$users[$id] : null;
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findStory($id)
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return isset(self::$stories[$id]) ? self::$stories[$id] : null;
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findComment($id)
|
2016-10-23 01:30:31 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return isset(self::$comments[$id]) ? self::$comments[$id] : null;
|
2016-10-23 01:30:31 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findLastStoryFor($authorId)
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
$storiesFound = array_filter(self::$stories, function(Story $story) use ($authorId) {
|
2016-10-21 14:43:11 +03:00
|
|
|
return $story->authorId == $authorId;
|
|
|
|
});
|
|
|
|
return !empty($storiesFound) ? $storiesFound[count($storiesFound) - 1] : null;
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findLikes($storyId, $limit)
|
|
|
|
{
|
|
|
|
$likes = isset(self::$storyLikes[$storyId]) ? self::$storyLikes[$storyId] : [];
|
|
|
|
$result = array_map(
|
|
|
|
function($userId) {
|
|
|
|
return self::$users[$userId];
|
|
|
|
},
|
|
|
|
$likes
|
|
|
|
);
|
|
|
|
return array_slice($result, 0, $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isLikedBy($storyId, $userId)
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
$subscribers = isset(self::$storyLikes[$storyId]) ? self::$storyLikes[$storyId] : [];
|
2016-10-23 01:13:55 +03:00
|
|
|
return in_array($userId, $subscribers);
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function getUserPhoto($userId, $size)
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
|
|
|
return new Image([
|
2016-10-23 01:13:55 +03:00
|
|
|
'id' => $userId,
|
2016-10-21 14:43:11 +03:00
|
|
|
'type' => Image::TYPE_USERPIC,
|
|
|
|
'size' => $size,
|
|
|
|
'width' => rand(100, 200),
|
|
|
|
'height' => rand(100, 200)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findLatestStory()
|
2016-10-21 14:43:11 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return array_pop(self::$stories);
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|
2016-10-23 01:13:55 +03:00
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findStories($limit, $afterId = null)
|
2016-10-23 01:13:55 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
$start = $afterId ? (int) array_search($afterId, array_keys(self::$stories)) + 1 : 0;
|
|
|
|
return array_slice(array_values(self::$stories), $start, $limit);
|
2016-10-23 01:13:55 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findComments($storyId, $limit = 5, $afterId = null)
|
2016-10-23 01:13:55 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
$storyComments = isset(self::$storyComments[$storyId]) ? self::$storyComments[$storyId] : [];
|
2016-10-23 01:13:55 +03:00
|
|
|
|
|
|
|
$start = isset($after) ? (int) array_search($afterId, $storyComments) + 1 : 0;
|
|
|
|
$storyComments = array_slice($storyComments, $start, $limit);
|
|
|
|
|
|
|
|
return array_map(
|
|
|
|
function($commentId) {
|
2016-11-06 17:33:13 +03:00
|
|
|
return self::$comments[$commentId];
|
2016-10-23 01:13:55 +03:00
|
|
|
},
|
|
|
|
$storyComments
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findReplies($commentId, $limit = 5, $afterId = null)
|
2016-10-23 01:13:55 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
$commentReplies = isset(self::$commentReplies[$commentId]) ? self::$commentReplies[$commentId] : [];
|
2016-10-23 01:13:55 +03:00
|
|
|
|
|
|
|
$start = isset($after) ? (int) array_search($afterId, $commentReplies) + 1: 0;
|
|
|
|
$commentReplies = array_slice($commentReplies, $start, $limit);
|
|
|
|
|
|
|
|
return array_map(
|
|
|
|
function($replyId) {
|
2016-11-06 17:33:13 +03:00
|
|
|
return self::$comments[$replyId];
|
2016-10-23 01:13:55 +03:00
|
|
|
},
|
|
|
|
$commentReplies
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function countComments($storyId)
|
2016-10-23 01:13:55 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return isset(self::$storyComments[$storyId]) ? count(self::$storyComments[$storyId]) : 0;
|
2016-10-23 01:13:55 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function countReplies($commentId)
|
2016-10-23 01:13:55 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return isset(self::$commentReplies[$commentId]) ? count(self::$commentReplies[$commentId]) : 0;
|
2016-10-23 01:13:55 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
public static function findStoryMentions($storyId)
|
2016-10-23 01:13:55 +03:00
|
|
|
{
|
2016-11-06 17:33:13 +03:00
|
|
|
return isset(self::$storyMentions[$storyId]) ? self::$storyMentions[$storyId] :[];
|
2016-10-23 01:13:55 +03:00
|
|
|
}
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|