|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the reva2/jsonapi. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sergey Revenko <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\Services; |
|
13
|
|
|
|
|
14
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface; |
|
15
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
|
16
|
|
|
use Reva2\JsonApi\Contracts\Decoders\DecoderInterface; |
|
17
|
|
|
use Reva2\JsonApi\Contracts\Services\EnvironmentInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* JSON API environment |
|
21
|
|
|
* |
|
22
|
|
|
* @package Reva2\JsonApi\Services |
|
23
|
|
|
* @author Sergey Revenko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Environment implements EnvironmentInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Expected query parameters type |
|
29
|
|
|
* |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $queryType; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Expected body content type |
|
36
|
|
|
* |
|
37
|
|
|
* @var string|null |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $bodyType; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Codec matcher configuration |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $matcherConfiguration; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* URLs prefix |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $urlPrefix = ''; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Serialization groups |
|
57
|
|
|
* |
|
58
|
|
|
* @var string[] |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $serializationGroups = ['Default']; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Validation groups |
|
64
|
|
|
* |
|
65
|
|
|
* @var string[]|null |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $validationGroups; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Request decoder |
|
71
|
|
|
* |
|
72
|
|
|
* @var DecoderInterface|null |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $decoder; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Response encoder |
|
78
|
|
|
* |
|
79
|
|
|
* @var EncoderInterface|null |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $encoder; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Response encoder media type |
|
85
|
|
|
* |
|
86
|
|
|
* @var MediaTypeInterface|null |
|
87
|
|
|
*/ |
|
88
|
6 |
|
protected $encoderMediaType; |
|
89
|
|
|
|
|
90
|
6 |
|
/** |
|
91
|
2 |
|
* Constructor |
|
92
|
2 |
|
* |
|
93
|
6 |
|
* @param array|null $config |
|
94
|
|
|
*/ |
|
95
|
|
|
public function __construct(array $config = null) |
|
96
|
|
|
{ |
|
97
|
|
|
if (null !== $config) { |
|
98
|
4 |
|
$this->fromArray($config); |
|
99
|
|
|
} |
|
100
|
4 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getQueryType() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->queryType; |
|
108
|
|
|
} |
|
109
|
3 |
|
|
|
110
|
|
|
/** |
|
111
|
3 |
|
* Sets expected query parameters type |
|
112
|
|
|
* |
|
113
|
3 |
|
* @param null|string $queryType |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setQueryType($queryType = null) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->queryType = $queryType; |
|
119
|
4 |
|
|
|
120
|
|
|
return $this; |
|
121
|
4 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritdoc |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getBodyType() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->bodyType; |
|
129
|
|
|
} |
|
130
|
2 |
|
|
|
131
|
|
|
/** |
|
132
|
2 |
|
* Sets expected body content type |
|
133
|
|
|
* |
|
134
|
2 |
|
* @param null|string $bodyType |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setBodyType($bodyType = null) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->bodyType = $bodyType; |
|
140
|
5 |
|
|
|
141
|
|
|
return $this; |
|
142
|
5 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @inheritdoc |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getMatcherConfiguration() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->matcherConfiguration; |
|
150
|
|
|
} |
|
151
|
6 |
|
|
|
152
|
|
|
/** |
|
153
|
6 |
|
* Sets codec matcher configuration |
|
154
|
|
|
* |
|
155
|
6 |
|
* @param array $matcherConfiguration |
|
156
|
|
|
* @return $this |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setMatcherConfiguration(array $matcherConfiguration) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->matcherConfiguration = $matcherConfiguration; |
|
161
|
1 |
|
|
|
162
|
|
|
return $this; |
|
163
|
1 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @inheritdoc |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getUrlPrefix() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->urlPrefix; |
|
171
|
|
|
} |
|
172
|
2 |
|
|
|
173
|
|
|
/** |
|
174
|
2 |
|
* Sets URLs prefix |
|
175
|
|
|
* |
|
176
|
2 |
|
* @param string $urlPrefix |
|
177
|
|
|
* @return $this |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setUrlPrefix($urlPrefix) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->urlPrefix = $urlPrefix; |
|
182
|
4 |
|
|
|
183
|
|
|
return $this; |
|
184
|
4 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return string[] |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getSerializationGroups() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->serializationGroups; |
|
192
|
|
|
} |
|
193
|
5 |
|
|
|
194
|
|
|
/** |
|
195
|
5 |
|
* @param string[] $serializationGroups |
|
196
|
|
|
* @return $this |
|
197
|
5 |
|
*/ |
|
198
|
|
|
public function setSerializationGroups(array $serializationGroups) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->serializationGroups = $serializationGroups; |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
1 |
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
/** |
|
206
|
|
|
* @inheritdoc |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getValidationGroups() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->validationGroups; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
3 |
|
* Sets validation groups |
|
215
|
|
|
* |
|
216
|
3 |
|
* @param array|null $validationGroups |
|
217
|
|
|
* @return $this |
|
218
|
3 |
|
*/ |
|
219
|
|
|
public function setValidationGroups(array $validationGroups = null) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->validationGroups = $validationGroups; |
|
222
|
|
|
|
|
223
|
|
|
return $this; |
|
224
|
1 |
|
} |
|
225
|
|
|
|
|
226
|
1 |
|
/** |
|
227
|
|
|
* @inheritdoc |
|
228
|
|
|
*/ |
|
229
|
|
|
public function getEncoder() |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->encoder; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
3 |
|
* Sets response encoder |
|
236
|
|
|
* |
|
237
|
3 |
|
* @param EncoderInterface $encoder |
|
238
|
|
|
* @return $this |
|
239
|
3 |
|
*/ |
|
240
|
|
|
public function setEncoder(EncoderInterface $encoder) |
|
241
|
|
|
{ |
|
242
|
|
|
$this->encoder = $encoder; |
|
243
|
|
|
|
|
244
|
|
|
return $this; |
|
245
|
3 |
|
} |
|
246
|
|
|
|
|
247
|
3 |
|
/** |
|
248
|
|
|
* @inheritdoc |
|
249
|
3 |
|
*/ |
|
250
|
|
|
public function getEncoderMediaType() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->encoderMediaType; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
1 |
|
/** |
|
256
|
|
|
* Sets response encoder media type |
|
257
|
1 |
|
* |
|
258
|
|
|
* @param MediaTypeInterface $encoderMediaType |
|
259
|
|
|
* @return $this |
|
260
|
|
|
*/ |
|
261
|
|
|
public function setEncoderMediaType(MediaTypeInterface $encoderMediaType) |
|
262
|
|
|
{ |
|
263
|
|
|
$this->encoderMediaType = $encoderMediaType; |
|
264
|
|
|
|
|
265
|
2 |
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
2 |
|
/** |
|
269
|
2 |
|
* @inheritdoc |
|
270
|
2 |
|
*/ |
|
271
|
2 |
|
public function setDecoder(DecoderInterface $decoder) |
|
272
|
|
|
{ |
|
273
|
2 |
|
$this->decoder = $decoder; |
|
274
|
|
|
|
|
275
|
2 |
|
return $this; |
|
276
|
2 |
|
} |
|
277
|
1 |
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @inheritdoc |
|
280
|
2 |
|
*/ |
|
281
|
2 |
|
public function getDecoder() |
|
282
|
2 |
|
{ |
|
283
|
|
|
return $this->decoder; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Sets environment configuration from array |
|
288
|
|
|
* |
|
289
|
|
|
* @param array $config |
|
290
|
|
|
*/ |
|
291
|
|
|
protected function fromArray(array $config) |
|
292
|
|
|
{ |
|
293
|
|
|
$fields = [ |
|
294
|
|
|
'query' => 'setQueryType', |
|
295
|
|
|
'body' => 'setBodyType', |
|
296
|
|
|
'matcher' => 'setMatcherConfiguration', |
|
297
|
|
|
'urlPrefix' => 'setUrlPrefix', |
|
298
|
|
|
'serialization' => 'setSerializationGroups', |
|
299
|
|
|
'validation' => 'setValidationGroups' |
|
300
|
|
|
]; |
|
301
|
|
|
|
|
302
|
|
|
foreach ($fields as $field => $setter) { |
|
303
|
|
|
if (!array_key_exists($field, $config)) { |
|
304
|
|
|
continue; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$this->{$setter}($config[$field]); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|