aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/FeedbackEntry.php
blob: 37e275e614a4ee3ac1dec1753699fc8cf04c657c (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
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
<?php

namespace JsonApi\Schemas;

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

class FeedbackEntry extends SchemaProvider
{
    const TYPE = 'feedback-entries';
    const REL_AUTHOR = 'author';
    const REL_FEEDBACK = 'feedback-element';

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

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        $attributes = [
            'comment' => (string) $resource['comment'],
            'rating' => 0 === $resource->feedback->mode ? null : $resource['rating'],
            'anonymous' => (bool) $resource['anonymous'],
            'mkdate' => date('c', $resource['mkdate']),
            'chdate' => date('c', $resource['chdate']),
        ];

        return $attributes;
    }

    /**
     * In dieser Methode können Relationships zu anderen Objekten
     * spezifiziert werden.
     *
     * {@inheritdoc}
     */
    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];

        $relationships = $this->getAuthorRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_AUTHOR)
        );
        $relationships = $this->getFeedbackElementRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_FEEDBACK)
        );

        return $relationships;
    }

    private function getAuthorRelationship(array $relationships, \FeedbackEntry $resource, $includeData): array
    {
        $userId = $resource['user_id'];
        $related = $includeData ? \User::find($userId) : \User::build(['id' => $userId], false);
        $relationships[self::REL_AUTHOR] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($related),
            ],
            self::RELATIONSHIP_DATA => $related,
        ];

        return $relationships;
    }

    private function getFeedbackElementRelationship(
        array $relationships,
        \FeedbackEntry $resource,
        $includeData
    ): array {
        $related = $includeData
            ? $resource->feedback
            : \FeedbackElement::build(['id' => $resource->feedback_id], false);

        $relationships[self::REL_FEEDBACK] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($related),
            ],
            self::RELATIONSHIP_DATA => $related,
        ];

        return $relationships;
    }
}