aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/JsonApiIntegration/Responses.php
blob: 8c9fbfd6c8704e3d094cb99712509237c76c5414 (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
<?php

namespace JsonApi\JsonApiIntegration;

use Neomerx\JsonApi\Http\BaseResponses;
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
use Neomerx\JsonApi\Contracts\Http\Headers\SupportedExtensionsInterface;
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;

/**
 * Diese Factory-Klasse verknüpft die "neomerx/json-api"-Bibliothek mit der
 * Slim-Applikation. Hier wird festgelegt, wie Slim-artige Response-Objekte gebildet
 * werden.
 */
class Responses extends BaseResponses
{
    public function __construct(
        private EncoderInterface $encoder,
        private MediaTypeInterface $outputMediaType,
        private ResponseFactoryInterface $responseFactory
    ) {
    }

    /**
     * Diese Methode ist die Schlüsselstelle der ganzen Klasse. Es
     * werden Body, Statuscode und Headers der zukünftigen Response
     * übergeben und eine \Slim\Http\Response zurückgegeben.
     *
     * @param string|null $content    der Body der zukünftigen Response
     * @param int         $statusCode der numerische Statuscode der
     *                                zukünftigen Response
     * @param array       $headers    die Header der zukünftigen Response
     *
     * @return mixed die fertige Slim-Response
     */
    protected function createResponse(?string $content, int $statusCode, array $headers)
    {
        $response = $this->responseFactory->createResponse($statusCode);
        foreach ($headers as $header => $value) {
            $response = $response->withHeader($header, $value);
        }
        $response->getBody()->write($content ?? '');

        return $response->withProtocolVersion('1.1');
    }

    /**
     * {@inheritdoc}
     *
     * @internal
     */
    protected function getEncoder(): EncoderInterface
    {
        return $this->encoder;
    }

    /**
     * {@inheritdoc}
     *
     * @internal
     */
    protected function getMediaType(): MediaTypeInterface
    {
        return $this->outputMediaType;
    }
}