1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the reva2/jsonapi. |
4
|
|
|
* |
5
|
|
|
* (c) OrbitScripts LLC <[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
|
|
|
* Validation groups |
57
|
|
|
* |
58
|
|
|
* @var string[]|null |
59
|
|
|
*/ |
60
|
|
|
protected $validationGroups; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Request decoder |
64
|
|
|
* |
65
|
|
|
* @var DecoderInterface|null |
66
|
|
|
*/ |
67
|
|
|
protected $decoder; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Response encoder |
71
|
|
|
* |
72
|
|
|
* @var EncoderInterface|null |
73
|
|
|
*/ |
74
|
|
|
protected $encoder; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Response encoder media type |
78
|
|
|
* |
79
|
|
|
* @var MediaTypeInterface|null |
80
|
|
|
*/ |
81
|
|
|
protected $encoderMediaType; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Constructor |
85
|
|
|
* |
86
|
|
|
* @param array|null $config |
87
|
|
|
*/ |
88
|
2 |
|
public function __construct(array $config = null) |
89
|
|
|
{ |
90
|
2 |
|
if (null !== $config) { |
91
|
2 |
|
$this->fromArray($config); |
92
|
2 |
|
} |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
1 |
|
public function getQueryType() |
99
|
|
|
{ |
100
|
1 |
|
return $this->queryType; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets expected query parameters type |
105
|
|
|
* |
106
|
|
|
* @param null|string $queryType |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
1 |
|
public function setQueryType($queryType = null) |
110
|
|
|
{ |
111
|
1 |
|
$this->queryType = $queryType; |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
1 |
|
public function getBodyType() |
120
|
|
|
{ |
121
|
1 |
|
return $this->bodyType; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets expected body content type |
126
|
|
|
* |
127
|
|
|
* @param null|string $bodyType |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
1 |
|
public function setBodyType($bodyType = null) |
131
|
|
|
{ |
132
|
1 |
|
$this->bodyType = $bodyType; |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
1 |
|
public function getMatcherConfiguration() |
141
|
|
|
{ |
142
|
1 |
|
return $this->matcherConfiguration; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Sets codec matcher configuration |
147
|
|
|
* |
148
|
|
|
* @param array $matcherConfiguration |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
2 |
|
public function setMatcherConfiguration(array $matcherConfiguration) |
152
|
|
|
{ |
153
|
2 |
|
$this->matcherConfiguration = $matcherConfiguration; |
154
|
|
|
|
155
|
2 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritdoc |
160
|
|
|
*/ |
161
|
1 |
|
public function getUrlPrefix() |
162
|
|
|
{ |
163
|
1 |
|
return $this->urlPrefix; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Sets URLs prefix |
168
|
|
|
* |
169
|
|
|
* @param string $urlPrefix |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
1 |
|
public function setUrlPrefix($urlPrefix) |
173
|
|
|
{ |
174
|
1 |
|
$this->urlPrefix = $urlPrefix; |
175
|
|
|
|
176
|
1 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritdoc |
181
|
|
|
*/ |
182
|
1 |
|
public function getValidationGroups() |
183
|
|
|
{ |
184
|
1 |
|
return $this->validationGroups; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Sets validation groups |
189
|
|
|
* |
190
|
|
|
* @param array|null $validationGroups |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
2 |
|
public function setValidationGroups(array $validationGroups = null) |
194
|
|
|
{ |
195
|
2 |
|
$this->validationGroups = $validationGroups; |
196
|
|
|
|
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
|
|
public function getEncoder() |
204
|
|
|
{ |
205
|
|
|
return $this->encoder; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Sets response encoder |
210
|
|
|
* |
211
|
|
|
* @param EncoderInterface $encoder |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
|
|
public function setEncoder(EncoderInterface $encoder) |
215
|
|
|
{ |
216
|
|
|
$this->encoder = $encoder; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @inheritdoc |
223
|
|
|
*/ |
224
|
|
|
public function getEncoderMediaType() |
225
|
|
|
{ |
226
|
|
|
return $this->encoderMediaType; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Sets response encoder media type |
231
|
|
|
* |
232
|
|
|
* @param MediaTypeInterface $encoderMediaType |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function setEncoderMediaType(MediaTypeInterface $encoderMediaType) |
236
|
|
|
{ |
237
|
|
|
$this->encoderMediaType = $encoderMediaType; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritdoc |
244
|
|
|
*/ |
245
|
|
|
public function setDecoder(DecoderInterface $decoder) |
246
|
|
|
{ |
247
|
|
|
$this->decoder = $decoder; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
255
|
|
|
public function getDecoder() |
256
|
|
|
{ |
257
|
|
|
return $this->decoder; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Sets environment configuration from array |
262
|
|
|
* |
263
|
|
|
* @param array $config |
264
|
|
|
*/ |
265
|
2 |
|
protected function fromArray(array $config) |
266
|
|
|
{ |
267
|
|
|
$fields = [ |
268
|
2 |
|
'query' => 'setQueryType', |
269
|
2 |
|
'body' => 'setBodyType', |
270
|
2 |
|
'matcher' => 'setMatcherConfiguration', |
271
|
2 |
|
'urlPrefix' => 'setUrlPrefix', |
272
|
|
|
'validation' => 'setValidationGroups' |
273
|
2 |
|
]; |
274
|
|
|
|
275
|
2 |
|
foreach ($fields as $field => $setter) { |
276
|
2 |
|
if (!array_key_exists($field, $config)) { |
277
|
1 |
|
continue; |
278
|
|
|
} |
279
|
|
|
|
280
|
2 |
|
$this->{$setter}($config[$field]); |
281
|
2 |
|
} |
282
|
2 |
|
} |
283
|
|
|
} |
284
|
|
|
|