aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/ForumCategory.php
blob: 411146498267568323f5413c2527301ab5a24db0 (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
<?php

namespace JsonApi\Schemas;

use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
use JsonApi\Models\ForumEntry as Entry;

class ForumCategory extends SchemaProvider
{
    const TYPE = 'forum-categories';
    const REL_COURSE = 'course';
    const REL_ENTRY = 'entries';



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

    public function getAttributes($category, ContextInterface $context): iterable
    {
        return [
            'title' => $category->entry_name,
            'position' => (int) $category->pos,
        ];
    }

    public function getRelationships($category, ContextInterface $context): iterable
    {
        $isPrimary = $context->getPosition()->getLevel() === 0;
        $includeList = $context->getIncludePaths();

        $relationships = [];
        if ($isPrimary) {
            $relationships = $this->addCourseRelationship($category, $isPrimary, $includeList);
            $relationships = $this->addEntryRelationship($category, $isPrimary, $includeList);
        }

        return $relationships;
    }

    public function addCourseRelationship($category, $isPrimary, $includeList)
    {
        $data = $isPrimary && in_array(self::REL_COURSE, $includeList)
              ? \Course::find($category->seminar_id)
              : \Course::buildExisting(['id' => $category->seminar_id]);
        $link = $this->createLinkToResource($data);
        $relationships = [
            self::REL_COURSE => [
                self::RELATIONSHIP_LINKS => [Link::RELATED => $link],
                self::RELATIONSHIP_DATA => $data,
            ],
        ];

        return $relationships;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    private function addEntryRelationship($category, $isPrimary, $includeList)
    {
        $data = Entry::getEntriesFromCat($category);
        $link = $this->getRelationshipRelatedLink($category, self::REL_ENTRY);
        $relationships[self::REL_ENTRY] = [
            self::RELATIONSHIP_DATA => $data,
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $link,
            ],
        ];

        return $relationships;
    }
}