aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/ForumEntry.php
blob: 10a6e218be16c5347664b15e4ea66a2c04821652 (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\Document\Link;
use JsonApi\Models\ForumCat;

class ForumEntry extends SchemaProvider
{
    const TYPE = 'forum-entries';
    const REL_CAT = 'category';
    const REL_ENTRY = 'entries';

    protected $resourceType = self::TYPE;

    public function getId($entry)
    {
        return $entry->topic_id;
    }

    public function getAttributes($entry)
    {
        return [
            'title' => $entry->name,
            'area' => (int) $entry->area,
            'content' => $entry->content,
        ];
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getRelationships($entry, $isPrimary, array $includeList)
    {
        $relationships = [];
        if ($isPrimary) {
            $relationships = $this->addCategoryRelationship($relationships, $entry, $includeList);
            $relationships = $this->addChildEntryRelationship($relationships, $entry, $includeList);
        }

        return $relationships;
    }

    private function addCategoryRelationship($relationships, $entry, $includeList)
    {
        $cat_link = new Link('/forum-categories/'.($entry->category)->id);
        $cat_data = in_array(self::REL_CAT, $includeList)
            ? ForumCat::find($entry->category->id)
            : ForumCat::buildExisting(['id' => $entry->category->id]);

        $relationships[self::REL_CAT] = [
            self::LINKS => [
                Link::RELATED => $cat_link,
            ],
            self::DATA => $cat_data,
        ];

        return $relationships;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    private function addChildEntryRelationship($relationships, $entry, $includeList)
    {
        $relationships[self::REL_ENTRY] = [
            self::DATA => $entry->getChildEntries($entry->id),

            self::LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($entry, self::REL_ENTRY),
            ],
        ];

        return $relationships;
    }
}