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
|
<?php
namespace JsonApi\Schemas\Forum;
use JsonApi\Schemas\SchemaProvider;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
class Posting extends SchemaProvider
{
const TYPE = 'forum-postings';
const REL_AUTHOR = 'author';
const REL_DISCUSSION = 'discussion';
const REL_POSTING = 'posting';
const REL_RANGE = 'range';
const REL_REACTIONS = 'reactions';
const REL_REACTIONS_USER = 'reactions.user';
const REL_OPENGRAPH_URLS = 'opengraph-urls';
/**
* @param \Forum\Posting $resource
*/
public function getId($resource): ?string
{
return $resource->posting_id;
}
/**
* @param \Forum\Posting $resource
*/
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'content' => \Studip\Markup::markupToHtml($resource->content),
'content-html' => formatReady($resource->content),
'anonymous' => (bool) $resource->anonymous,
'mkdate' => date('c', $resource->mkdate),
'chdate' => date('c', $resource->chdate)
];
}
/**
* @param \Forum\Posting $resource
*/
public function hasResourceMeta($resource): bool
{
return true;
}
/**
* @param \Forum\Posting $resource
*/
public function getResourceMeta($resource)
{
return [
self::REL_OPENGRAPH_URLS => array_map(fn($og) => [
'url' => $og['url'],
'is-opengraph' => (bool) $og['is_opengraph'],
'title' => $og['title'],
'description' => $og['description'],
'image' => $og['image'],
], $resource->getOpenGraphURLs())
];
}
/**
* @param \Forum\Posting $resource
*/
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$relationships = $this->addAuthorRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_AUTHOR));
$relationships = $this->addDiscussionRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DISCUSSION));
$relationships = $this->addPostingRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_POSTING));
$relationships = $this->addReactionsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_REACTIONS));
return $relationships;
}
private function addAuthorRelationship(array $relationships, \Forum\Posting $posting, bool $withAuthor = false)
{
$author = $posting->author;
if ($withAuthor && $author) {
$relationships[self::REL_AUTHOR] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($author)
],
self::RELATIONSHIP_DATA => $author
];
}
return $relationships;
}
private function addDiscussionRelationship(array $relationships, \Forum\Posting $posting, bool $withDiscussion = false)
{
if ($withDiscussion) {
$relationships[self::REL_DISCUSSION] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($posting->discussion)
],
self::RELATIONSHIP_DATA => $posting->discussion
];
}
return $relationships;
}
private function addPostingRelationship(array $relationships, \Forum\Posting $posting, bool $withPosting = false)
{
$posting = $posting->posting;
if ($withPosting && $posting) {
$relationships[self::REL_POSTING] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($posting)
],
self::RELATIONSHIP_DATA => $posting
];
}
return $relationships;
}
private function addReactionsRelationship(array $relationships, \Forum\Posting $posting, bool $withReactions = false)
{
if ($withReactions) {
$relationships[self::REL_REACTIONS] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($posting, self::REL_REACTIONS)
],
self::RELATIONSHIP_DATA => $posting->reactions
];
}
return $relationships;
}
}
|