1 | <?php |
||
9 | class TelegramResponse |
||
10 | { |
||
11 | /** |
||
12 | * Nothing is done so far with this, but it's always a good idea to have the original around |
||
13 | * @var string |
||
14 | */ |
||
15 | private $rawData = ''; |
||
16 | |||
17 | /** |
||
18 | * The actual representation of the decoded data |
||
19 | * @var array |
||
20 | */ |
||
21 | private $decodedData = []; |
||
22 | |||
23 | /** |
||
24 | * The headers sent with the response. |
||
25 | * @var array |
||
26 | */ |
||
27 | private $headers = []; |
||
28 | |||
29 | /** |
||
30 | * @var \Exception |
||
31 | */ |
||
32 | private $exception = null; |
||
33 | |||
34 | /** |
||
35 | * Marks the actual response as an error |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $isError = false; |
||
39 | |||
40 | 41 | public function __construct(string $rawData, array $headers = [], \Exception $e = null) |
|
41 | { |
||
42 | 41 | $this->fillRawData($rawData); |
|
43 | 41 | $this->headers = $headers; |
|
44 | |||
45 | 41 | if (!is_null($e)) { |
|
46 | 1 | $this->exception = $e; |
|
47 | 1 | $this->isError = true; |
|
48 | } |
||
49 | 41 | } |
|
50 | |||
51 | /** |
||
52 | * Will return true if the request was an unsuccessful one, false otherwise |
||
53 | * @return bool |
||
54 | */ |
||
55 | 1 | public function isError(): bool |
|
56 | { |
||
57 | 1 | return $this->isError; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Fills in the raw data |
||
62 | * |
||
63 | * @param string $rawData |
||
64 | * @return TelegramResponse |
||
65 | */ |
||
66 | 41 | public function fillRawData(string $rawData): TelegramResponse |
|
73 | |||
74 | /** |
||
75 | * To quickly find out what type of request we are dealing with |
||
76 | * |
||
77 | * Unused so far |
||
78 | * |
||
79 | * @return string |
||
80 | * @throws InvalidResultType |
||
81 | */ |
||
82 | 8 | public function getTypeOfResult(): string |
|
95 | |||
96 | /** |
||
97 | * Most of the requests Telegram sends, come as an array, so send the response back as an array by default |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 23 | public function getResult(): array |
|
105 | |||
106 | /** |
||
107 | * Hack: for some requests Telegram sends back an array, integer, string or a boolean value, convert it to boolean |
||
108 | * here |
||
109 | * @return bool |
||
110 | */ |
||
111 | 5 | public function getResultBoolean(): bool |
|
115 | |||
116 | /** |
||
117 | * Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to int here |
||
118 | * @return int |
||
119 | */ |
||
120 | 1 | public function getResultInt(): int |
|
124 | |||
125 | /** |
||
126 | * Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to string |
||
127 | * here |
||
128 | * @return string |
||
129 | */ |
||
130 | 2 | public function getResultString(): string |
|
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | 1 | public function getRawData() |
|
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | 2 | public function getHeaders(): array |
|
150 | |||
151 | /** |
||
152 | * Returns the raw error data |
||
153 | * @return array |
||
154 | */ |
||
155 | 1 | public function getErrorData(): array |
|
159 | |||
160 | /** |
||
161 | * @return \Exception |
||
162 | */ |
||
163 | 1 | public function getException(): \Exception |
|
167 | } |
||
168 |