aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Forum/ForumPostingReaction.php
blob: 1b7bfa9b21308cdee89bbb905d85e7e617160233 (plain)
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
<?php

namespace JsonApi\Schemas\Forum;

use JsonApi\Schemas\SchemaProvider;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;

class ForumPostingReaction extends SchemaProvider
{
    const TYPE = 'forum-posting-reactions';
    const REL_POSTING = 'posting';
    const REL_USER = 'user';

    public function getId($postingReaction): ?string
    {
        return $postingReaction->id;
    }

    public function getAttributes($postingReaction, ContextInterface $context): iterable
    {
        return [
            'emoji' => $postingReaction->emoji,
            'mkdate' => date('c', $postingReaction->mkdate),
            'chdate' => date('c', $postingReaction->chdate)
        ];
    }


    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getRelationships($postingReaction, ContextInterface $context): iterable
    {
        $relationships = [];

        $relationships = $this->addPostingRelationship($relationships, $postingReaction, $this->shouldInclude($context, self::REL_POSTING));
        $relationships = $this->addUserRelationship($relationships, $postingReaction, $this->shouldInclude($context, self::REL_USER));

        return $relationships;
    }


    private function addPostingRelationship(array $relationships, $postingReaction, bool $withPosting = false)
    {
        if ($withPosting) {
            $relationships[self::REL_POSTING] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($postingReaction->posting)
                ],
                self::RELATIONSHIP_DATA => $postingReaction->posting
            ];
        }

        return $relationships;
    }

    private function addUserRelationship(array $relationships, $discussion, bool $withUser = false)
    {
        if ($withUser) {
            $relationships[self::REL_USER] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($discussion->user)
                ],
                self::RELATIONSHIP_DATA => $discussion->user
            ];
        }

        return $relationships;
    }
}