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
|
<?php
namespace JsonApi\Schemas;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
class UserFilterField extends SchemaProvider
{
const TYPE = 'user-filter-fields';
const REL_USERS = 'users';
public function getId($resource): ?string
{
return get_class($resource) . '_' . $resource->getId();
}
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'type' => get_class($resource),
'typeparam' => property_exists($resource, 'datafield_id') ? $resource->datafield_id : null,
'id' => $resource->getId(),
'compare-operator' => $resource->getCompareOperator(),
'compare-operator-text' => $resource->getCompareOperatorAsText(),
'name' => $resource->getName(),
'valid-compare-operators' => $resource->getValidCompareOperators(),
'valid-values' => $resource->getValidValues(),
'value' => $resource->getValue()
];
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$relationships = $this->addUsersRelationship(
$relationships,
$resource,
$this->shouldInclude($context, self::REL_USERS)
);
return $relationships;
}
private function addUsersRelationship(
array $relationships,
$resource,
$includeData
) {
$relation = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_USERS),
]
];
if ($includeData) {
$related = $resource->getUsers();
$relation[self::RELATIONSHIP_DATA] = $related;
}
return array_merge($relationships, [self::REL_USERS => $relation]);
}
}
|