aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/ComponentSection.php
blob: 459d6e0791ab824087130e0f701b2a6081213a84 (plain)
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
<?php
namespace JsonApi\Schemas;

use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;

class ComponentSection extends SchemaProvider
{
    const REL_MODULES = 'modules';
    const TYPE = 'component-sections';

    public function getId($resource): ?string
    {
        return $resource->id;
    }

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'display-name' => (string) $resource->getDisplayName(),
            'comment' => $resource->kommentar,
            'position' => $resource->position,
            'cp' => $resource->kp,
            'caption' => $resource->ueberschrift,
            'type' => get_class($resource)
        ];
    }

    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];

        $relationships = $this->addModulesRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_MODULES));

        return $relationships;
    }

    private function addModulesRelationship(array $relationships, $resource, $includeData)
    {
        $relationships[self::REL_MODULES] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_MODULES),
            ],
        ];

        if ($includeData) {
            $relationships[self::REL_MODULES][self::RELATIONSHIP_DATA] = $resource->module;
        }

        return $relationships;
    }
}