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

BaseType::createFromJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
}