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 Sf4\Api\Dto\DtoInterface; |
12
|
|
|
use Sf4\Api\Dto\Response\EmptyDto; |
13
|
|
|
use Sf4\Api\Dto\Traits\CreateErrorDtoTrait; |
14
|
|
|
use Sf4\Api\Dto\Traits\DtoTrait; |
15
|
|
|
use Sf4\Api\Repository\AbstractRepository; |
16
|
|
|
use Sf4\Api\Request\RequestTrait; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
|
19
|
|
|
abstract class AbstractResponse implements ResponseInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use DtoTrait; |
23
|
|
|
use RequestTrait; |
24
|
|
|
use CreateErrorDtoTrait; |
25
|
|
|
|
26
|
|
|
/** @var DtoInterface $responseDto */ |
27
|
|
|
protected $responseDto; |
28
|
|
|
|
29
|
|
|
/** @var int $responseStatus */ |
30
|
|
|
protected $responseStatus; |
31
|
|
|
|
32
|
|
|
/** @var array $responseHeaders */ |
33
|
|
|
protected $responseHeaders; |
34
|
|
|
|
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->createJsonResponse(new EmptyDto(), 200, static::HEADERS); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param DtoInterface $data |
42
|
|
|
* @param int $status |
43
|
|
|
* @param array $headers |
44
|
|
|
*/ |
45
|
|
|
protected function createJsonResponse(DtoInterface $data, int $status = 200, array $headers = self::HEADERS) |
46
|
|
|
{ |
47
|
|
|
$this->setResponseDto($data); |
48
|
|
|
$this->setResponseStatus($status); |
49
|
|
|
$this->setResponseHeaders($headers); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
abstract public function init(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return JsonResponse |
56
|
|
|
*/ |
57
|
|
|
public function getJsonResponse(): JsonResponse |
58
|
|
|
{ |
59
|
|
|
$response = new JsonResponse( |
60
|
|
|
$this->getResponseDto()->toArray(), |
61
|
|
|
$this->getResponseStatus(), |
62
|
|
|
$this->getResponseHeaders() |
63
|
|
|
); |
64
|
|
|
$request = $this->getRequest()->getRequest(); |
65
|
|
|
$response->headers->set( |
66
|
|
|
'Access-Control-Allow-Origin', |
67
|
|
|
$request->headers->get('Origin') |
68
|
|
|
); |
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return DtoInterface |
74
|
|
|
*/ |
75
|
|
|
public function getResponseDto(): DtoInterface |
76
|
|
|
{ |
77
|
|
|
return $this->responseDto; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param DtoInterface $responseDto |
82
|
|
|
*/ |
83
|
|
|
public function setResponseDto(DtoInterface $responseDto): void |
84
|
|
|
{ |
85
|
|
|
$this->responseDto = $responseDto; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getResponseStatus(): int |
92
|
|
|
{ |
93
|
|
|
return $this->responseStatus; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $responseStatus |
98
|
|
|
*/ |
99
|
|
|
public function setResponseStatus($responseStatus): void |
100
|
|
|
{ |
101
|
|
|
$this->responseStatus = $responseStatus; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getResponseHeaders(): array |
108
|
|
|
{ |
109
|
|
|
return $this->responseHeaders; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $responseHeaders |
114
|
|
|
*/ |
115
|
|
|
public function setResponseHeaders(array $responseHeaders): void |
116
|
|
|
{ |
117
|
|
|
$this->responseHeaders = $responseHeaders; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $entityClass |
122
|
|
|
* @return AbstractRepository|null |
123
|
|
|
*/ |
124
|
|
|
public function getRepository(string $entityClass): ?AbstractRepository |
125
|
|
|
{ |
126
|
|
|
$request = $this->getRequest(); |
127
|
|
|
if (!$request) { |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
$requestHandler = $request->getRequestHandler(); |
131
|
|
|
$manager = $requestHandler->getEntityManager(); |
132
|
|
|
|
133
|
|
|
$repository = $manager->getRepository($entityClass); |
134
|
|
|
if ($repository instanceof AbstractRepository) { |
135
|
|
|
return $repository; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $id |
143
|
|
|
* @param array $parameters |
144
|
|
|
* @param null $domain |
|
|
|
|
145
|
|
|
* @param null $locale |
|
|
|
|
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function translate($id, array $parameters = array(), $domain = null, $locale = null) |
149
|
|
|
{ |
150
|
|
|
$translator = $this->getRequest()->getRequestHandler()->getTranslator(); |
151
|
|
|
|
152
|
|
|
return $translator->trans($id, $parameters, $domain, $locale); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param DtoInterface $dto |
157
|
|
|
* @param array $data |null |
158
|
|
|
* @return DtoInterface|\Sf4\Api\Dto\Response\ErrorDto |
159
|
|
|
*/ |
160
|
|
|
protected function populateDto(DtoInterface $dto, ?array $data) |
161
|
|
|
{ |
162
|
|
|
if ($data) { |
|
|
|
|
163
|
|
|
try { |
164
|
|
|
$dto->populate($data); |
165
|
|
|
} catch (\ReflectionException $e) { |
166
|
|
|
$dto = $this->createErrorDtoTrait($e); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
return $dto; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|