aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Forum/Category.php
blob: 08359374739966d7ebbdd3c6b270246d1fb65249 (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
<?php
namespace JsonApi\Schemas\Forum;

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

class Category extends SchemaProvider
{
    const TYPE = 'forum-categories';
    const REL_TOPICS = 'topics';

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

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

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

    /**
     * @param \Forum\Category $resource
     */
    public function getResourceMeta($resource)
    {
        $metadata = $resource->getMetadata();

        return [
            'topics-count' => (int) $metadata['topics_count'],
            'discussions-count' => (int) $metadata['discussions_count'],
            'postings-count' => (int) $metadata['postings_count'],
            'unread-postings-count' => (int) $metadata['unread_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\Category $resource
     */
    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];
        $relationships = $this->addTopicsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_TOPICS));

        return $relationships;
    }

    private function addTopicsRelationship(array $relationships, \Forum\Category $category, $withTopics = false)
    {
        if ($withTopics) {
            $relationships[self::REL_TOPICS] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->getRelationshipRelatedLink($category, self::REL_TOPICS)
                ],
                self::RELATIONSHIP_DATA => $category->topics
            ];
        }

        return $relationships;
    }
}