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

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

class CourseOfStudyComponent extends SchemaProvider
{
    const REL_SUBJECT = 'subject';
    const REL_VERSIONS = 'versions';
    const TYPE = 'courses-of-study-components';

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

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'display-name' => (string) $resource->getDisplayName(),
            'title-supplement' => (string) $resource->zusatz,
            'cp' => (string) $resource->kp,
            'semesters' => (string) $resource->semester,
            'classname' => get_class($resource)
        ];
    }

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

        if ($resource->fach) {
            $relationships[self::REL_SUBJECT] = $this->getSubject($resource, $this->shouldInclude($context, self::REL_SUBJECT));
        }

        $relationships = $this->addVersionsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_VERSIONS));

        return $relationships;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    private function getSubject(\StudiengangTeil $component, $shouldInclude)
    {
        return $component->fach
            ?  [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($component->fach),
                ],
                self::RELATIONSHIP_DATA => $component->fach,
            ]
            : [
                self::RELATIONSHIP_DATA => null,
            ];
    }

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

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

        return $relationships;
    }
}