Completed
Pull Request — master (#48)
by Rick
03:30
created

TelegramRawData::fillRawData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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
    /**
24
     * @var \Exception
25
     */
26
    private $exception = null;
27
28
    /**
29
     * Marks the actual response as an error
30
     * @var bool
31
     */
32
    private $isError = false;
33
34 35
    public function __construct(string $rawData = '', \Exception $e = null)
35
    {
36 35
        if (!empty($rawData)) {
37 35
            $this->fillRawData($rawData);
38
        }
39
40 35
        if (!is_null($e)) {
41
            $this->exception = $e;
42
            $this->isError = true;
43
        }
44 35
    }
45
46
    /**
47
     * Will return true if the request was an unsuccessful one, false otherwise
48
     * @return bool
49
     */
50 23
    public function isError(): bool
51
    {
52 23
        return $this->isError;
53
    }
54
55
    /**
56
     * Fills in the raw data
57
     *
58
     * @param string $rawData
59
     * @return TelegramRawData
60
     */
61 35
    public function fillRawData(string $rawData): TelegramRawData
62
    {
63 35
        $this->rawData = $rawData;
64 35
        $this->decodedData = json_decode($this->rawData, true);
65
66 35
        return $this;
67
    }
68
69
    /**
70
     * To quickly find out what type of request we are dealing with
71
     *
72
     * Unused so far
73
     *
74
     * @return string
75
     * @throws InvalidResultType
76
     */
77 8
    public function getTypeOfResult(): string
78
    {
79 8
        switch (gettype($this->decodedData['result'])) {
80 8
            case 'array':
81 6
            case 'integer':
82 5
            case 'boolean':
83 5
                return gettype($this->decodedData['result']);
84
            default:
85 3
                throw new InvalidResultType(
86 3
                    sprintf('The passed data type ("%s") is not supported', gettype($this->decodedData['result']))
87
                );
88
        }
89
    }
90
91
    /**
92
     * Most of the requests Telegram sends, come as an array, so send the response back as an array by default
93
     *
94
     * @return array
95
     */
96 21
    public function getResult(): array
97
    {
98 21
        return (array)$this->decodedData['result'];
99
    }
100
101
    /**
102
     * Hack: for some requests Telegram sends back an array, integer, string or a boolean value, convert it to boolean
103
     * here
104
     * @return bool
105
     */
106 5
    public function getResultBoolean(): bool
107
    {
108 5
        return (bool)$this->decodedData['result'];
109
    }
110
111
    /**
112
     * Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to int here
113
     * @return int
114
     */
115 1
    public function getResultInt(): int
116
    {
117 1
        return (int)$this->decodedData['result'];
118
    }
119
120
    /**
121
     * Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to string
122
     * here
123
     * @return string
124
     */
125
    public function getResultString(): string
126
    {
127
        return (string)$this->decodedData['result'];
128
    }
129
130
    /**
131
     * Returns the raw error data
132
     * @return array
133
     */
134
    public function getErrorData(): array
135
    {
136
        return $this->decodedData;
137
    }
138
139
    /**
140
     * @return \Exception
141
     */
142
    public function getException(): \Exception
143
    {
144
        return $this->exception;
145
    }
146
}
147