1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Response class which should be instantiated from received or passed raw JSON string. |
5
|
|
|
* |
6
|
|
|
* @package Teebot (Telegram bot framework) |
7
|
|
|
* |
8
|
|
|
* @author Stanislav Drozdov <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Teebot; |
12
|
|
|
|
13
|
|
|
use Teebot\Entity\Update; |
14
|
|
|
use Teebot\Exception\Fatal; |
15
|
|
|
use Teebot\Entity\AbstractEntity; |
16
|
|
|
use Teebot\Entity\Error; |
17
|
|
|
use Teebot\Exception\Critical; |
18
|
|
|
use Teebot\Exception\Output; |
19
|
|
|
|
20
|
|
|
class Response |
21
|
|
|
{ |
22
|
|
|
const DEFAULT_ENTITY_TYPE = Update::class; |
23
|
|
|
|
24
|
|
|
protected $decodedData = []; |
25
|
|
|
|
26
|
|
|
protected $lastUpdate = 0; |
27
|
|
|
|
28
|
|
|
protected $entities = []; |
29
|
|
|
|
30
|
|
|
protected $parent; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates an instanse of Response class from Raw JSON string and builds corresponding entity |
34
|
|
|
* if passed in entity class. This class should be instantiated for every JSON response from Telegram. |
35
|
|
|
* |
36
|
|
|
* @param string $rawData Raw JSON string |
37
|
|
|
* @param null|string $entityClass Entity class that should be instantiated with decoded JSON data |
38
|
|
|
* @param null|AbstractEntity $parent Parent class should be set as parent for newly instantiated entity |
39
|
|
|
*/ |
40
|
|
|
public function __construct($rawData, $entityClass = null, $parent = null) |
41
|
|
|
{ |
42
|
|
|
$this->parent = $parent; |
43
|
|
|
$this->decodedData = $this->decodeData($rawData); |
|
|
|
|
44
|
|
|
|
45
|
|
|
if (empty($this->decodedData)) { |
46
|
|
|
Output::log(new Critical('Error decoding data!')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$entityClass = $this->isErrorReceived() ? Error::class : $entityClass; |
50
|
|
|
$entitiesSource = $this->getRawEntitiesList(); |
51
|
|
|
$this->entities = $this->buildEntities($entitiesSource, $entityClass); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns JSON data decoded as an array. |
56
|
|
|
* |
57
|
|
|
* @param string $rawData JSON string |
58
|
|
|
* |
59
|
|
|
* @return array|mixed |
60
|
|
|
*/ |
61
|
|
|
protected function decodeData($rawData) |
62
|
|
|
{ |
63
|
|
|
if (!is_string($rawData) || !strlen($rawData)) { |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$result = json_decode($rawData, true); |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks whether an error response from Telegram was received. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function isErrorReceived() |
82
|
|
|
{ |
83
|
|
|
if ((isset($this->decodedData['ok']) && $this->decodedData['ok'] === true)) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Builds entity objects array from an array of raw entity data. Returns an array with built entities. |
92
|
|
|
* |
93
|
|
|
* @param array $rawData Raw entity data array |
94
|
|
|
* @param null|string $entityClass Entity class |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function buildEntities(array $rawData, $entityClass = null) |
99
|
|
|
{ |
100
|
|
|
$entities = []; |
101
|
|
|
$entity = null; |
102
|
|
|
|
103
|
|
|
if (empty($rawData)) { |
104
|
|
|
return $entities; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!isset($rawData[0])) { |
108
|
|
|
$rawData = [$rawData]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($rawData as $rawItemData) { |
112
|
|
|
if (empty($rawItemData)) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
$entity = $this->buildEntity($rawItemData, $entityClass); |
118
|
|
|
|
119
|
|
|
if ($entity && $entity instanceof Update) { |
120
|
|
|
$this->lastUpdate = (int) $entity->getUpdateId(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$entities[] = $entity; |
124
|
|
|
} catch (Fatal $e) { |
125
|
|
|
Output::log($e); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $entities; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Builds desired entity from raw entity's data array. Returns class entity |
134
|
|
|
* |
135
|
|
|
* @param array $rawItemData Array with raw entity's data |
136
|
|
|
* @param null|string $entityClass Entity class to instantiate, if not passed - default Entity class will be used. |
137
|
|
|
* |
138
|
|
|
* @return AbstractEntity |
139
|
|
|
*/ |
140
|
|
|
protected function buildEntity(array $rawItemData, $entityClass = null) |
141
|
|
|
{ |
142
|
|
|
$entityClass = $entityClass ? $entityClass : static::DEFAULT_ENTITY_TYPE; |
143
|
|
|
$entity = null; |
|
|
|
|
144
|
|
|
|
145
|
|
|
if (!class_exists($entityClass)) { |
146
|
|
|
Output::log(new Critical('Entity "' . $entityClass . '" does not exists or not supported yet!')); |
147
|
|
|
} |
148
|
|
|
/** @var AbstractEntity $entity */ |
149
|
|
|
$entity = new $entityClass($rawItemData); |
150
|
|
|
$entity->setParent($this->parent); |
151
|
|
|
|
152
|
|
|
return $entity; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns an array with entities |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function getEntities() |
161
|
|
|
{ |
162
|
|
|
return $this->entities; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns an entity at certain offset or null if entity does not exists. |
167
|
|
|
* |
168
|
|
|
* @param int $offset Offset |
169
|
|
|
* |
170
|
|
|
* @return mixed|null |
171
|
|
|
*/ |
172
|
|
|
public function getEntityByOffset($offset = 0) |
173
|
|
|
{ |
174
|
|
|
return is_array($this->entities) && isset($this->entities[$offset]) ? $this->entities[$offset] : null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns first entity. More meaningful wrapper for self::getEntityByOffset(0) method. |
179
|
|
|
* |
180
|
|
|
* @return mixed|null |
181
|
|
|
*/ |
182
|
|
|
public function getFirstEntity() |
183
|
|
|
{ |
184
|
|
|
return $this->getEntityByOffset(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns raw entities list from decoded JSON data array. |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
protected function getRawEntitiesList() |
193
|
|
|
{ |
194
|
|
|
if (!is_array($this->decodedData) || empty($this->decodedData)) { |
195
|
|
|
return []; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (isset($this->decodedData['result']) && is_array($this->decodedData['result'])) { |
199
|
|
|
return $this->decodedData['result']; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->decodedData; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns count of entities. |
207
|
|
|
* |
208
|
|
|
* @return int |
209
|
|
|
*/ |
210
|
|
|
public function getEntitiesCount() |
211
|
|
|
{ |
212
|
|
|
return count($this->entities); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns last update id gathered from the last Update entity. |
217
|
|
|
* |
218
|
|
|
* @return int |
219
|
|
|
*/ |
220
|
|
|
public function getLastUpdate() |
221
|
|
|
{ |
222
|
|
|
return $this->lastUpdate; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns an offset to skip previous updates in the listener mode. |
227
|
|
|
* |
228
|
|
|
* @return int |
229
|
|
|
*/ |
230
|
|
|
public function getOffset() |
231
|
|
|
{ |
232
|
|
|
return $this->lastUpdate + 1; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..