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

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

class ModuleInstitute extends SchemaProvider
{
    const REL_MODULE = 'modules';
    const REL_INSTITUTE = 'institutes';
    const TYPE = 'module-institutes';

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

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'name' => (string) $resource->name,
            'short-name' => (string) $resource->name_kurz,
            'description' => (string) $resource->beschreibung,
            'type' => get_class($resource)
        ];
    }

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

        $relationships = $this->addModuleRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_MODULE));
        $relationships = $this->addInstituteRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_INSTITUTE));

        return $relationships;
    }

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

        if ($includeData) {
            $relationships[self::REL_MODULE][self::RELATIONSHIP_DATA] = $resource->module;
        }

        return $relationships;
    }

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

        if ($includeData) {
            $relationships[self::REL_INSTITUTE][self::RELATIONSHIP_DATA] = $resource->institute;
        }

        return $relationships;
    }
}