aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/WikiPage.php
blob: d68390fa256f236e07b2bd036220a15a1da6ec0a (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

namespace JsonApi\Schemas;

use Neomerx\JsonApi\Document\Link;

class WikiPage extends SchemaProvider
{
    const REGEXP_KEYWORD = '/([\w\.\-\:\(\)_ยง\/@# ]|&[AOUaou]uml;|&szlig;)+/A';

    const TYPE = 'wiki-pages';
    const REL_AUTHOR = 'author';
    const REL_CHILDREN = 'children';
    const REL_DESCENDANTS = 'descendants';
    const REL_PARENT = 'parent';
    const REL_RANGE = 'range';

    protected $resourceType = self::TYPE;

    /**
     * {@inheritdoc}
     */
    public function getResourceLinks($resource)
    {
        $url = $this->getDiContainer()->get('router')->pathFor(
            'get-wiki-page',
            ['id' => sprintf("%s_%s", $resource->range_id, $resource->keyword)]
        );
        $links = [ Link::SELF => $this->createLink($url) ];

        return $links;
    }

    public static function getRangeClasses()
    {
        return [
            'sem' => \Course::class,
            'inst' => \Institute::class,
        ];
    }

    public static function getRangeTypes()
    {
        return [
            'sem' => Course::TYPE,
            'inst' => Institute::TYPE,
        ];
    }

    public static function getRangeClass($resource)
    {
        $classes = self::getRangeClasses();

        return $classes[
            get_object_type(
                $resource->range_id,
                array_keys($classes)
            )
        ];
    }

    public static function getRangeType($resource)
    {
        $types = self::getRangeTypes();

        return $types[
            get_object_type(
                $resource->range_id,
                array_keys($types)
            )
        ];
    }

    public function getId($wiki)
    {
        return sprintf(
            '%s_%s',
            $wiki->range_id,
            $wiki->keyword
        );
    }

    public function getAttributes($wiki)
    {
        return [
            'keyword' => $wiki->keyword,
            'content' => $wiki->body,
            'chdate' => date('c', $wiki->chdate),
            'version' => (int) $wiki->version,
        ];
    }

    public function getRelationships($wiki, $isPrimary, array $includeList)
    {
        $relationships = [];

        if ($isPrimary) {
            $relationships = $this->addAuthorRelationship($relationships, $wiki, $includeList);
            $relationships = $this->addChildrenRelationship($relationships, $wiki, $includeList);
            $relationships = $this->addDescendantsRelationship($relationships, $wiki, $includeList);
            $relationships = $this->addParentRelationship($relationships, $wiki, $includeList);
            $relationships = $this->addRangeRelationship($relationships, $wiki, $includeList);
        }

        return $relationships;
    }

    private function addParentRelationship($relationships, $wiki, $includeList)
    {
        $related = $wiki->parent;
        $relationships[self::REL_PARENT] = [
            self::SHOW_SELF => true,
            self::DATA => $related
        ];
        if ($related) {
            $relationships[self::REL_PARENT][self::LINKS] = [
                Link::RELATED => $this->getSchemaContainer()
                    ->getSchema($related)
                    ->getSelfSubLink($related)
            ];
        }

        return $relationships;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    private function addChildrenRelationship($relationships, $wiki, $includeList)
    {
        $relationships[self::REL_CHILDREN] = [
            self::LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($wiki, self::REL_CHILDREN),
            ],
        ];

        return $relationships;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    private function addDescendantsRelationship($relationships, $wiki, $includeList)
    {
        $relationships[self::REL_DESCENDANTS] = [
            self::LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($wiki, self::REL_DESCENDANTS),
            ],
        ];

        return $relationships;
    }

    private function addAuthorRelationship($relationships, $wiki, $includeList)
    {
        $data = in_array(self::REL_AUTHOR, $includeList)
              ? $wiki->author
              : \User::build(['id' => $wiki->user_id], false);
        $relationships[self::REL_AUTHOR] = [
            self::LINKS => [
                Link::RELATED => new Link('/users/' . $wiki->user_id),
            ],
            self::DATA => $data,
        ];

        return $relationships;
    }

    private function addRangeRelationship($relationships, $wiki, $includeList)
    {
        $relationships[self::REL_RANGE] = [
            self::LINKS => [
                Link::RELATED => new Link('/' . self::getRangeType($wiki) . '/' . $wiki->range_id),
            ],
            self::DATA => $this->prepareRange($wiki, $includeList),
        ];

        return $relationships;
    }

    private function prepareRange($wiki)
    {
        $class = self::getRangeClass($wiki);

        return $class::build(['id' => $wiki->range_id], false);
    }
}