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
100
101
102
103
104
105
106
|
<?php
namespace JsonApi\Schemas;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
class StudyArea extends SchemaProvider
{
const REL_CHILDREN = 'children';
const REL_COURSES = 'courses';
const REL_INSTITUTE = 'institute';
const REL_PARENT = 'parent';
const TYPE = 'study-areas';
public function getId($resource): ?string
{
return $resource['id'];
}
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'name' => (string) $resource['name'],
'info' => (string) $resource['info'],
'priority' => (int) $resource['priority'],
'type-name' => (string) $resource->getTypeName(),
'has-children' => (bool) $resource->hasChildNodes(),
'ancestors' => (array) $resource->getAncestors(),
'classname' => get_class($resource)
];
}
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$relationships = $this->addChildrenRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_CHILDREN));
$relationships = $this->addCoursesRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_COURSES));
$relationships = $this->addInstituteRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_INSTITUTE));
$relationships = $this->addParentRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_PARENT));
return $relationships;
}
private function addChildrenRelationship(array $relationships, $resource, $includeData)
{
$relationships[self::REL_CHILDREN] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_CHILDREN),
],
];
if ($includeData) {
$children = $resource->getChildren();
$relationships[self::REL_CHILDREN][self::RELATIONSHIP_DATA] = $children;
}
return $relationships;
}
private function addCoursesRelationship(array $relationships, $resource, $includeData)
{
$relationships[self::REL_COURSES] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_COURSES),
],
];
if ($includeData) {
$children = $resource->courses;
$relationships[self::REL_COURSES][self::RELATIONSHIP_DATA] = $children;
}
return $relationships;
}
private function addInstituteRelationship(array $relationships, $resource, $includeData)
{
$relationships[self::REL_INSTITUTE] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_INSTITUTE),
],
];
if ($includeData) {
$relationships[self::REL_INSTITUTE][self::RELATIONSHIP_DATA] = $resource->institute;
}
return $relationships;
}
private function addParentRelationship(array $relationships, $resource, $includeData)
{
$relationships[self::REL_PARENT] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_PARENT),
],
];
if ($includeData) {
$relationships[self::REL_PARENT][self::RELATIONSHIP_DATA] = $resource->getParent();
}
return $relationships;
}
}
|