|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\InternalFunctionality; |
|
6
|
|
|
|
|
7
|
|
|
use unreal4u\TelegramAPI\Exceptions\InvalidResultType; |
|
8
|
|
|
|
|
9
|
|
|
class TelegramRawData |
|
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
|
31 |
|
public function __construct(string $rawData) |
|
24
|
|
|
{ |
|
25
|
31 |
|
$this->rawData = $rawData; |
|
26
|
31 |
|
$this->decodedData = json_decode($this->rawData, true); |
|
27
|
31 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* To quickly find out what type of request we are dealing with |
|
31
|
|
|
* |
|
32
|
|
|
* Unused so far |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
* @throws InvalidResultType |
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function getTypeOfResult(): string |
|
38
|
|
|
{ |
|
39
|
8 |
|
switch (gettype($this->decodedData['result'])) { |
|
40
|
8 |
|
case 'array': |
|
41
|
6 |
|
case 'integer': |
|
42
|
5 |
|
case 'boolean': |
|
43
|
5 |
|
return gettype($this->decodedData['result']); |
|
44
|
|
|
default: |
|
45
|
3 |
|
throw new InvalidResultType( |
|
46
|
3 |
|
sprintf('The passed data type ("%s") is not supported', gettype($this->decodedData['result'])) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Most of the requests Telegram sends, come as an array, so send the response back as an array by default |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
18 |
|
public function getResult(): array |
|
57
|
|
|
{ |
|
58
|
18 |
|
return (array)$this->decodedData['result']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Hack: for some requests Telegram sends back an array, integer or a boolean value, convert it to boolean here |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function getResultBoolean(): bool |
|
66
|
|
|
{ |
|
67
|
4 |
|
return (bool)$this->decodedData['result']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Hack: for some requests Telegram send back an array, integer or a boolean value, convert it to int here |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function getResultInt(): int |
|
75
|
|
|
{ |
|
76
|
1 |
|
return (int)$this->decodedData['result']; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|