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\Services\EnvironmentInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* JSON API environment |
20
|
|
|
* |
21
|
|
|
* @package Reva2\JsonApi\Services |
22
|
|
|
* @author Sergey Revenko <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Environment implements EnvironmentInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Expected query parameters type |
28
|
|
|
* |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
protected $queryType; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Expected body content type |
35
|
|
|
* |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected $bodyType; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Codec matcher configuration |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $matcherConfiguration; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* URLs prefix |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $urlPrefix = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Validation groups |
56
|
|
|
* |
57
|
|
|
* @var string[]|null |
58
|
|
|
*/ |
59
|
|
|
protected $validationGroups; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Response encoder |
63
|
|
|
* |
64
|
|
|
* @var EncoderInterface|null |
65
|
|
|
*/ |
66
|
|
|
protected $encoder; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Response encoder media type |
70
|
|
|
* |
71
|
|
|
* @var MediaTypeInterface|null |
72
|
|
|
*/ |
73
|
|
|
protected $encoderMediaType; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Constructor |
77
|
|
|
* |
78
|
|
|
* @param array|null $config |
79
|
|
|
*/ |
80
|
2 |
|
public function __construct(array $config = null) |
81
|
|
|
{ |
82
|
2 |
|
if (null !== $config) { |
83
|
2 |
|
$this->fromArray($config); |
84
|
2 |
|
} |
85
|
2 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
1 |
|
public function getQueryType() |
91
|
|
|
{ |
92
|
1 |
|
return $this->queryType; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets expected query parameters type |
97
|
|
|
* |
98
|
|
|
* @param null|string $queryType |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
1 |
|
public function setQueryType($queryType = null) |
102
|
|
|
{ |
103
|
1 |
|
$this->queryType = $queryType; |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
1 |
|
public function getBodyType() |
112
|
|
|
{ |
113
|
1 |
|
return $this->bodyType; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets expected body content type |
118
|
|
|
* |
119
|
|
|
* @param null|string $bodyType |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
1 |
|
public function setBodyType($bodyType = null) |
123
|
|
|
{ |
124
|
1 |
|
$this->bodyType = $bodyType; |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
1 |
|
public function getMatcherConfiguration() |
133
|
|
|
{ |
134
|
1 |
|
return $this->matcherConfiguration; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets codec matcher configuration |
139
|
|
|
* |
140
|
|
|
* @param array $matcherConfiguration |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
2 |
|
public function setMatcherConfiguration(array $matcherConfiguration) |
144
|
|
|
{ |
145
|
2 |
|
$this->matcherConfiguration = $matcherConfiguration; |
146
|
|
|
|
147
|
2 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
1 |
|
public function getUrlPrefix() |
154
|
|
|
{ |
155
|
1 |
|
return $this->urlPrefix; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Sets URLs prefix |
160
|
|
|
* |
161
|
|
|
* @param string $urlPrefix |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
1 |
|
public function setUrlPrefix($urlPrefix) |
165
|
|
|
{ |
166
|
1 |
|
$this->urlPrefix = $urlPrefix; |
167
|
|
|
|
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritdoc |
173
|
|
|
*/ |
174
|
1 |
|
public function getValidationGroups() |
175
|
|
|
{ |
176
|
1 |
|
return $this->validationGroups; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets validation groups |
181
|
|
|
* |
182
|
|
|
* @param array|null $validationGroups |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
2 |
|
public function setValidationGroups(array $validationGroups = null) |
186
|
|
|
{ |
187
|
2 |
|
$this->validationGroups = $validationGroups; |
188
|
|
|
|
189
|
2 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
public function getEncoder() |
196
|
|
|
{ |
197
|
|
|
return $this->encoder; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets response encoder |
202
|
|
|
* |
203
|
|
|
* @param EncoderInterface $encoder |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function setEncoder(EncoderInterface $encoder) |
207
|
|
|
{ |
208
|
|
|
$this->encoder = $encoder; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritdoc |
215
|
|
|
*/ |
216
|
|
|
public function getEncoderMediaType() |
217
|
|
|
{ |
218
|
|
|
return $this->encoderMediaType; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Sets response encoder media type |
223
|
|
|
* |
224
|
|
|
* @param MediaTypeInterface $encoderMediaType |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function setEncoderMediaType(MediaTypeInterface $encoderMediaType) |
228
|
|
|
{ |
229
|
|
|
$this->encoderMediaType = $encoderMediaType; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Sets environment configuration from array |
236
|
|
|
* |
237
|
|
|
* @param array $config |
238
|
|
|
*/ |
239
|
2 |
|
protected function fromArray(array $config) |
240
|
|
|
{ |
241
|
|
|
$fields = [ |
242
|
2 |
|
'query' => 'setQueryType', |
243
|
2 |
|
'body' => 'setBodyType', |
244
|
2 |
|
'matcher' => 'setMatcherConfiguration', |
245
|
2 |
|
'urlPrefix' => 'setUrlPrefix', |
246
|
|
|
'validation' => 'setValidationGroups' |
247
|
2 |
|
]; |
248
|
|
|
|
249
|
2 |
|
foreach ($fields as $field => $setter) { |
250
|
2 |
|
if (!array_key_exists($field, $config)) { |
251
|
1 |
|
continue; |
252
|
|
|
} |
253
|
|
|
|
254
|
2 |
|
$this->{$setter}($config[$field]); |
255
|
2 |
|
} |
256
|
2 |
|
} |
257
|
|
|
} |
258
|
|
|
|