|
1
|
|
|
<?php namespace Pz\Doctrine\Rest\Response; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
4
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
5
|
|
|
use League\Fractal\Manager; |
|
6
|
|
|
use League\Fractal\Pagination\DoctrinePaginatorAdapter; |
|
7
|
|
|
use League\Fractal\Resource\Collection; |
|
8
|
|
|
use League\Fractal\Resource\Item; |
|
9
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
|
10
|
|
|
use League\Fractal\TransformerAbstract; |
|
11
|
|
|
|
|
12
|
|
|
use Pz\Doctrine\Rest\Request\CreateRequestInterface; |
|
13
|
|
|
use Pz\Doctrine\Rest\Request\DeleteRequestInterface; |
|
14
|
|
|
use Pz\Doctrine\Rest\Request\IndexRequestInterface; |
|
15
|
|
|
use Pz\Doctrine\Rest\Request\ShowRequestInterface; |
|
16
|
|
|
use Pz\Doctrine\Rest\Request\UpdateRequestInterface; |
|
17
|
|
|
use Pz\Doctrine\Rest\RestException; |
|
18
|
|
|
use Pz\Doctrine\Rest\RestRequestInterface; |
|
19
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
|
20
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class FractalResponse implements RestResponseFactory |
|
24
|
|
|
{ |
|
25
|
|
|
const JSON_API_CONTENT_TYPE = 'application/vnd.api+json'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TransformerAbstract |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $transformer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param TransformerAbstract|null $transformer |
|
34
|
|
|
* |
|
35
|
|
|
* @return TransformerAbstract |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function transformer(TransformerAbstract $transformer = null) |
|
38
|
|
|
{ |
|
39
|
4 |
|
if ($transformer !== null) { |
|
40
|
|
|
$this->transformer = $transformer; |
|
41
|
|
|
return $this; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
return $this->transformer; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param IndexRequestInterface $request |
|
49
|
|
|
* @param QueryBuilder $qb |
|
50
|
|
|
* |
|
51
|
|
|
* @return RestResponse |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function index(IndexRequestInterface $request, QueryBuilder $qb) |
|
54
|
|
|
{ |
|
55
|
2 |
|
$resource = new Collection($paginator = new Paginator($qb), $this->transformer()); |
|
56
|
2 |
|
$resource->setPaginator(new DoctrinePaginatorAdapter($paginator, $this->getPaginatorRouteGenerator())); |
|
57
|
|
|
|
|
58
|
2 |
|
return $this->response( |
|
59
|
2 |
|
$this->fractal($request) |
|
60
|
2 |
|
->createData($resource) |
|
61
|
2 |
|
->toArray() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ShowRequestInterface $request |
|
67
|
|
|
* @param $entity |
|
68
|
|
|
* |
|
69
|
|
|
* @return RestResponse |
|
70
|
|
|
*/ |
|
71
|
|
|
public function show(ShowRequestInterface $request, $entity) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->response( |
|
74
|
|
|
$this->fractal($request) |
|
75
|
|
|
->createData(new Item($entity, $this->transformer())) |
|
76
|
|
|
->toArray() |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param CreateRequestInterface $request |
|
82
|
|
|
* @param $entity |
|
83
|
|
|
* |
|
84
|
|
|
* @return RestResponse |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function create(CreateRequestInterface $request, $entity) |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->response( |
|
89
|
1 |
|
$this->fractal($request) |
|
90
|
1 |
|
->createData(new Item($entity, $this->transformer())) |
|
91
|
1 |
|
->toArray() |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param UpdateRequestInterface $request |
|
97
|
|
|
* @param $entity |
|
98
|
|
|
* |
|
99
|
|
|
* @return RestResponse |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function update(UpdateRequestInterface $request, $entity) |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->response( |
|
104
|
1 |
|
$this->fractal($request) |
|
105
|
1 |
|
->createData(new Item($entity, $this->transformer())) |
|
106
|
1 |
|
->toArray() |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param DeleteRequestInterface $request |
|
112
|
|
|
* @param $entity |
|
113
|
|
|
* |
|
114
|
|
|
* @return RestResponse |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function delete(DeleteRequestInterface $request, $entity) |
|
117
|
|
|
{ |
|
118
|
1 |
|
return $this->response(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param RestRequestInterface $request |
|
123
|
|
|
* |
|
124
|
|
|
* @return RestResponse |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function notFound(RestRequestInterface $request) |
|
127
|
|
|
{ |
|
128
|
3 |
|
return $this->response(null, RestResponse::HTTP_NOT_FOUND); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param \Error|\Exception|\Pz\Doctrine\Rest\RestException $exception |
|
133
|
|
|
* |
|
134
|
|
|
* @return RestResponse |
|
135
|
|
|
* @throws \Error|\Exception|RestException |
|
136
|
|
|
*/ |
|
137
|
6 |
|
public function exception($exception) |
|
138
|
|
|
{ |
|
139
|
6 |
|
$message = $exception->getMessage(); |
|
140
|
|
|
|
|
141
|
|
|
switch (true) { |
|
142
|
6 |
|
case ($exception instanceof RestException): |
|
143
|
5 |
|
$httpStatus = $exception->httpStatus(); |
|
144
|
5 |
|
$errors = $exception->errors(); |
|
145
|
5 |
|
break; |
|
146
|
|
|
|
|
147
|
|
|
default: |
|
148
|
1 |
|
throw $exception; |
|
149
|
|
|
// $httpStatus = RestResponse::HTTP_INTERNAL_SERVER_ERROR; |
|
150
|
|
|
// $errors = $exception->getTrace(); |
|
151
|
|
|
break; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
5 |
|
return $this->response(['message' => $message, 'errors' => $errors], $httpStatus); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return configured fractal by request format. |
|
159
|
|
|
* |
|
160
|
|
|
* @param RestRequestInterface $request |
|
161
|
|
|
* |
|
162
|
|
|
* @return Manager |
|
163
|
|
|
*/ |
|
164
|
4 |
|
protected function fractal(RestRequestInterface $request) |
|
165
|
|
|
{ |
|
166
|
4 |
|
$fractal = new Manager(); |
|
167
|
|
|
|
|
168
|
4 |
|
if (in_array(static::JSON_API_CONTENT_TYPE, $request->http()->getAcceptableContentTypes())) { |
|
169
|
1 |
|
$fractal->setSerializer(new JsonApiSerializer()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
4 |
|
if ($includes = $request->http()->get('include')) { |
|
173
|
1 |
|
$fractal->parseIncludes($includes); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
4 |
|
if ($excludes = $request->http()->get('exclude')) { |
|
177
|
1 |
|
$fractal->parseExcludes($excludes); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
4 |
|
return $fractal; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param mixed $data |
|
185
|
|
|
* @param int $httStatus |
|
186
|
|
|
* |
|
187
|
|
|
* @return RestResponse |
|
188
|
|
|
*/ |
|
189
|
11 |
|
protected function response($data = null, $httStatus = RestResponse::HTTP_OK) |
|
190
|
|
|
{ |
|
191
|
11 |
|
return new RestResponse($data, $httStatus); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return \Closure |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function getPaginatorRouteGenerator() |
|
198
|
|
|
{ |
|
199
|
2 |
|
return function() { |
|
200
|
2 |
|
return null; |
|
201
|
2 |
|
}; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|