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

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

final class ResourceBookingIntervalSchema extends SchemaProvider
{
    const TYPE = 'resource-booking-intervals';

    const REL_BOOKING = 'booking';
    const REL_RESOURCE = 'resource';

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

    /**
     * @param ResourceBookingInterval $resource
     */
    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'begin' => date('c', $resource->begin),
            'end' => date('c', $resource->end),

            'takes-place' => (bool) $resource->takes_place,

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

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

        $isPrimary = $context->getPosition()->getLevel() === 0;
        if ($isPrimary) {
            $relationships = $this->getBookingRelationship(
                $relationships,
                $resource->booking,
                $this->shouldInclude($context, self::REL_BOOKING)
            );
            $relationships = $this->getResourceRelationship(
                $relationships,
                $resource->resource,
                $this->shouldInclude($context, self::REL_RESOURCE)
            );
        }

        return $relationships;
    }

    private function getBookingRelationship(
        array $relationships,
        \ResourceBooking $booking,
        bool $includeData
    ): array {
        $relationships[self::REL_BOOKING] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($booking),
            ],
            self::RELATIONSHIP_DATA => $includeData ? $booking : \ResourceBooking::build(['id' => $booking->id]),
        ];

        return $relationships;
    }

    private function getResourceRelationship(
        array $relationships,
        \Resource $resource,
        bool $includeData
    ): array {
        $relationships[self::REL_RESOURCE] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($resource),
            ],
            self::RELATIONSHIP_DATA => $includeData ? $resource : \Resource::build(['id' => $resource->id]),
        ];
        return $relationships;
    }
}