aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Avatar.php
blob: 5926f591a035da88b733cc6dc10359edcbd735c7 (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
<?php

namespace JsonApi\Schemas;

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

class Avatar extends SchemaProvider
{
    public const TYPE = 'avatar';
    const REL_RANGE = 'range';

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

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'type' => $resource::AVATAR_TYPE,
            'customized' => $resource->is_customized(),
            'is-nobody' => $resource->isNobody(),
        ];
    }
    public function hasResourceMeta($resource): bool
    {
        return true;
    }

    public function getResourceMeta($resource)
    {
        return [
            'url' => [
                'normal' => $resource->getURL(\Avatar::NORMAL),
                'medium' => $resource->getURL(\Avatar::MEDIUM),
                'small' => $resource->getURL(\Avatar::SMALL),
            ]
        ];
    }

    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];
        $range = self::getRange($resource->getId(),  $resource::AVATAR_TYPE);
        $relationships[self::REL_RANGE] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($range),
            ],
            self::RELATIONSHIP_DATA => $range,
        ];
        return $relationships;
    }

    private function getRange(String $range_id, String $range_type)
    {
        switch ($range_type) {
            case 'course':
            case 'studygroup':
                return \Course::build(['id' => $range_id], false);
            case 'user':
                return \User::build(['id' => $range_id], false);
            case 'institute':
                return \Institute::build(['id' => $range_id], false);
        }
        return null;
    }
}