Completed
Push — master ( 26dc26...9f091f )
by Camilo
02:31
created

TelegramTypes::mapSubObjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\Abstracts;
6
7
abstract class TelegramTypes
8
{
9 14
    public function __construct(\stdClass $data = null)
10
    {
11 14
        $this->populateObject($data);
12 14
    }
13
14
    /**
15
     * Fills the class with the data passed on through the constructor
16
     *
17
     * @param \stdClass $data
18
     * @return TelegramTypes
19
     */
20 14
    final protected function populateObject(\stdClass $data = null): TelegramTypes
21
    {
22 14
        if (!is_null($data)) {
23 14
            $subObjects = $this->mapSubObjects();
24 14
            foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type object<stdClass> is not traversable.
Loading history...
25 14
                if (!empty($subObjects) && array_key_exists($key, $subObjects)) {
26 12
                    $className = 'unreal4u\\Telegram\\Types\\' . $subObjects[$key];
27 12
                    $candidateKey = new $className($value);
28 12
                    if (isset($candidateKey->data)) {
29 4
                        $this->$key = $candidateKey->data;
30
                    } else {
31 12
                        $this->$key = $candidateKey;
32
                    }
33
                } else {
34 14
                    $this->$key = $value;
35
                }
36
            }
37
        }
38
39 14
        return $this;
40
    }
41
42
    /**
43
     * The default is that we have no subobjects at all
44
     *
45
     * @return array
46
     */
47 13
    protected function mapSubObjects(): array
48
    {
49 13
        return [];
50
    }
51
}
52