aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/ConsultationBooking.php
blob: 5aa96310c96ca838b6039576b3aecad284ef2927 (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
<?php

namespace JsonApi\Schemas;

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

class ConsultationBooking extends SchemaProvider
{
    const TYPE = 'consultation-bookings';

    const REL_SLOT = 'slot';
    const REL_USER = 'user';

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

    /**
     * @param \ConsultationBooking $resource
     */
    public function getAttributes($resource, ContextInterface $context): iterable
    {
        $attributes = [
            'reason' => $resource->reason,

            'mkdate' => date('c', $resource->mkdate),
            'chdate' => date('c', $resource->chdate),
        ];

        return $attributes;
    }

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

        $isPrimary = $context->getPosition()->getLevel() === 0;
        if ($isPrimary) {
            $relationships = $this->getSlotRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_SLOT));
            $relationships = $this->getUserRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_USER));
        }

        return $relationships;
    }

    // #### PRIVATE HELPERS ####

    private function getSlotRelationship(array $relationships, \ConsultationBooking $resource, $includeData)
    {
        $slot = $resource->slot;

        $relationships[self::REL_SLOT] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_SLOT),
            ],
            self::RELATIONSHIP_DATA => $includeData ? $slot : \ConsultationSlot::build(['id' => $slot->id], false),
        ];

        return $relationships;
    }

    private function getUserRelationship($relationships, \ConsultationBooking $resource, $includeData)
    {
        $user = $resource->user;

        $relationships[self::REL_USER] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_USER),
            ],
            self::RELATIONSHIP_DATA => $includeData ? $user : \User::build(['id' => $user->id], false),
        ];

        return $relationships;
    }
}