Passed
Push — master ( 58897c...6910c8 )
by Alexander
06:09 queued 12s
created

BaseType::fromResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 10
cc 2
nc 2
nop 1
crap 2.0116
1
<?php
2
3
namespace TelegramBot\Api;
4
5
/**
6
 * Class BaseType
7
 * Base class for Telegram Types
8
 *
9
 * @package TelegramBot\Api
10
 */
11
abstract class BaseType
12
{
13
    /**
14
     * Array of required data params for type
15
     *
16
     * @var array
17
     */
18
    protected static $requiredParams = [];
19
20
    /**
21
     * Map of input data
22
     *
23
     * @var array
24
     */
25
    protected static $map = [];
26
27
    /**
28
     * Validate input data
29
     *
30
     * @param array $data
31
     *
32
     * @return bool
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 115
    public static function validate($data)
37
    {
38 115
        if (count(array_intersect_key(array_flip(static::$requiredParams), $data)) === count(static::$requiredParams)) {
39 89
            return true;
40
        }
41
42 26
        throw new InvalidArgumentException();
43
    }
44
45 88
    public function map($data)
46
    {
47 88
        foreach (static::$map as $key => $item) {
48 88
            if (isset($data[$key]) && (!is_array($data[$key]) || (is_array($data[$key]) && !empty($data[$key])))) {
49 88
                $method = 'set' . self::toCamelCase($key);
50 88
                if ($item === true) {
51 88
                    $this->$method($data[$key]);
52 88
                } else {
53 28
                    $this->$method($item::fromResponse($data[$key]));
54
                }
55 88
            }
56 88
        }
57 88
    }
58
59 102
    protected static function toCamelCase($str)
60
    {
61 102
        return str_replace(" ", "", ucwords(str_replace("_", " ", $str)));
62
    }
63
64 16
    public function toJson($inner = false)
65
    {
66 16
        $output = [];
67
68 16
        foreach (static::$map as $key => $item) {
69 16
            $property = lcfirst(self::toCamelCase($key));
70 16
            if (!is_null($this->$property)) {
71 16
                if (is_array($this->$property)) {
72 5
                    $output[$key] = array_map(
73 5
                        function ($v) {
74 5
                            return is_object($v) ? $v->toJson(true) : $v;
75 5
                        },
76 5
                        $this->$property
77 5
                    );
78 5
                } else {
79 14
                    $output[$key] = $item === true ? $this->$property : $this->$property->toJson(true);
80
                }
81 16
            }
82 16
        }
83
84 16
        return $inner === false ? json_encode($output) : $output;
85
    }
86
87 113
    public static function fromResponse($data)
88
    {
89 113
        if ($data === true) {
90
            return true;
91
        }
92
        
93 113
        self::validate($data);
94 88
        $instance = new static();
95 88
        $instance->map($data);
96
97 88
        return $instance;
98
    }
99
}
100