Completed
Push — master ( a4a0d7...5dd6ba )
by Camilo
07:16
created

TelegramMethods::export()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.0534
cc 4
eloc 13
nc 4
nop 0
crap 20
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\Abstracts;
6
7
use Psr\Log\LoggerInterface;
8
use unreal4u\TelegramAPI\Exceptions\MissingMandatoryField;
9
use unreal4u\TelegramAPI\Interfaces\TelegramMethodDefinitions;
10
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
11
use unreal4u\TelegramAPI\Telegram\Types\Message;
12
13
/**
14
 * Contains methods that all Telegram methods should implement
15
 */
16
abstract class TelegramMethods implements TelegramMethodDefinitions
17
{
18
    /**
19
     * Most of the methods will return a Message object on success, so set that as the default.
20
     *
21
     * This function may however be overwritten if the method uses another object, there are many examples of this, so
22
     * just check out the rest of the code. A good place to start is GetUserProfilePhotos or LeaveChat
23
     *
24
     * @see unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos
25
     * @see unreal4u\TelegramAPI\Telegram\Methods\LeaveChat
26
     *
27
     * @param TelegramRawData $data
28
     * @param LoggerInterface $logger
29
     *
30
     * @return TelegramTypes
31 8
     */
32
    public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
33 8
    {
34
        return new Message($data->getResult(), $logger);
35
    }
36
37
    /**
38
     * Before making the actual request this method will be called
39
     *
40
     * It must be used to json_encode stuff, or do other changes in the internal class representation _before_ sending
41
     * it to the Telegram servers
42
     *
43
     * @return TelegramMethods
44 22
     */
45
    public function performSpecialConditions(): TelegramMethods
46 22
    {
47 1
        if (!empty($this->reply_markup)) {
48
            $this->reply_markup = json_encode($this->reply_markup);
49
        }
50 22
51
        return $this;
52
    }
53
54
    public function export(): array
55
    {
56
        $finalArray = [];
57
        $objectProspect = get_object_vars($this);
58
        $cleanObject = new $this();
59
        foreach ($objectProspect as $fieldId => $value) {
60
            // Strict comparison, type checking!
61
            if ($objectProspect[$fieldId] === $cleanObject->$fieldId) {
62
                if (in_array($fieldId, $this->getMandatoryFields())) {
63
                    throw new MissingMandatoryField(sprintf(
64
                        'The field "%s" is mandatory and empty, please correct',
65
                        $fieldId
66
                    ));
67
                }
68
            } else {
69
                $finalArray[$fieldId] = $value;
70
            }
71
        }
72
73
        return $finalArray;
74
    }
75
}
76