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
|
<?php
namespace JsonApi\Schemas;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
class ConsultationSlot extends SchemaProvider
{
const TYPE = 'consultation-slots';
const REL_BLOCK = 'block';
const REL_BOOKINGS = 'bookings';
public function getId($resource): ?string
{
return $resource->id;
}
/**
* @param \ConsultationSlot $resource
* @param ContextInterface $context
*
* @return iterable
*/
public function getAttributes($resource, ContextInterface $context): iterable
{
$attributes = [
'note' => $resource->note,
'note-html' => formatLinks($resource->note),
'start_time' => date('c', $resource->start_time),
'end_time' => date('c', $resource->end_time),
'is-bookable' => !$resource->isOccupied() && !$resource->isLocked(),
'is-locked' => $resource->isLocked(),
'mkdate' => date('c', $resource->mkdate),
'chdate' => date('c', $resource->chdate),
];
return $attributes;
}
/**
* In dieser Methode können Relationships zu anderen Objekten
* spezifiziert werden.
* {@inheritdoc}
*/
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$isPrimary = $context->getPosition()->getLevel() === 0;
if ($isPrimary) {
$relationships = $this->getBlockRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_BLOCK));
$relationships = $this->getBookingsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_BOOKINGS));
}
return $relationships;
}
// #### PRIVATE HELPERS ####
private function getBlockRelationship($relationships, \ConsultationSlot $resource, $includeData)
{
$block = $resource->block;
$relationships[self::REL_BLOCK] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($block),
],
self::RELATIONSHIP_DATA => $includeData ? $block : \ConsultationBlock::build(['id' => $block->id], false),
];
return $relationships;
}
private function getBookingsRelationship(array $relationships, \ConsultationSlot $resource, $includeData)
{
if ($includeData) {
$relatedBookings = $resource->bookings;
} else {
$relatedBookings = $resource->bookings->map(function ($booking) {
return \ConsultationBooking::build(['booking_id' => $booking->id], false);
});
}
$relationships[self::REL_BOOKINGS] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_BOOKINGS),
],
self::RELATIONSHIP_DATA => $relatedBookings,
];
return $relationships;
}
}
|