|
1
|
|
|
<?php namespace Limoncello\Flute\Http; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Flute\Contracts\Models\PaginatedDataInterface; |
|
20
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface; |
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface; |
|
24
|
|
|
use Neomerx\JsonApi\Http\BaseResponses; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @package Limoncello\Flute |
|
28
|
|
|
*/ |
|
29
|
|
|
class Responses extends BaseResponses |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var EncodingParametersInterface|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private $parameters; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var EncoderInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $encoder; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var MediaTypeInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $outputMediaType; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ContainerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $schemes; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var null|string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $urlPrefix; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var mixed|null |
|
58
|
|
|
*/ |
|
59
|
|
|
private $defaultMeta; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param MediaTypeInterface $outputMediaType |
|
63
|
|
|
* @param EncoderInterface $encoder |
|
64
|
|
|
* @param ContainerInterface $schemes |
|
65
|
|
|
* @param EncodingParametersInterface|null $parameters |
|
66
|
|
|
* @param string|null $urlPrefix |
|
67
|
|
|
* @param mixed|null $defaultMeta |
|
68
|
|
|
*/ |
|
69
|
22 |
|
public function __construct( |
|
70
|
|
|
MediaTypeInterface $outputMediaType, |
|
71
|
|
|
EncoderInterface $encoder, |
|
72
|
|
|
ContainerInterface $schemes, |
|
73
|
|
|
EncodingParametersInterface $parameters = null, |
|
74
|
|
|
string $urlPrefix = null, |
|
75
|
|
|
$defaultMeta = null |
|
76
|
|
|
) { |
|
77
|
22 |
|
$this->encoder = $encoder; |
|
78
|
22 |
|
$this->outputMediaType = $outputMediaType; |
|
79
|
22 |
|
$this->schemes = $schemes; |
|
80
|
22 |
|
$this->urlPrefix = $urlPrefix; |
|
81
|
22 |
|
$this->parameters = $parameters; |
|
82
|
22 |
|
$this->defaultMeta = $defaultMeta; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
16 |
|
public function getContentResponse( |
|
89
|
|
|
$data, |
|
90
|
|
|
int $statusCode = self::HTTP_OK, |
|
91
|
|
|
array $links = null, |
|
92
|
|
|
$meta = null, |
|
93
|
|
|
array $headers = [] |
|
94
|
|
|
) { |
|
95
|
16 |
|
return parent::getContentResponse($data, $statusCode, $links, $this->mergeDefaultMeta($meta), $headers); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getCreatedResponse($resource, array $links = null, $meta = null, array $headers = []) |
|
102
|
|
|
{ |
|
103
|
1 |
|
return parent::getCreatedResponse($resource, $links, $this->mergeDefaultMeta($meta), $headers); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @inheritdoc |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function getIdentifiersResponse( |
|
110
|
|
|
$data, |
|
111
|
|
|
int $statusCode = self::HTTP_OK, |
|
112
|
|
|
array $links = null, |
|
113
|
|
|
$meta = null, |
|
114
|
|
|
array $headers = [] |
|
115
|
|
|
) { |
|
116
|
1 |
|
return parent::getIdentifiersResponse($data, $statusCode, $links, $this->mergeDefaultMeta($meta), $headers); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @inheritdoc |
|
121
|
|
|
*/ |
|
122
|
22 |
|
protected function createResponse(?string $content, int $statusCode, array $headers) |
|
123
|
|
|
{ |
|
124
|
22 |
|
return new JsonApiResponse($content, $statusCode, $headers); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
18 |
|
protected function getEncoder(): EncoderInterface |
|
131
|
|
|
{ |
|
132
|
18 |
|
return $this->encoder; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritdoc |
|
137
|
|
|
*/ |
|
138
|
1 |
|
protected function getUrlPrefix(): ?string |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $this->urlPrefix; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @inheritdoc |
|
145
|
|
|
*/ |
|
146
|
18 |
|
protected function getEncodingParameters(): ?EncodingParametersInterface |
|
147
|
|
|
{ |
|
148
|
18 |
|
return $this->parameters; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @inheritdoc |
|
153
|
|
|
*/ |
|
154
|
1 |
|
protected function getSchemaContainer(): ?ContainerInterface |
|
155
|
|
|
{ |
|
156
|
1 |
|
return $this->schemes; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @inheritdoc |
|
161
|
|
|
*/ |
|
162
|
18 |
|
protected function getMediaType(): MediaTypeInterface |
|
163
|
|
|
{ |
|
164
|
18 |
|
return $this->outputMediaType; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @inheritdoc |
|
169
|
|
|
*/ |
|
170
|
1 |
|
protected function getResourceLocationUrl($resource): string |
|
171
|
|
|
{ |
|
172
|
1 |
|
return parent::getResourceLocationUrl( |
|
173
|
1 |
|
$resource instanceof PaginatedDataInterface ? $resource->getData() : $resource |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return mixed|null |
|
179
|
|
|
*/ |
|
180
|
18 |
|
protected function getDefaultMeta() |
|
181
|
|
|
{ |
|
182
|
18 |
|
return $this->defaultMeta; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Merge with default meta if possible (if both are arrays). |
|
187
|
|
|
* |
|
188
|
|
|
* @param mixed $meta |
|
189
|
|
|
* |
|
190
|
|
|
* @return mixed|null |
|
191
|
|
|
*/ |
|
192
|
18 |
|
private function mergeDefaultMeta($meta) |
|
193
|
|
|
{ |
|
194
|
18 |
|
$default = $this->getDefaultMeta(); |
|
195
|
|
|
|
|
196
|
18 |
|
return $meta === null ? |
|
197
|
18 |
|
$default : (is_array($meta) === true && is_array($default) === true ? $meta + $default : $meta); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|