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
|
36 |
|
public function __construct(string $rawData) |
24
|
|
|
{ |
25
|
36 |
|
$this->rawData = $rawData; |
26
|
36 |
|
$this->decodedData = json_decode($this->rawData, true); |
27
|
36 |
|
} |
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
|
22 |
|
public function getResult(): array |
57
|
|
|
{ |
58
|
22 |
|
return (array)$this->decodedData['result']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Hack: for some requests Telegram sends back an array, integer, string or a boolean value, convert it to boolean here |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
5 |
|
public function getResultBoolean(): bool |
66
|
|
|
{ |
67
|
5 |
|
return (bool)$this->decodedData['result']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Hack: for some requests Telegram send back an array, integer, string 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
|
|
|
/** |
80
|
|
|
* Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to string here |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getResultString(): string |
84
|
|
|
{ |
85
|
|
|
return (string)$this->decodedData['result']; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|