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

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

class CourseOfStudy extends SchemaProvider
{
    const REL_SECTIONS = 'sections';
    const REL_INSTITUTE = 'institute';
    const REL_COMPONENTS = 'components';
    const REL_DEGREE = 'degree';
    const REL_END_SEMESTER = 'end-semester';
    const REL_START_SEMESTER = 'start-semester';
    const TYPE = 'courses-of-study';

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

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'display-name' => (string) $resource->getDisplayName(),
            'name' => (string) $resource->name,
            'short-name' => (string) $resource->name_kurz,
            'type' => (string) $resource->typ,
            'status' => (string) $resource->stat,
            'status-name' => \Config::get()->MVV_STUDIENGANG['STATUS']['values'][$resource->stat]['name'] ?? '',
            'classname' => get_class($resource)
        ];
    }

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

        $institute = \Institute::find($resource->institut_id);
        if ($institute) {
            $relationships[self::REL_INSTITUTE] = $this->getInstitute($resource, $this->shouldInclude($context, self::REL_INSTITUTE));
        }

        if ($semester = $this->getStartSemester($resource)) {
            $relationships[self::REL_START_SEMESTER] = $semester;
        }
        if ($semester = $this->getEndSemester($resource)) {
            $relationships[self::REL_END_SEMESTER] = $semester;
        }

        $relationships = $this->addSectionsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_SECTIONS));
        $relationships = $this->addComponentsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_COMPONENTS));
        $relationships = $this->addDegreeRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DEGREE));

        return $relationships;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    private function getInstitute(\Studiengang $course_of_study, $shouldInclude)
    {
        $institute = \Institute::find($course_of_study->institut_id);
        return $institute
            ?  [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($institute),
                ],
                self::RELATIONSHIP_DATA => $institute,
            ]
            : [
                self::RELATIONSHIP_DATA => null,
            ];
    }

    private function getStartSemester(\Studiengang $course_of_study)
    {
        $semester = \Semester::find($course_of_study->start);
        if (!$semester) {
            return null;
        }

        return [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($semester),
            ],
            self::RELATIONSHIP_DATA => $semester,
        ];
    }

    private function getEndSemester(\Studiengang $course_of_study)
    {
        $semester = \Semester::find($course_of_study->end);
        if (!$semester) {
            return null;
        }

        return [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($semester),
            ],
            self::RELATIONSHIP_DATA => $semester,
        ];
    }

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

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

        return $relationships;
    }

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

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

        return $relationships;
    }

    private function addDegreeRelationship(array $relationships, $resource, $includeData)
    {
        $relationships[self::REL_DEGREE] = [
            self::RELATIONSHIP_LINKS_SELF => $this->createLinkToResource($resource->abschluss),
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_DEGREE),
            ],
        ];

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

        return $relationships;
    }
}