zend-blog-3-backend
81 строка · 1.4 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 24.09.17
6* Time: 14:03
7*/
8
9namespace Xelbot\Telegram;
10
11use Xelbot\Telegram\Exception\TelegramException;
12
13/**
14* @method string getDescription()
15*/
16class TelegramResponse
17{
18/**
19* @var bool
20*/
21protected $ok;
22
23/**
24* @var array|null
25*/
26protected $result;
27
28/**
29* @var array
30*/
31protected $responseData = [];
32
33/**
34* @param array $data
35*/
36public function __construct(array $data)
37{
38$this->ok = isset($data['ok']) && $data['ok'];
39$this->result = $data['result'] ?? null;
40
41$this->responseData = $data;
42}
43
44/**
45* @return bool
46*/
47public function isOk(): bool
48{
49return $this->ok;
50}
51
52/**
53* @return array|null
54*/
55public function getResult(): ?array
56{
57return $this->result;
58}
59
60/**
61* @param $method
62* @param $args
63*
64* @throws TelegramException
65*
66* @return mixed
67*/
68public function __call($method, $args)
69{
70$action = substr($method, 0, 3);
71if ($action === 'get') {
72$propertyName = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($method, 3)), '_'));
73
74if (isset($this->responseData[$propertyName])) {
75return $this->responseData[$propertyName];
76}
77}
78
79throw new TelegramException('Undefined method: ' . $method);
80}
81}
82