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
|
<?php
namespace JsonApi\Schemas;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
final class ClipboardItem extends SchemaProvider
{
public const TYPE = 'clipboard-items';
public const REL_CLIPBOARD = 'clipboard';
/**
* @param \ClipboardItem $resource
*/
public function getId($resource): ?string
{
return (string) $resource->id;
}
/**
* @param \ClipboardItem $resource
*/
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'range_id' => $resource->range_id,
'range_type' => $resource->range_type,
'name' => $resource->name,
'mkdate' => date('c', $resource->mkdate),
'chdate' => date('c', $resource->chdate),
];
}
/**
* @param \ClipboardItem $resource
*/
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$isPrimary = $context->getPosition()->getLevel() === 0;
if ($isPrimary) {
$relationships = $this->getClipboardRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_CLIPBOARD));
}
return $relationships;
}
private function getClipboardRelationship(array $relationships, \ClipboardItem $clipboardItem, bool $includeData): array
{
$relationships[self::REL_CLIPBOARD] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($clipboardItem->clipboard),
],
self::RELATIONSHIP_DATA => $includeData ? $clipboardItem->clipboard : \User::build(['id' => $clipboardItem->clipboard_id], false),
];
return $relationships;
}
}
|