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

namespace JsonApi\Schemas;

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

class SemClass extends SchemaProvider
{
    const REL_SEM_TYPES = 'sem-types';
    const TYPE = 'sem-classes';

    public function getId($resource): ?string
    {
        return $resource['id'];
    }

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'name' => (string) $resource['name'],
            'only-inst-user' => (bool) $resource['only_inst_user'],
            'default-read-level' => (int) $resource['default_read_level'],
            'default-write-level' => (int) $resource['default_write_level'],
            'bereiche' => (int) $resource['bereiche'],
            'show-browse' => (bool) $resource['show_browse'],
            'write-access-nobody' => (bool) $resource['write_access_nobody'],
            'topic-create-autor' => (bool) $resource['topic_create_autor'],
            'visible' => (bool) $resource['visible'],
            'course-creation-forbidden' => (bool) $resource['course_creation_forbidden'],
        ];
    }

    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];

        $relationships = $this->addSemTypesRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_SEM_TYPES)
        );

        return $relationships;
    }

    private function addSemTypesRelationship(array $relationships, $resource, $includeData)
    {
        $relation = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_SEM_TYPES),
            ],
        ];

        if ($includeData) {
            $related = $resource->getSemTypes();
            $relation[self::RELATIONSHIP_DATA] = $related;
        }

        $relationships[self::REL_SEM_TYPES] = $relation;

        return $relationships;
    }
}