|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: siim |
|
5
|
|
|
* Date: 17.01.19 |
|
6
|
|
|
* Time: 9:19 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Sf4\Api\Response; |
|
10
|
|
|
|
|
11
|
|
|
use ReflectionException; |
|
12
|
|
|
use Sf4\Api\Dto\DtoInterface; |
|
13
|
|
|
use Sf4\Api\Dto\Response\EmptyDto; |
|
14
|
|
|
use Sf4\Api\Dto\Response\ErrorDto; |
|
15
|
|
|
use Sf4\Api\Dto\Traits\CreateErrorDtoTrait; |
|
16
|
|
|
use Sf4\Api\Dto\Traits\DtoTrait; |
|
17
|
|
|
use Sf4\Api\Repository\AbstractRepository; |
|
18
|
|
|
use Sf4\Api\Repository\RepositoryInterface; |
|
19
|
|
|
use Sf4\Api\Request\RequestTrait; |
|
20
|
|
|
use Sf4\Api\RequestHandler\RequestHandlerInterface; |
|
21
|
|
|
use Sf4\Api\Utils\Traits\TranslatorTrait; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
23
|
|
|
|
|
24
|
|
|
abstract class AbstractResponse implements ResponseInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
use DtoTrait; |
|
28
|
|
|
use RequestTrait; |
|
29
|
|
|
use CreateErrorDtoTrait; |
|
30
|
|
|
use TranslatorTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** @var DtoInterface $responseDto */ |
|
33
|
|
|
protected $responseDto; |
|
34
|
|
|
|
|
35
|
|
|
/** @var int $responseStatus */ |
|
36
|
|
|
protected $responseStatus; |
|
37
|
|
|
|
|
38
|
|
|
/** @var array $responseHeaders */ |
|
39
|
|
|
protected $responseHeaders; |
|
40
|
|
|
|
|
41
|
|
|
/** @var JsonResponse $jsonResponse */ |
|
42
|
|
|
protected $jsonResponse; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->addTranslator(); |
|
47
|
|
|
$this->createJsonResponse(new EmptyDto(), 200, static::HEADERS); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function addTranslator(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$requestHandler = $this->getRequestHandler(); |
|
53
|
|
|
if ($requestHandler) { |
|
54
|
|
|
$translator = $requestHandler->getTranslator(); |
|
55
|
|
|
$this->setTranslator($translator); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return RequestHandlerInterface|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getRequestHandler(): ?RequestHandlerInterface |
|
63
|
|
|
{ |
|
64
|
|
|
$request = $this->getRequest(); |
|
65
|
|
|
if ($request) { |
|
66
|
|
|
return $request->getRequestHandler(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param DtoInterface $data |
|
74
|
|
|
* @param int $status |
|
75
|
|
|
* @param array $headers |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function createJsonResponse(DtoInterface $data, int $status = 200, array $headers = self::HEADERS): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->setResponseDto($data); |
|
80
|
|
|
$this->setResponseStatus($status); |
|
81
|
|
|
$this->setResponseHeaders($headers); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
abstract public function init(); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return JsonResponse |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getJsonResponse(): JsonResponse |
|
90
|
|
|
{ |
|
91
|
|
|
if (!$this->jsonResponse) { |
|
92
|
|
|
$response = new JsonResponse( |
|
93
|
|
|
$this->getResponseDto()->toArray(), |
|
94
|
|
|
$this->getResponseStatus(), |
|
95
|
|
|
$this->getResponseHeaders() |
|
96
|
|
|
); |
|
97
|
|
|
} else { |
|
98
|
|
|
$response = $this->jsonResponse; |
|
99
|
|
|
} |
|
100
|
|
|
$request = $this->getRequest(); |
|
101
|
|
|
if ($request) { |
|
102
|
|
|
$httpRequest = $request->getRequest(); |
|
103
|
|
|
if ($httpRequest) { |
|
104
|
|
|
$response->headers->set( |
|
105
|
|
|
'Access-Control-Allow-Origin', |
|
106
|
|
|
$httpRequest->headers->get('Origin') |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
return $response; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param JsonResponse $jsonResponse |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setJsonResponse(JsonResponse $jsonResponse) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->jsonResponse = $jsonResponse; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return DtoInterface |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getResponseDto(): DtoInterface |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->responseDto; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param DtoInterface $responseDto |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setResponseDto(DtoInterface $responseDto): void |
|
133
|
|
|
{ |
|
134
|
|
|
$this->responseDto = $responseDto; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return int |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getResponseStatus(): int |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->responseStatus; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param int $responseStatus |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setResponseStatus($responseStatus): void |
|
149
|
|
|
{ |
|
150
|
|
|
$this->responseStatus = $responseStatus; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getResponseHeaders(): array |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->responseHeaders; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param array $responseHeaders |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setResponseHeaders(array $responseHeaders): void |
|
165
|
|
|
{ |
|
166
|
|
|
$this->responseHeaders = $responseHeaders; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $tableName |
|
171
|
|
|
* @return AbstractRepository|null |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getRepository(string $tableName): ?RepositoryInterface |
|
174
|
|
|
{ |
|
175
|
|
|
$requestHandler = $this->getRequestHandler(); |
|
176
|
|
|
if ($requestHandler) { |
|
177
|
|
|
$repositoryFactory = $requestHandler->getRepositoryFactory(); |
|
178
|
|
|
return $repositoryFactory->create($tableName); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return null; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param DtoInterface $dto |
|
186
|
|
|
* @param array $data |null |
|
187
|
|
|
* @return DtoInterface|ErrorDto |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function populateDto(DtoInterface $dto, ?array $data) |
|
190
|
|
|
{ |
|
191
|
|
|
if ($data) { |
|
|
|
|
|
|
192
|
|
|
try { |
|
193
|
|
|
$dto->populate($data); |
|
194
|
|
|
} catch (ReflectionException $e) { |
|
195
|
|
|
$dto = $this->createErrorDto($e); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
return $dto; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.