28
28
use CloudCreativity \LaravelJsonApi \Encoder \Neomerx \Factory ;
29
29
use CloudCreativity \LaravelJsonApi \Routing \Route ;
30
30
use CloudCreativity \LaravelJsonApi \Utils \Helpers ;
31
+ use Illuminate \Http \RedirectResponse ;
31
32
use Illuminate \Http \Response ;
33
+ use InvalidArgumentException ;
32
34
use Neomerx \JsonApi \Contracts \Document \DocumentInterface ;
33
35
use Neomerx \JsonApi \Contracts \Document \ErrorInterface ;
34
36
use Neomerx \JsonApi \Contracts \Encoder \Parameters \EncodingParametersInterface ;
@@ -115,7 +117,7 @@ public function withCodec(Codec $codec): self
115
117
public function withMediaType (string $ mediaType ): self
116
118
{
117
119
if (!$ encoding = $ this ->api ->getEncodings ()->find ($ mediaType )) {
118
- throw new \ InvalidArgumentException (
120
+ throw new InvalidArgumentException (
119
121
"Media type {$ mediaType } is not valid for API {$ this ->api ->getName ()}. "
120
122
);
121
123
}
@@ -135,13 +137,13 @@ public function withMediaType(string $mediaType): self
135
137
* @param int $options
136
138
* @param int $depth
137
139
* @param string|null $mediaType
138
- * @return Responses
140
+ * @return $this
139
141
*/
140
142
public function withEncoding (
141
143
int $ options = 0 ,
142
144
int $ depth = 512 ,
143
145
string $ mediaType = MediaTypeInterface::JSON_API_MEDIA_TYPE
144
- ) {
146
+ ): self {
145
147
$ encoding = Encoding::create (
146
148
$ mediaType ,
147
149
$ options ,
@@ -174,18 +176,18 @@ public function withEncodingParameters(?EncodingParametersInterface $parameters)
174
176
/**
175
177
* @param $statusCode
176
178
* @param array $headers
177
- * @return mixed
179
+ * @return Response
178
180
*/
179
- public function statusCode ($ statusCode , array $ headers = [])
181
+ public function statusCode ($ statusCode , array $ headers = []): Response
180
182
{
181
183
return $ this ->getCodeResponse ($ statusCode , $ headers );
182
184
}
183
185
184
186
/**
185
187
* @param array $headers
186
- * @return mixed
188
+ * @return Response
187
189
*/
188
- public function noContent (array $ headers = [])
190
+ public function noContent (array $ headers = []): Response
189
191
{
190
192
return $ this ->getCodeResponse (204 , $ headers );
191
193
}
@@ -194,9 +196,9 @@ public function noContent(array $headers = [])
194
196
* @param $meta
195
197
* @param int $statusCode
196
198
* @param array $headers
197
- * @return mixed
199
+ * @return Response
198
200
*/
199
- public function meta ($ meta , $ statusCode = self ::HTTP_OK , array $ headers = [])
201
+ public function meta ($ meta , $ statusCode = self ::HTTP_OK , array $ headers = []): Response
200
202
{
201
203
return $ this ->getMetaResponse ($ meta , $ statusCode , $ headers );
202
204
}
@@ -206,9 +208,9 @@ public function meta($meta, $statusCode = self::HTTP_OK, array $headers = [])
206
208
* @param $meta
207
209
* @param int $statusCode
208
210
* @param array $headers
209
- * @return mixed
211
+ * @return Response
210
212
*/
211
- public function noData (array $ links = [], $ meta = null , $ statusCode = self ::HTTP_OK , array $ headers = [])
213
+ public function noData (array $ links = [], $ meta = null , $ statusCode = self ::HTTP_OK , array $ headers = []): Response
212
214
{
213
215
$ encoder = $ this ->getEncoder ();
214
216
$ content = $ encoder ->withLinks ($ links )->encodeMeta ($ meta ?: []);
@@ -222,30 +224,38 @@ public function noData(array $links = [], $meta = null, $statusCode = self::HTTP
222
224
* @param mixed $meta
223
225
* @param int $statusCode
224
226
* @param array $headers
225
- * @return mixed
227
+ * @return Response
226
228
*/
227
229
public function content (
228
230
$ data ,
229
231
array $ links = [],
230
232
$ meta = null ,
231
233
$ statusCode = self ::HTTP_OK ,
232
234
array $ headers = []
233
- ) {
235
+ ): Response {
234
236
return $ this ->getContentResponse ($ data , $ statusCode , $ links , $ meta , $ headers );
235
237
}
236
238
239
+
237
240
/**
238
- * @inheritdoc
241
+ * Get response with regular JSON API Document in body.
242
+ *
243
+ * @param array|object $data
244
+ * @param int $statusCode
245
+ * @param null $links
246
+ * @param null $meta
247
+ * @param array $headers
248
+ * @return Response
239
249
*/
240
250
public function getContentResponse (
241
251
$ data ,
242
252
$ statusCode = self ::HTTP_OK ,
243
253
$ links = null ,
244
254
$ meta = null ,
245
255
array $ headers = []
246
- ) {
256
+ ): Response {
247
257
if ($ data instanceof PageInterface) {
248
- list ( $ data , $ meta , $ links) = $ this ->extractPage ($ data , $ meta , $ links );
258
+ [ $ data , $ meta , $ links] = $ this ->extractPage ($ data , $ meta , $ links );
249
259
}
250
260
251
261
return parent ::getContentResponse ($ data , $ statusCode , $ links , $ meta , $ headers );
@@ -256,9 +266,9 @@ public function getContentResponse(
256
266
* @param array $links
257
267
* @param mixed $meta
258
268
* @param array $headers
259
- * @return mixed
269
+ * @return Response
260
270
*/
261
- public function created ($ resource = null , array $ links = [], $ meta = null , array $ headers = [])
271
+ public function created ($ resource = null , array $ links = [], $ meta = null , array $ headers = []): Response
262
272
{
263
273
if ($ this ->isNoContent ($ resource , $ links , $ meta )) {
264
274
return $ this ->noContent ();
@@ -282,14 +292,14 @@ public function created($resource = null, array $links = [], $meta = null, array
282
292
* @param array $links
283
293
* @param mixed $meta
284
294
* @param array $headers
285
- * @return mixed
295
+ * @return Response
286
296
*/
287
297
public function updated (
288
298
$ resource = null ,
289
299
array $ links = [],
290
300
$ meta = null ,
291
301
array $ headers = []
292
- ) {
302
+ ): Response {
293
303
return $ this ->getResourceResponse ($ resource , $ links , $ meta , $ headers );
294
304
}
295
305
@@ -300,14 +310,14 @@ public function updated(
300
310
* @param array $links
301
311
* @param mixed|null $meta
302
312
* @param array $headers
303
- * @return mixed
313
+ * @return Response
304
314
*/
305
315
public function deleted (
306
316
$ resource = null ,
307
317
array $ links = [],
308
318
$ meta = null ,
309
319
array $ headers = []
310
- ) {
320
+ ): Response {
311
321
return $ this ->getResourceResponse ($ resource , $ links , $ meta , $ headers );
312
322
}
313
323
@@ -316,9 +326,9 @@ public function deleted(
316
326
* @param array $links
317
327
* @param null $meta
318
328
* @param array $headers
319
- * @return mixed
329
+ * @return Response
320
330
*/
321
- public function accepted (AsynchronousProcess $ job , array $ links = [], $ meta = null , array $ headers = [])
331
+ public function accepted (AsynchronousProcess $ job , array $ links = [], $ meta = null , array $ headers = []): Response
322
332
{
323
333
$ headers ['Content-Location ' ] = $ this ->getResourceLocationUrl ($ job );
324
334
@@ -330,7 +340,7 @@ public function accepted(AsynchronousProcess $job, array $links = [], $meta = nu
330
340
* @param array $links
331
341
* @param null $meta
332
342
* @param array $headers
333
- * @return \Illuminate\Http\ RedirectResponse|mixed
343
+ * @return RedirectResponse|mixed
334
344
*/
335
345
public function process (AsynchronousProcess $ job , array $ links = [], $ meta = null , array $ headers = [])
336
346
{
@@ -348,15 +358,15 @@ public function process(AsynchronousProcess $job, array $links = [], $meta = nul
348
358
* @param mixed $meta
349
359
* @param int $statusCode
350
360
* @param array $headers
351
- * @return mixed
361
+ * @return Response
352
362
*/
353
363
public function relationship (
354
364
$ data ,
355
365
array $ links = [],
356
366
$ meta = null ,
357
367
$ statusCode = 200 ,
358
368
array $ headers = []
359
- ) {
369
+ ): Response {
360
370
return $ this ->getIdentifiersResponse ($ data , $ statusCode , $ links , $ meta , $ headers );
361
371
}
362
372
@@ -366,17 +376,17 @@ public function relationship(
366
376
* @param $links
367
377
* @param $meta
368
378
* @param array $headers
369
- * @return mixed
379
+ * @return Response
370
380
*/
371
381
public function getIdentifiersResponse (
372
382
$ data ,
373
383
$ statusCode = self ::HTTP_OK ,
374
384
$ links = null ,
375
385
$ meta = null ,
376
386
array $ headers = []
377
- ) {
387
+ ): Response {
378
388
if ($ data instanceof PageInterface) {
379
- list ( $ data , $ meta , $ links) = $ this ->extractPage ($ data , $ meta , $ links );
389
+ [ $ data , $ meta , $ links] = $ this ->extractPage ($ data , $ meta , $ links );
380
390
}
381
391
382
392
return parent ::getIdentifiersResponse ($ data , $ statusCode , $ links , $ meta , $ headers );
@@ -388,9 +398,9 @@ public function getIdentifiersResponse(
388
398
* @param Error|ErrorInterface|array $error
389
399
* @param int|null $defaultStatusCode
390
400
* @param array $headers
391
- * @return mixed
401
+ * @return Response
392
402
*/
393
- public function error ($ error , int $ defaultStatusCode = null , array $ headers = [])
403
+ public function error ($ error , int $ defaultStatusCode = null , array $ headers = []): Response
394
404
{
395
405
if (!$ error instanceof ErrorInterface) {
396
406
$ error = $ this ->factory ->createError (
@@ -411,9 +421,10 @@ public function error($error, int $defaultStatusCode = null, array $headers = []
411
421
* @param iterable $errors
412
422
* @param int|null $defaultStatusCode
413
423
* @param array $headers
414
- * @return mixed
424
+ *
425
+ * @return Response
415
426
*/
416
- public function errors (iterable $ errors , int $ defaultStatusCode = null , array $ headers = [])
427
+ public function errors (iterable $ errors , int $ defaultStatusCode = null , array $ headers = []): Response
417
428
{
418
429
$ errors = $ this ->factory ->createErrors ($ errors );
419
430
$ statusCode = Helpers::httpErrorStatus ($ errors , $ defaultStatusCode );
@@ -426,9 +437,9 @@ public function errors(iterable $errors, int $defaultStatusCode = null, array $h
426
437
* @param array $links
427
438
* @param null $meta
428
439
* @param array $headers
429
- * @return mixed
440
+ * @return Response
430
441
*/
431
- protected function getResourceResponse ($ resource , array $ links = [], $ meta = null , array $ headers = [])
442
+ protected function getResourceResponse ($ resource , array $ links = [], $ meta = null , array $ headers = []): Response
432
443
{
433
444
if ($ this ->isNoContent ($ resource , $ links , $ meta )) {
434
445
return $ this ->noContent ();
@@ -518,9 +529,15 @@ protected function getSupportedExtensions()
518
529
}
519
530
520
531
/**
521
- * @inheritdoc
532
+ * Create HTTP response.
533
+ *
534
+ * @param string|null $content
535
+ * @param int $statusCode
536
+ * @param array $headers
537
+ *
538
+ * @return Response
522
539
*/
523
- protected function createResponse ($ content , $ statusCode , array $ headers )
540
+ protected function createResponse ($ content , $ statusCode , array $ headers ): Response
524
541
{
525
542
return response ($ content , $ statusCode , $ headers );
526
543
}
0 commit comments