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 Fach;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
class Subject extends SchemaProvider
{
const REL_DEPARTMENTS = 'departments';
const TYPE = 'subjects';
/**
* @param Fach $resource
*/
public function getId($resource): ?string
{
return $resource->id;
}
/**
* @param Fach $resource
*/
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'name' => (string) $resource->name,
'short-name' => (string) $resource->name_kurz,
'description' => (string) $resource->beschreibung,
'type' => get_class($resource)
];
}
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$relationships = $this->addDepartmentsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DEPARTMENTS));
return $relationships;
}
private function addDepartmentsRelationship(array $relationships, $resource, $includeData)
{
$relationships[self::REL_DEPARTMENTS] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_DEPARTMENTS),
],
];
if ($includeData) {
// use institute schema
if (!empty($resource->departments)) {
$institutes = \Institute::findMany($resource->departments->pluck('id'));
$relationships[self::REL_DEPARTMENTS][self::RELATIONSHIP_DATA] = $institutes;
}
}
return $relationships;
}
}
|