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\DtoTrait; |
12
|
|
|
use Sf4\Api\Request\RequestTrait; |
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
14
|
|
|
|
15
|
|
|
abstract class AbstractResponse implements ResponseInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
use DtoTrait; |
19
|
|
|
use RequestTrait; |
20
|
|
|
|
21
|
|
|
/** @var array $responseData */ |
22
|
|
|
protected $responseData; |
23
|
|
|
|
24
|
|
|
/** @var int $responseStatus */ |
25
|
|
|
protected $responseStatus; |
26
|
|
|
|
27
|
|
|
/** @var array $responseHeaders */ |
28
|
|
|
protected $responseHeaders; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->createJsonResponse([], 200, static::HEADERS); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $data |
37
|
|
|
* @param int $status |
38
|
|
|
* @param array $headers |
39
|
|
|
*/ |
40
|
|
|
protected function createJsonResponse(array $data, int $status = 200, array $headers = self::HEADERS) |
41
|
|
|
{ |
42
|
|
|
$this->setResponseData($data); |
43
|
|
|
$this->setResponseStatus($status); |
44
|
|
|
$this->setResponseHeaders($headers); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public abstract function init(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return JsonResponse |
51
|
|
|
*/ |
52
|
|
|
public function getJsonResponse(): JsonResponse |
53
|
|
|
{ |
54
|
|
|
$response = new JsonResponse( |
55
|
|
|
$this->getResponseData(), |
56
|
|
|
$this->getResponseStatus(), |
57
|
|
|
$this->getResponseHeaders() |
58
|
|
|
); |
59
|
|
|
$request = $this->getRequest()->getRequest(); |
60
|
|
|
$response->headers->set( |
61
|
|
|
'Access-Control-Allow-Origin', |
62
|
|
|
$request->headers->get('Origin') |
63
|
|
|
); |
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getResponseData(): array |
71
|
|
|
{ |
72
|
|
|
return $this->responseData; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $responseData |
77
|
|
|
*/ |
78
|
|
|
public function setResponseData(array $responseData): void |
79
|
|
|
{ |
80
|
|
|
$this->responseData = $responseData; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function getResponseStatus(): int |
87
|
|
|
{ |
88
|
|
|
return $this->responseStatus; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $responseStatus |
93
|
|
|
*/ |
94
|
|
|
public function setResponseStatus($responseStatus): void |
95
|
|
|
{ |
96
|
|
|
$this->responseStatus = $responseStatus; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getResponseHeaders(): array |
103
|
|
|
{ |
104
|
|
|
return $this->responseHeaders; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $responseHeaders |
109
|
|
|
*/ |
110
|
|
|
public function setResponseHeaders(array $responseHeaders): void |
111
|
|
|
{ |
112
|
|
|
$this->responseHeaders = $responseHeaders; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|