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