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

namespace JsonApi\Schemas\Courseware;

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

class Clipboard extends SchemaProvider
{
    const TYPE = 'courseware-clipboards';

    const REL_USER = 'user';
    const REL_STRUCTURAL_ELEMENT = 'structural-element';
    const REL_CONTAINER = 'container';
    const REL_BLOCK = 'block';

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

    /**
     * {@inheritdoc}
     */
    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'name' => (string) $resource->name,
            'description' => (string) $resource->description,
            'block-id' => (int) $resource->block_id,
            'container-id' => (int) $resource->container_id,
            'structural-element-id' => (int) $resource->structural_element_id,
            'object-type' => (string) $resource->object_type,
            'object-kind' => (string) $resource->object_kind,
            'backup' => $resource->backup,
            'mkdate'    => date('c', $resource->mkdate),
            'chdate'    => date('c', $resource->chdate),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];

        $relationships[self::REL_USER] = $resource->user
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($resource->user),
                ],
                self::RELATIONSHIP_DATA => $resource->user,
            ]
            : [self::RELATIONSHIP_DATA => null];
        
        $relationships[self::REL_BLOCK] = $resource->block
        ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($resource->block),
                ],
                self::RELATIONSHIP_DATA => $resource->block,
            ]
            : [self::RELATIONSHIP_DATA => null];

        $relationships[self::REL_CONTAINER] = $resource->container
            ? [
                    self::RELATIONSHIP_LINKS => [
                        Link::RELATED => $this->createLinkToResource($resource->container),
                    ],
                    self::RELATIONSHIP_DATA => $resource->container,
                ]
                : [self::RELATIONSHIP_DATA => null];

        $relationships[self::REL_STRUCTURAL_ELEMENT] = $resource->structural_element
            ? [
                    self::RELATIONSHIP_LINKS => [
                        Link::RELATED => $this->createLinkToResource($resource->structural_element),
                    ],
                    self::RELATIONSHIP_DATA => $resource->structural_element,
                ]
                : [self::RELATIONSHIP_DATA => null];

        return $relationships;
    }
}