aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Courseware/Container.php
blob: 853bf76296471a8bfcfa309a13ecd315da9b02c8 (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
<?php

namespace JsonApi\Schemas\Courseware;

use JsonApi\Schemas\SchemaProvider;
use Neomerx\JsonApi\Document\Link;

class Container extends SchemaProvider
{
    const TYPE = 'courseware-containers';

    const REL_BLOCKS = 'blocks';
    const REL_OWNER = 'owner';
    const REL_EDITOR = 'editor';
    const REL_EDITBLOCKER = 'edit-blocker';
    const REL_STRUCTURAL_ELEMENT = 'structural-element';

    protected $resourceType = self::TYPE;

    /**
     * {@inheritdoc}
     */
    public function getId($resource)
    {
        return $resource->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getAttributes($resource)
    {
        return [
            'position' => (int) $resource['position'],
            'site' => (int) $resource['site'],
            'container-type' => (string) $resource['container_type'],
            'title' => (string) $resource->type->getTitle(),
            'width' => (string) $resource->type->getContainerWidth(),
            'visible' => (bool) $resource['visible'],
            'payload' => $resource['payload']->getIterator(),
            'mkdate' => date('c', $resource['mkdate']),
            'chdate' => date('c', $resource['chdate']),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getRelationships($resource, $isPrimary, array $includeList)
    {
        $relationships = [];

        $shouldInclude = function ($key) use ($includeList) {
            return in_array($key, $includeList);
        };

        $relationships = $this->addBlocksRelationship($relationships, $resource, $shouldInclude(self::REL_BLOCKS));

        $relationships[self::REL_OWNER] = $resource->owner
            ? [
                self::LINKS => [
                    Link::RELATED => $this->getSchemaContainer()
                        ->getSchema($resource->owner)
                        ->getSelfSubLink($resource->owner),
                ],
                self::DATA => $resource->owner,
            ]
            : [self::DATA => $resource->owner];

        $relationships[self::REL_EDITOR] = $resource->editor
            ? [
                self::LINKS => [
                    Link::RELATED => $this->getSchemaContainer()
                        ->getSchema($resource->editor)
                        ->getSelfSubLink($resource->editor),
                ],
                self::DATA => $resource->editor,
            ]
            : [self::DATA => null];

        $relationships[self::REL_EDITBLOCKER] = $resource->edit_blocker
            ? [
                self::SHOW_SELF => true,
                self::LINKS => [
                    Link::RELATED => $this->getSchemaContainer()
                        ->getSchema($resource->edit_blocker)
                        ->getSelfSubLink($resource->edit_blocker),
                ],
                self::DATA => $resource->edit_blocker,
            ]
            : [self::SHOW_SELF => true, self::DATA => null];

        $relationships[self::REL_STRUCTURAL_ELEMENT] = $resource['structural_element_id']
            ? [
                self::LINKS => [
                    Link::RELATED => $this->getSchemaContainer()
                        ->getSchema($resource->structural_element)
                        ->getSelfSubLink($resource->structural_element),
                ],
                self::DATA => $resource->structural_element,
            ]
            : [self::DATA => null];

        return $relationships;
    }

    private function addBlocksRelationship(array $relationships, $resource, $includeData)
    {
        $relation = [
            self::SHOW_SELF => true,
            self::LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_BLOCKS),
            ],
        ];

        $relation[self::DATA] = $resource->blocks;

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

        return $relationships;
    }
}