Completed
Push — master ( e9be36...9488e8 )
by Camilo
04:57
created

TelegramRawData   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 70
ccs 8
cts 18
cp 0.4444
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTypeOfResult() 0 14 4
A getResult() 0 4 1
A getResultBoolean() 0 4 1
A getResultInt() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\InternalFunctionality;
6
7
class TelegramRawData
8
{
9
    /**
10
     * Nothing is done so far with this, but it's always a good idea to have the original around
11
     * @var string
12
     */
13
    private $rawData = '';
14
15
    /**
16
     * The actual representation of the decoded data
17
     * @var array
18
     */
19
    private $decodedData = [];
20
21 17
    public function __construct(string $rawData)
22
    {
23 17
        $this->rawData = $rawData;
24 17
        $this->decodedData = json_decode($this->rawData, true);
25 17
    }
26
27
    /**
28
     * To quickly find out what type of request we are dealing with
29
     *
30
     * Unused so far
31
     *
32
     * @return string
33
     */
34
    public function getTypeOfResult(): string
35
    {
36
        if (is_array($this->decodedData['result'])) {
37
            return 'array';
38
        }
39
40
        if (is_integer($this->decodedData['result'])) {
41
            return 'int';
42
        }
43
44
        if (is_bool($this->decodedData['result'])) {
45
            return 'bool';
46
        }
47
    }
48
49
    /**
50
     * Most of the requests Telegram sends, come as an array, so send the response back as an array by default
51
     *
52
     * @return array
53
     */
54 15
    public function getResult(): array
55
    {
56 15
        return (array)$this->decodedData['result'];
57
    }
58
59
    /**
60
     * Hack: for some requests Telegram sends back an array, integer or a boolean value, convert it to boolean here
61
     * @return bool
62
     */
63 2
    public function getResultBoolean(): bool
64
    {
65 2
        return (bool)$this->decodedData['result'];
66
    }
67
68
    /**
69
     * Hack: for some requests Telegram send back an array, integer or a boolean value, convert it to int here
70
     * @return int
71
     */
72
    public function getResultInt(): int
73
    {
74
        return (int)$this->decodedData['result'];
75
    }
76
}
77