1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\InternalFunctionality; |
6
|
|
|
|
7
|
|
|
use unreal4u\TelegramAPI\Exceptions\ClientException; |
8
|
|
|
use unreal4u\TelegramAPI\Exceptions\InvalidResultType; |
9
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Custom\UnsuccessfulRequest; |
10
|
|
|
|
11
|
|
|
class TelegramResponse |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Used in the TelegramDocument class, saves some processing |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $rawData = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The actual representation of the decoded data |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $decodedData = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The headers sent with the response. |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $headers; |
30
|
|
|
|
31
|
42 |
|
public function __construct(string $rawData, array $headers = []) |
32
|
|
|
{ |
33
|
42 |
|
$this->fillRawData($rawData); |
34
|
40 |
|
$this->headers = $headers; |
35
|
40 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Fills in the raw data |
39
|
|
|
* |
40
|
|
|
* @param string $rawData |
41
|
|
|
* @return TelegramResponse |
42
|
|
|
* @throws \unreal4u\TelegramAPI\Exceptions\ClientException |
43
|
|
|
*/ |
44
|
42 |
|
public function fillRawData(string $rawData): TelegramResponse |
45
|
|
|
{ |
46
|
42 |
|
$this->rawData = $rawData; |
47
|
42 |
|
$this->decodedData = json_decode($this->rawData, true); |
48
|
|
|
|
49
|
42 |
|
if ($this->decodedData['ok'] === false) { |
50
|
2 |
|
$exception = new ClientException(); |
51
|
2 |
|
$exception->setError(new UnsuccessfulRequest($this->decodedData)); |
52
|
2 |
|
throw $exception; |
53
|
|
|
} |
54
|
|
|
|
55
|
40 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* To quickly find out what type of request we are dealing with |
60
|
|
|
* |
61
|
|
|
* Unused so far |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
* @throws InvalidResultType |
65
|
|
|
*/ |
66
|
8 |
|
public function getTypeOfResult(): string |
67
|
|
|
{ |
68
|
8 |
|
switch (gettype($this->decodedData['result'])) { |
69
|
8 |
|
case 'array': |
70
|
6 |
|
case 'integer': |
71
|
5 |
|
case 'boolean': |
72
|
5 |
|
return gettype($this->decodedData['result']); |
73
|
|
|
default: |
74
|
3 |
|
throw new InvalidResultType( |
75
|
3 |
|
sprintf('The passed data type ("%s") is not supported', gettype($this->decodedData['result'])) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Most of the requests Telegram sends, come as an array, so send the response back as an array by default |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
22 |
|
public function getResult(): array |
86
|
|
|
{ |
87
|
22 |
|
return (array)$this->decodedData['result']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Hack: for some requests Telegram sends back an array, integer, string or a boolean value, convert it to boolean |
92
|
|
|
* here |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
5 |
|
public function getResultBoolean(): bool |
96
|
|
|
{ |
97
|
5 |
|
return (bool)$this->decodedData['result']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to int here |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
1 |
|
public function getResultInt(): int |
105
|
|
|
{ |
106
|
1 |
|
return (int)$this->decodedData['result']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to string |
111
|
|
|
* here |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
public function getResultString(): string |
115
|
|
|
{ |
116
|
1 |
|
return (string)$this->decodedData['result']; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
2 |
|
public function getRawData(): string |
123
|
|
|
{ |
124
|
2 |
|
return $this->rawData; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
2 |
|
public function getHeaders(): array |
131
|
|
|
{ |
132
|
2 |
|
return $this->headers; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|