|
1
|
|
|
<?php namespace Pz\Doctrine\Rest\Response; |
|
2
|
|
|
|
|
3
|
|
|
use League\Fractal\Manager; |
|
4
|
|
|
use League\Fractal\Resource\Collection; |
|
5
|
|
|
use League\Fractal\Resource\Item; |
|
6
|
|
|
use League\Fractal\TransformerAbstract; |
|
7
|
|
|
|
|
8
|
|
|
use Pz\Doctrine\Rest\Action\Index\ResponseDataInterface; |
|
9
|
|
|
use Pz\Doctrine\Rest\Request\CreateRequestInterface; |
|
10
|
|
|
use Pz\Doctrine\Rest\Request\DeleteRequestInterface; |
|
11
|
|
|
use Pz\Doctrine\Rest\Request\IndexRequestInterface; |
|
12
|
|
|
use Pz\Doctrine\Rest\Request\ShowRequestInterface; |
|
13
|
|
|
use Pz\Doctrine\Rest\Request\UpdateRequestInterface; |
|
14
|
|
|
use Pz\Doctrine\Rest\RestRequestInterface; |
|
15
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
|
16
|
|
|
|
|
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
|
|
20
|
|
|
class FractalResponse implements RestResponseFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var TransformerAbstract |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $transformer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Manager|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $fractal; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* FractalResponse constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param TransformerAbstract $transformer |
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function __construct(TransformerAbstract $transformer) |
|
38
|
|
|
{ |
|
39
|
8 |
|
$this->transformer = $transformer; |
|
40
|
8 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param IndexRequestInterface $request |
|
44
|
|
|
* @param ResponseDataInterface $response |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function index(IndexRequestInterface $request, ResponseDataInterface $response) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$resource = new Collection($response->data(), $this->transformer()); |
|
51
|
1 |
|
$resource->setMetaValue('count', $response->count()); |
|
52
|
1 |
|
$resource->setMetaValue('limit', $response->limit()); |
|
53
|
1 |
|
$resource->setMetaValue('start', $response->start()); |
|
54
|
|
|
|
|
55
|
1 |
|
return $this->response( |
|
|
|
|
|
|
56
|
1 |
|
$this->fractal($request) |
|
57
|
1 |
|
->createData($resource) |
|
58
|
1 |
|
->toArray() |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param ShowRequestInterface $request |
|
64
|
|
|
* @param $entity |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function show(ShowRequestInterface $request, $entity) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->response( |
|
|
|
|
|
|
71
|
1 |
|
$this->fractal($request) |
|
72
|
1 |
|
->createData(new Item($entity, $this->transformer())) |
|
73
|
1 |
|
->toArray() |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param CreateRequestInterface $request |
|
79
|
|
|
* @param $entity |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function create(CreateRequestInterface $request, $entity) |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->response( |
|
|
|
|
|
|
86
|
1 |
|
$this->fractal($request) |
|
87
|
1 |
|
->createData(new Item($entity, $this->transformer())) |
|
88
|
1 |
|
->toArray() |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param UpdateRequestInterface $request |
|
94
|
|
|
* @param $entity |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function update(UpdateRequestInterface $request, $entity) |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->response( |
|
|
|
|
|
|
101
|
1 |
|
$this->fractal($request) |
|
102
|
1 |
|
->createData(new Item($entity, $this->transformer())) |
|
103
|
1 |
|
->toArray() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param DeleteRequestInterface $request |
|
109
|
|
|
* @param $entity |
|
110
|
|
|
* |
|
111
|
|
|
* @return JsonResponse |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function delete(DeleteRequestInterface $request, $entity) |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->response(); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param RestRequestInterface $request |
|
120
|
|
|
* |
|
121
|
|
|
* @return JsonResponse |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function notFound(RestRequestInterface $request) |
|
124
|
|
|
{ |
|
125
|
3 |
|
return $this->response(null, Response::HTTP_NOT_FOUND); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param int $httpStatus |
|
130
|
|
|
* @param string $message |
|
131
|
|
|
* @param array $errors |
|
132
|
|
|
* |
|
133
|
|
|
* @return JsonResponse |
|
134
|
|
|
*/ |
|
135
|
|
|
public function error($httpStatus, $message, $errors) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->response(['message' => $message, 'errors' => $errors,], $httpStatus); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Return configured fractal by request format. |
|
142
|
|
|
* |
|
143
|
|
|
* @param RestRequestInterface $request |
|
144
|
|
|
* |
|
145
|
|
|
* @return Manager |
|
146
|
|
|
*/ |
|
147
|
4 |
|
protected function fractal(/** @scrutinizer ignore-unused */ RestRequestInterface $request) |
|
148
|
|
|
{ |
|
149
|
4 |
|
if ($this->fractal === null) { |
|
150
|
4 |
|
$this->fractal = new Manager(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
4 |
|
return $this->fractal; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return TransformerAbstract |
|
158
|
|
|
*/ |
|
159
|
4 |
|
protected function transformer() |
|
160
|
|
|
{ |
|
161
|
4 |
|
return $this->transformer; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param null $data |
|
|
|
|
|
|
166
|
|
|
* @param int $httStatus |
|
167
|
|
|
* |
|
168
|
|
|
* @return JsonResponse |
|
169
|
|
|
*/ |
|
170
|
8 |
|
protected function response($data = null, $httStatus = Response::HTTP_OK) |
|
171
|
|
|
{ |
|
172
|
8 |
|
return new JsonResponse($data, $httStatus); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|