|
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\Api\ModelsDataInterface; |
|
20
|
|
|
use Limoncello\Flute\Contracts\Encoder\EncoderInterface; |
|
21
|
|
|
use Limoncello\Flute\Contracts\Models\PaginatedDataInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
|
24
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\SupportedExtensionsInterface; |
|
25
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface; |
|
26
|
|
|
use Neomerx\JsonApi\Http\Responses as JsonApiResponses; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @package Limoncello\Flute |
|
30
|
|
|
*/ |
|
31
|
|
|
class Responses extends JsonApiResponses |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var EncodingParametersInterface|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $parameters; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var EncoderInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $encoder; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var MediaTypeInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $outputMediaType; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var SupportedExtensionsInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $extensions; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var ContainerInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $schemes; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var null|string |
|
60
|
|
|
*/ |
|
61
|
|
|
private $urlPrefix; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param MediaTypeInterface $outputMediaType |
|
65
|
|
|
* @param SupportedExtensionsInterface $extensions |
|
66
|
|
|
* @param EncoderInterface $encoder |
|
67
|
|
|
* @param ContainerInterface $schemes |
|
68
|
|
|
* @param EncodingParametersInterface|null $parameters |
|
69
|
|
|
* @param string|null $urlPrefix |
|
70
|
|
|
*/ |
|
71
|
20 |
|
public function __construct( |
|
72
|
|
|
MediaTypeInterface $outputMediaType, |
|
73
|
|
|
SupportedExtensionsInterface $extensions, |
|
74
|
|
|
EncoderInterface $encoder, |
|
75
|
|
|
ContainerInterface $schemes, |
|
76
|
|
|
EncodingParametersInterface $parameters = null, |
|
77
|
|
|
string $urlPrefix = null |
|
78
|
|
|
) { |
|
79
|
20 |
|
$this->extensions = $extensions; |
|
80
|
20 |
|
$this->encoder = $encoder; |
|
81
|
20 |
|
$this->outputMediaType = $outputMediaType; |
|
82
|
20 |
|
$this->schemes = $schemes; |
|
83
|
20 |
|
$this->urlPrefix = $urlPrefix; |
|
84
|
20 |
|
$this->parameters = $parameters; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
20 |
|
protected function createResponse($content, $statusCode, array $headers) |
|
91
|
|
|
{ |
|
92
|
20 |
|
return new JsonApiResponse($content, $statusCode, $headers); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
*/ |
|
98
|
17 |
|
protected function getEncoder() |
|
99
|
|
|
{ |
|
100
|
17 |
|
return $this->encoder; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @inheritdoc |
|
105
|
|
|
*/ |
|
106
|
1 |
|
protected function getUrlPrefix() |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->urlPrefix; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @inheritdoc |
|
113
|
|
|
*/ |
|
114
|
17 |
|
protected function getEncodingParameters() |
|
115
|
|
|
{ |
|
116
|
17 |
|
return $this->parameters; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @inheritdoc |
|
121
|
|
|
*/ |
|
122
|
1 |
|
protected function getSchemaContainer() |
|
123
|
|
|
{ |
|
124
|
1 |
|
return $this->schemes; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
20 |
|
protected function getSupportedExtensions() |
|
131
|
|
|
{ |
|
132
|
20 |
|
return $this->extensions; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritdoc |
|
137
|
|
|
*/ |
|
138
|
20 |
|
protected function getMediaType() |
|
139
|
|
|
{ |
|
140
|
20 |
|
return $this->outputMediaType; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @inheritdoc |
|
145
|
|
|
*/ |
|
146
|
1 |
|
protected function getResourceLocationUrl($resource) |
|
147
|
|
|
{ |
|
148
|
1 |
|
if ($resource instanceof ModelsDataInterface) { |
|
149
|
1 |
|
$resource = $resource->getPaginatedData(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
if ($resource instanceof PaginatedDataInterface) { |
|
153
|
1 |
|
$resource = $resource->getData(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
return parent::getResourceLocationUrl($resource); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|