1 | <?php |
||
2 | |||
3 | namespace Pagantis\ModuleUtils\Model\Response; |
||
4 | |||
5 | use Nayjest\StrCaseConverter\Str; |
||
6 | |||
7 | /** |
||
8 | * Class AbstractJsonResponse |
||
9 | * @package Pagantis\ModuleUtils\Model\Response |
||
10 | */ |
||
11 | abstract class AbstractJsonResponse |
||
12 | { |
||
13 | /** |
||
14 | * @var int $timestamp |
||
15 | */ |
||
16 | protected $timestamp; |
||
17 | |||
18 | /** |
||
19 | * @var string $merchantOrderId |
||
20 | */ |
||
21 | protected $merchantOrderId; |
||
22 | |||
23 | /** |
||
24 | * @var string $pagantisOrderId |
||
25 | */ |
||
26 | protected $pagantisOrderId; |
||
27 | |||
28 | /** |
||
29 | * @var int $statusCode |
||
30 | */ |
||
31 | protected $statusCode; |
||
32 | |||
33 | /** |
||
34 | * JsonResponse constructor. |
||
35 | */ |
||
36 | public function __construct() |
||
37 | { |
||
38 | $this->timestamp = time(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return false|string |
||
43 | */ |
||
44 | public function toJson() |
||
45 | { |
||
46 | $response = $this->jsonSerialize(); |
||
47 | |||
48 | return json_encode($response, JSON_UNESCAPED_SLASHES); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return array |
||
53 | */ |
||
54 | public function jsonSerialize() |
||
55 | { |
||
56 | $arrayProperties = array(); |
||
57 | |||
58 | foreach ($this as $key => $value) { |
||
59 | $arrayProperties[Str::toSnakeCase($key)] = $value; |
||
60 | } |
||
61 | |||
62 | return $arrayProperties; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Post response |
||
67 | */ |
||
68 | public function printResponse() |
||
69 | { |
||
70 | header("HTTP/1.1 ".$this->getStatusCode(), true, $this->getStatusCode()); |
||
71 | header('Content-Type: application/json', true); |
||
72 | header('Content-Length: ' . strlen($this->toJson())); |
||
73 | echo ($this->toJson()); |
||
74 | exit(); |
||
0 ignored issues
–
show
|
|||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getTimestamp() |
||
81 | { |
||
82 | return $this->timestamp; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param int $timestamp |
||
87 | */ |
||
88 | public function setTimestamp($timestamp) |
||
89 | { |
||
90 | $this->timestamp = $timestamp; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getMerchantOrderId() |
||
97 | { |
||
98 | return $this->merchantOrderId; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param string $merchantOrderId |
||
103 | */ |
||
104 | public function setMerchantOrderId($merchantOrderId) |
||
105 | { |
||
106 | $this->merchantOrderId = $merchantOrderId; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getPagantisOrderId() |
||
113 | { |
||
114 | return $this->pagantisOrderId; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $pagantisOrderId |
||
119 | */ |
||
120 | public function setPagantisOrderId($pagantisOrderId) |
||
121 | { |
||
122 | $this->pagantisOrderId = $pagantisOrderId; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return int |
||
127 | */ |
||
128 | public function getStatusCode() |
||
129 | { |
||
130 | return $this->statusCode; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param int $statusCode |
||
135 | */ |
||
136 | public function setStatusCode($statusCode) |
||
137 | { |
||
138 | $this->statusCode = $statusCode; |
||
139 | } |
||
140 | } |
||
141 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.