|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Http; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersParserInterface; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
|
24
|
|
|
use Neomerx\JsonApi\Contracts\Http\ResponsesInterface; |
|
25
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @package Neomerx\JsonApi |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class BaseResponses implements ResponsesInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** Header name that contains format of input data from client */ |
|
33
|
|
|
const HEADER_CONTENT_TYPE = HeaderParametersParserInterface::HEADER_CONTENT_TYPE; |
|
34
|
|
|
|
|
35
|
|
|
/** Header name that location of newly created resource */ |
|
36
|
|
|
const HEADER_LOCATION = 'Location'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create HTTP response. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string|null $content |
|
42
|
|
|
* @param int $statusCode |
|
43
|
|
|
* @param array $headers |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract protected function createResponse(?string $content, int $statusCode, array $headers); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return EncoderInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function getEncoder(): EncoderInterface; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return MediaTypeInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract protected function getMediaType(): MediaTypeInterface; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function getContentResponse($data, int $statusCode = self::HTTP_OK, array $headers = []) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$content = $this->getEncoder()->encodeData($data); |
|
65
|
|
|
|
|
66
|
2 |
|
return $this->createJsonApiResponse($content, $statusCode, $headers, true); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function getCreatedResponse($resource, string $url, array $headers = []) |
|
73
|
|
|
{ |
|
74
|
2 |
|
$content = $this->getEncoder()->encodeData($resource); |
|
75
|
2 |
|
$headers[self::HEADER_LOCATION] = $url; |
|
76
|
|
|
|
|
77
|
2 |
|
return $this->createJsonApiResponse($content, self::HTTP_CREATED, $headers, true); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritdoc |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getCodeResponse(int $statusCode, array $headers = []) |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->createJsonApiResponse(null, $statusCode, $headers, false); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritdoc |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getMetaResponse($meta, int $statusCode = self::HTTP_OK, array $headers = []) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$content = $this->getEncoder()->encodeMeta($meta); |
|
94
|
|
|
|
|
95
|
2 |
|
return $this->createJsonApiResponse($content, $statusCode, $headers, true); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritDoc |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function getIdentifiersResponse($data, int $statusCode = self::HTTP_OK, array $headers = []) |
|
102
|
|
|
{ |
|
103
|
2 |
|
$content = $this->getEncoder()->encodeIdentifiers($data); |
|
104
|
|
|
|
|
105
|
2 |
|
return $this->createJsonApiResponse($content, $statusCode, $headers, true); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
* |
|
111
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function getErrorResponse($errors, int $statusCode = self::HTTP_BAD_REQUEST, array $headers = []) |
|
114
|
|
|
{ |
|
115
|
4 |
|
if (\is_iterable($errors) === true) { |
|
116
|
|
|
/** @var iterable $errors */ |
|
117
|
2 |
|
$content = $this->getEncoder()->encodeErrors($errors); |
|
118
|
|
|
} else { |
|
119
|
2 |
|
\assert($errors instanceof ErrorInterface); |
|
120
|
2 |
|
$content = $this->getEncoder()->encodeError($errors); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
4 |
|
return $this->createJsonApiResponse($content, $statusCode, $headers, true); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string|null $content |
|
128
|
|
|
* @param int $statusCode |
|
129
|
|
|
* @param array $headers |
|
130
|
|
|
* @param bool $addContentType |
|
131
|
|
|
* |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
* |
|
134
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
135
|
|
|
*/ |
|
136
|
13 |
|
protected function createJsonApiResponse( |
|
137
|
|
|
?string $content, |
|
138
|
|
|
int $statusCode, |
|
139
|
|
|
array $headers = [], |
|
140
|
|
|
$addContentType = true |
|
141
|
|
|
) { |
|
142
|
13 |
|
if ($addContentType === true) { |
|
143
|
12 |
|
$headers[self::HEADER_CONTENT_TYPE] = $this->getMediaType()->getMediaType(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
13 |
|
return $this->createResponse($content, $statusCode, $headers); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|