1
|
|
|
<?php namespace Pz\Doctrine\Rest; |
2
|
|
|
|
3
|
|
|
use League\Fractal\Resource\ResourceInterface; |
4
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
5
|
|
|
use Pz\Doctrine\Rest\Fractal\JsonApiSerializer; |
6
|
|
|
use Pz\Doctrine\Rest\Fractal\Manager; |
7
|
|
|
use Pz\Doctrine\Rest\Fractal\ScopeFactory; |
8
|
|
|
|
9
|
|
|
class RestResponseFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Return configured fractal by request format. |
13
|
|
|
* |
14
|
|
|
* @param RestRequestContract $request |
15
|
|
|
* |
16
|
|
|
* @return Manager |
17
|
|
|
*/ |
18
|
18 |
|
protected static function fractal(RestRequestContract $request) |
19
|
|
|
{ |
20
|
18 |
|
$fractal = new Manager(new ScopeFactory(), $request); |
21
|
18 |
|
$fractal->setSerializer(new JsonApiSerializer($request)); |
22
|
|
|
|
23
|
18 |
|
if ($includes = $request->getInclude()) { |
24
|
5 |
|
$fractal->parseIncludes($includes); |
25
|
|
|
} |
26
|
|
|
|
27
|
18 |
|
if ($excludes = $request->getExclude()) { |
28
|
1 |
|
$fractal->parseExcludes($excludes); |
29
|
|
|
} |
30
|
|
|
|
31
|
18 |
|
if ($fields = $request->getFields()) { |
32
|
5 |
|
$fractal->parseFieldsets($fields); |
33
|
|
|
} |
34
|
|
|
|
35
|
18 |
|
return $fractal; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param RestRequestContract $request |
40
|
|
|
* @param ResourceInterface|null $resource |
41
|
|
|
* @param int $httStatus |
42
|
|
|
* @param array $headers |
43
|
|
|
* |
44
|
|
|
* @return RestResponse |
45
|
|
|
*/ |
46
|
18 |
|
public function resource( |
47
|
|
|
RestRequestContract $request, |
48
|
|
|
ResourceInterface $resource = null, |
49
|
|
|
int $httStatus = RestResponse::HTTP_OK, |
50
|
|
|
array $headers = [] |
51
|
|
|
) { |
52
|
18 |
|
$data = $resource ? $this->fractal($request)->createData($resource)->toArray() : null; |
53
|
|
|
|
54
|
17 |
|
return RestResponse::create($data, $httStatus, $headers); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return RestResponse |
59
|
|
|
*/ |
60
|
1 |
|
public function noContent() |
61
|
|
|
{ |
62
|
1 |
|
return RestResponse::noContent(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|