1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tomaj\NetteApi\Misc; |
6
|
|
|
|
7
|
|
|
class ConsoleResponse |
8
|
|
|
{ |
9
|
|
|
private $postFields; |
10
|
|
|
|
11
|
|
|
private $rawPost; |
12
|
|
|
|
13
|
|
|
private $getFields; |
14
|
|
|
|
15
|
|
|
private $cookieFields; |
16
|
|
|
|
17
|
|
|
private $url; |
18
|
|
|
|
19
|
|
|
private $method; |
20
|
|
|
|
21
|
|
|
private $headers; |
22
|
|
|
|
23
|
|
|
private $responseCode; |
24
|
|
|
|
25
|
|
|
private $responseBody; |
26
|
|
|
|
27
|
|
|
private $responseHeaders; |
28
|
|
|
|
29
|
|
|
private $responseTime; |
30
|
|
|
|
31
|
|
|
private $isError = false; |
32
|
|
|
|
33
|
|
|
private $errorNumber; |
34
|
|
|
|
35
|
|
|
private $errorMessage; |
36
|
|
|
|
37
|
9 |
|
public function __construct(string $url, string $method, array $postFields = [], array $getFields = [], array $cookieFields = [], array $headers = [], ?string $rawPost = null) |
38
|
|
|
{ |
39
|
9 |
|
$this->url = $url; |
40
|
9 |
|
$this->method = $method; |
41
|
9 |
|
$this->postFields = $postFields; |
42
|
9 |
|
$this->getFields = $getFields; |
43
|
9 |
|
$this->cookieFields = $cookieFields; |
44
|
9 |
|
$this->headers = $headers; |
45
|
9 |
|
$this->rawPost = $rawPost; |
46
|
9 |
|
} |
47
|
|
|
|
48
|
3 |
|
public function logRequest(int $responseCode, string $responseBody, string $responseHeaders, int $responseTime): void |
49
|
|
|
{ |
50
|
3 |
|
$this->responseCode = $responseCode; |
51
|
3 |
|
$this->responseBody = $responseBody; |
52
|
3 |
|
$this->responseHeaders = $responseHeaders; |
53
|
3 |
|
$this->responseTime = $responseTime; |
54
|
3 |
|
} |
55
|
|
|
|
56
|
6 |
|
public function logError(int $errorNumber, string $errorMessage, int $responseTime): void |
57
|
|
|
{ |
58
|
6 |
|
$this->isError = true; |
59
|
6 |
|
$this->errorNumber = $errorNumber; |
60
|
6 |
|
$this->errorMessage = $errorMessage; |
61
|
6 |
|
$this->responseTime = $responseTime; |
62
|
6 |
|
} |
63
|
|
|
|
64
|
6 |
|
public function getUrl(): string |
65
|
|
|
{ |
66
|
6 |
|
return $this->url; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function getMethod(): string |
70
|
|
|
{ |
71
|
3 |
|
return $this->method; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function getPostFields(): array |
75
|
|
|
{ |
76
|
3 |
|
return $this->postFields; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getRawPost(): ?string |
80
|
|
|
{ |
81
|
|
|
return $this->rawPost; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function getGetFields(): array |
85
|
|
|
{ |
86
|
3 |
|
return $this->getFields; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
public function getCookieFields(): array |
90
|
|
|
{ |
91
|
3 |
|
return $this->cookieFields; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public function getHeaders(): array |
95
|
|
|
{ |
96
|
3 |
|
return $this->headers; |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
public function isError(): bool |
100
|
|
|
{ |
101
|
9 |
|
return $this->isError; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
public function getResponseCode(): ?int |
105
|
|
|
{ |
106
|
3 |
|
return $this->responseCode; |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
public function getResponseBody(): string |
110
|
|
|
{ |
111
|
3 |
|
return $this->responseBody; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public function getFormattedJsonBody(): string |
115
|
|
|
{ |
116
|
3 |
|
$body = $this->responseBody; |
117
|
3 |
|
$decoded = json_decode($body); |
118
|
3 |
|
if ($decoded) { |
119
|
3 |
|
$body = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
120
|
|
|
} |
121
|
3 |
|
return $body; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
public function getResponseHeaders(): string |
125
|
|
|
{ |
126
|
3 |
|
return $this->responseHeaders; |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
public function getResponseTime(): int |
130
|
|
|
{ |
131
|
6 |
|
return $this->responseTime; |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
public function getErrorNumber(): int |
135
|
|
|
{ |
136
|
3 |
|
return $this->errorNumber; |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
public function getErrorMessage(): string |
140
|
|
|
{ |
141
|
3 |
|
return $this->errorMessage; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|