Completed
Push — master ( d90799...9daa28 )
by Алексей
12:20 queued 09:23
created

BaseType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 21 6
A create() 0 4 1
A createFromJson() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: akeinhell
5
 * Date: 27.06.16
6
 * Time: 11:58
7
 */
8
9
namespace Telegram\Base;
10
11
12
use Telegram\Helpers\AnnotationHelper;
13
14
class BaseType
15
{
16
    /**
17
     * BaseType constructor.
18
     * @param array $data
19
     */
20
    public function __construct($data)
21
    {
22
        if (!is_array($data)) {
23
            return $data;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
24
        }
25
        foreach ($data as $key => $value) {
26
            $annotatinons = AnnotationHelper::getAnnotations($this, $key);
27
            if (!property_exists($this, $key)) {
28
                $this->$key = $value;
29
                continue;
30
            }
31
32
            if (array_key_exists('var', $annotatinons)) {
33
                $class = '\\Telegram\\Types\\' . $annotatinons['var'];
34
                if (class_exists($class)) {
35
                    /** @var BaseType $class */
36
                    $this->$key = $class::create($value);
37
                }
38
            }
39
        }
40
    }
41
42
    /**
43
     * @param $data
44
     * @return static
45
     */
46
    public static function create($data)
47
    {
48
        return new static($data);
49
    }
50
51
    /**
52
     * @param $json
53
     * @return static
54
     */
55
    public static function createFromJson($json)
56
    {
57
        return new static(json_decode($json, true));
58
    }
59
}