aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Forum/Topic.php
blob: 22a8c8376c5db21652b8cbfce49f93628b264b8d (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
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
<?php

namespace JsonApi\Schemas\Forum;

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

class Topic extends SchemaProvider
{
    const TYPE = 'forum-topics';
    const REL_CATEGORY = 'category';
    const REL_DISCUSSION = 'discussion';

    /**
     * @param \Forum\Topic $resource
     */
    public function getId($resource): ?string
    {
        return $resource->topic_id;
    }

    /**
     * @param \Forum\Topic $resource
     */
    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'name' => $resource->name,
            'description' => $resource->description,
            'position' => (int) $resource->position,
            'mkdate' => date('c', $resource->mkdate),
            'chdate' => date('c', $resource->chdate)
        ];
     }

    /**
     * @param \Forum\Topic $resource
     */
    public function hasResourceMeta($resource): bool
    {
        return true;
    }

    /**
     * @param \Forum\Topic $resource
     */
    public function getResourceMeta($resource)
    {
        $metaData = $resource->getMetaData();

        return [
            'discussions-count' => (int) $metaData['discussions_count'],
            'postings-count' => (int) $metaData['postings_count'],
            'user-read-index' => (int) $metaData['user_read_index'],
            'users-count' => (int) $metaData['users_count'],
            'recent-activity' => $metaData['recent_activity'] ? date('c', $metaData['recent_activity']) : '',
        ];
    }

    /**
     * @param \Forum\Topic $resource
     */
    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];
        $relationships = $this->addCategoryRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_CATEGORY));
        $relationships = $this->addDiscussionsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DISCUSSION));

        return $relationships;
    }

    private function addCategoryRelationship(array $relationships, \Forum\Topic $topic, bool $withCategory = false)
    {
        if ($withCategory) {
            $relationships[self::REL_CATEGORY] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->getRelationshipRelatedLink($topic, self::REL_CATEGORY)
                ],
                self::RELATIONSHIP_DATA => $topic->category
            ];
        }

        return $relationships;
    }

    private function addDiscussionsRelationship(array $relationships, \Forum\Topic $topic, bool $withDiscussions = false)
    {
        if ($withDiscussions) {
            $relationships[self::REL_DISCUSSION] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->getRelationshipRelatedLink($topic, self::REL_DISCUSSION)
                ],
                self::RELATIONSHIP_DATA => $topic->dicussions
            ];
        }

        return $relationships;
    }
}